2010-07-17 80 views
3

我必須爲這樣的查詢設置哪些索引?謝謝mysql索引設置爲查詢

SELECT distinct event_dates.* FROM `event_dates` 
INNER JOIN `events` ON `events`.id = `event_dates`.event_id 
INNER JOIN `cores` ON `cores`.resource_id = `events`.id AND cores.resource_type = 'Event' 
INNER JOIN `cores_kinds` ON `cores_kinds`.core_id = `cores`.id 
INNER JOIN `kinds` ON `kinds`.id = `cores_kinds`.kind_id 
WHERE (((super_kind_en IN ('party','cinema and theater')) 
AND (day >= '2010-07-17' AND day <= '2010-08-16')) 
AND (cores.searchable like '%p%')) 
ORDER BY day, cores.vote desc LIMIT 0, 30 
+1

super_kind_en,day,cores.searchable – Sarfraz 2010-07-17 07:54:13

回答

3

一般的經驗法則是在篩選結果集的列上有索引。換句話說,包含在WHERE子句中的列和您執行連接的列。

對於特定查詢,您可能想引用查詢的解釋計劃。它會告訴你的MySQL如何執行查詢,這樣可以相應地設置指標:http://dev.mysql.com/doc/refman/5.0/en/explain.html

+0

ORDER BY和SELECT可以觸發索引使用;特別是JOIN標準應該被評估用於索引創建。 – 2010-07-17 16:23:04

0

我把它們放在

events.id 
event_dates.event_id 
cores.resource_id 
events.id 
cores.resource_type 
cores_kinds.core_id 
cores.id 
kinds.id 
cores_kinds.kind_id 
super_kind_en 
day 
cores.searchable 
cores.vote 

,因爲他們都出現在你的任何一個JOIN,WHERE或ORDER BY條款。