我有一個SQL查詢(見下文),返回我需要的,但是當通過phpMyAdmin運行需要從0.0009秒到0.1149秒,偶爾一直到7.4983秒。爲什麼MySQL查詢需要從1毫秒到7秒的任何地方?
查詢:
SELECT
e.id,
e.title,
e.special_flag,
CASE WHEN a.date >= '2013-03-29' THEN a.date ELSE '9999-99-99' END as date
CASE WHEN a.date >= '2013-03-29' THEN a.time ELSE '99-99-99' END as time,
cat.lastname,
FROM e_table as e
LEFT JOIN a_table as a ON (a.e_id=e.id)
LEFT JOIN c_table as c ON (e.c_id=c.id)
LEFT JOIN cat_table as cat ON (cat.id=e.cat_id)
LEFT JOIN m_table as m ON (cat.name=m.name AND cat.lastname=m.lastname)
JOIN (
SELECT DISTINCT innere.id
FROM e_table as innere
LEFT JOIN a_table as innera ON (innera.e_id=innere.id AND
innera.date >= '2013-03-29')
LEFT JOIN c_table as innerc ON (innere.c_id=innerc.id)
WHERE (
(
innera.date >= '2013-03-29' AND
innera.flag_two=1
) OR
innere.special_flag=1
) AND
innere.flag_three=1 AND
innere.flag_four=1
ORDER BY COALESCE(innera.date, '9999-99-99') ASC,
innera.time ASC,
innere.id DESC LIMIT 0, 10
) AS elist ON (e.id=elist.id)
WHERE (a.flag_two=1 OR e.special_flag) AND e.flag_three=1 AND e.flag_four=1
ORDER BY a.date ASC, a.time ASC, e.id DESC
解釋計劃:
的問題是: 此查詢的一部分可能會造成大範圍的性能差異?
你能後的上
對於「一」表,索引的索引解釋計劃? – Taryn
你有一個索引上的所有連接列(a.e_id,e.id,c.id,e.cat_id))和過濾列(a.flag_two,e.special_flag,e.flag_three? –
@bluefeet我我」將不得不穀歌如何做到這一點,我會一次我想出如何讓「解釋計劃」 –