についてどのように:
Tables:
---------
user:
user_id, (Primary Key - DB will create index automatically)
username, (Add unique index to prevent duplicate usernames)
created_on
city:
city_id, (Primary Key)
country, (You may want to index some of these location fields, but I would
region, wait until you see the need for them based on your queries)
city,
latitude,
longitude
user_location:
user_id, (If you want a user to only have one location, then create a primary
city_id, key for user_id and city_id. (Composite) If you want to allow multiple
update_on per user then create a non-unique composite index on user_id and city_id
user_hobby:
user_id, (Create a unique composite index on user_id and hobby_id)
hobby_id
hobby:
hobby_id, (Primary Key)
hobby_name (Create a unique index to prevent duplicate hobbies with different keys)
SQL:
---------
SELECT user_id, username, c.country, c.region, c.city
FROM user u
JOIN user_location ul ON (u.user_id = ul.user_id)
JOIN city c ON (ul.city_id = c.city_id)
JOIN user_hobby uh ON (h.user_id = uh.user_id)
JOIN hobby h ON (uh.hobby_id = h.hobby_id)
WHERE h.hobby_name = 'Model Cars';
あなたはこれらのいくつかは、あなたが必要とするアプリケーションのために必要ではないか、ということかもしれません索引を追加することもできますが、これは開始するのに適しています。使用しているDBは指定していませんが、LAMPスタックを使用すると仮定します。ここにはcreating indexes via MySQLの情報があります。ユーザーテーブルにユーザー名に一意索引の例は次のようになります。
CREATE UNIQUE INDEX idx_unq_user_username ON user(username);
な限りそれはtrival例えばテーブルの多くのように見えるかもしれませんが、リレーショナルデータベースにあなたは一般的に、あなたのテーブルを正規化したいです可能。一般的なクエリがある場合、より簡単なクエリでデータにアクセスできるようにするビューを作成できます。テーブルをこのように設定するもう1つの側面は、列が意味を成す場所に簡単に追加できることです。あなたの最初のスキーマでは、ユーザーテーブル内に都市を格納してから緯度/経度を追加したい場合、ユーザーテーブルが偶然に配置されたユーザーテーブルを持つ場所テーブルのように見えるようになります。
ノーマライズは、実際の更新がほとんどない状態でデータの変更を伝えることができ、データの密集度を高めてクエリを満たすためのI/O要件を軽減し、データの整合性を保つことができます。
適切なインデックスを使用すると、これはすべての点で(スピード、保守性、一貫性)最善の解決策になります。 – CodeCaster
助けてくれてありがとう、私は初心者ですので、クエリのパフォーマンスが良いかどうか知りたいですか? – carma
インデックスを使用する方法はわかりませんが、タンクは – carma