我有了很多列的用戶表,它看起來大致是這樣的:建議需要適當的索引中搜索與許多字段的表上
dname: { type: string(255), notnull: true }
email: { type: string(255), notnull: true, unique: true }
email_code: { type: string(255) }
email_confirmed: { type: boolean, default: false }
profile_filled: { type: boolean, default: false }
password: { type: string(255), notnull: true }
image_id: { type: integer }
gender: { type: enum, values: [male, female] }
description: { type: string }
dob: { type: date }
height: { type: integer(3) }
looks: { type: enum, values: [thin, average, athletic, heavy] }
looking_for: { type: enum, values: [marriage, dating, friends] }
looking_for_age1: { type: integer }
looking_for_age2: { type: integer }
color_hair: { type: enum, values: [black, brown, blond, red] }
color_eyes: { type: enum, values: [black, brown, blue, green, grey] }
marital_status: { type: enum, values: [single, married, divorced, widowed] }
smokes: { type: enum, values: [no, yes, sometimes] }
drinks: { type: enum, values: [no, yes, sometimes] }
has_children: { type: enum, values: [no, yes] }
wants_children: { type: enum, values: [no, yes] }
education: { type: enum, values: [school, college, university, masters, phd] }
occupation: { type: enum, values: [no, yes] }
country_id: { type: integer }
city_id: { type: integer }
lastlogin_at: { type: timestamp }
deleted_at: { type: timestamp }
我已經創建了一個包含了大部分的一種形式字段(枚舉,國家,城市),允許用戶根據他們選擇的字段生成where語句。因此,如果有人選擇抽菸:不和COUNTRY_ID:7那麼SQL where語句可能看起來像這樣:
SELECT id
FROM user u
WHERE u.deleted_t IS NULL AND u.profile_filled IS NOT NULL AND smokes = 'no' AND country_id = 7;
因爲用戶可以選擇字段的任意組合進行過濾,我不知道我應該怎麼去索引這張表,我應該只在所有可以過濾的字段上創建一個列索引?你會建議什麼?
請告訴我,你的'真正'查詢中使用綁定變量。 – Gerrat 2011-01-06 01:26:13
任何特殊原因:height,looking_for_age1,looking_for_age2,country_id都是有符號整數(4字節)。有人可以是-2147483648英尺高或2147483647歲?你不認爲tinyint UNSIGNED會適合年齡和country_id(0..255歲,0..255個國家)身高可能應該是小數(3,2)。你有沒有考慮過當你在表中有兩百萬行時會發生什麼,你需要擴展現有的枚舉? – 2011-01-06 01:27:23
@Gerrat,我還沒有使用綁定變量,你能建議一個很好的閱讀? – BugBusterX 2011-01-06 01:51:47