這裏是我的查詢的 「解釋」:查詢太慢,並且不使用索引
explain
select eil.sell_fmt, count(sell_fmt) as itemCount
from table_items eil
where eil.cl_Id=123 and eil.si_Id='0'
and start_date <= now() and end_date is not null and end_date < NOW()
group by eil.sell_fmt
沒有日期(日期,結束日期)濾波器:
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE eil ref table_items_clid_siid_sellFmt 39 const,const 7393 Using where; Using index
隨着日期的過濾器:
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE eil ref table_items_clid_siid_sellFmt 39 const,const 8400 Using where
possible_keys是:
table_items_clid_siid, table_items_clid_siid_itemId, table_items_clid_siid_startDate_endDate, table_items_clid_siid_sellFmt
沒有日期過濾器的查詢速度非常快(0.4秒),但是使用日期過濾器的時間大約需要30秒。總記錄只有14K。
表的字段類型:
`cl_Id` int(11) NOT NULL,
`si_Id` varchar(11) NOT NULL,
`start_date` datetime DEFAULT NULL,
`end_date` datetime DEFAULT NULL,
`sell_fmt` varchar(20) DEFAULT NULL
我串接場名稱,以索引名,這樣你就可以估算出指數可結合領域。
有人可以指導我嗎?這裏發生了什麼?什麼是我應該採取的最佳行動方式,或者我做錯了什麼?
我需要一個更suggestio N:在同一個表中的另一個查詢,用戶可以過濾基於UPTO 10個字段,和在字段沒有明確的順序(隨機沒有以隨機順序字段)。那麼這種類型的搜索會再次變得太慢。那麼最好的策略是什麼?一個覆蓋索引與「所有」可能的可搜索字段?如果是的話,索引事項中的字段順序是? (即如果該順序不同於查詢中字段的順序,將使用索引嗎?
可能你請發表create table語句? – dav
是否有可能在你的設置中使用'start_date'超出'end_date'? – Quassnoi
這真的是一個14k行的表嗎?或者查詢返回14k行? – piotrm