1
該查詢運行得很好,但如果將AND h2.delete_datetime IS NULL
條件從WHERE
子句移動到左連接的ON
子句中,該查詢將持續進行。這使我感到困惑,因爲我習慣於將盡可能多的條件放在ON
條款中以提高性能(ON
條款中的更多條件意味着連接的行數少,行數少)。在這裏看來相反,我想知道爲什麼。與WHERE子句相比,IS NULL檢查在ON子句中性能更差
好:
SELECT
count(*),
min(h.history_date) AS history_date
FROM
history AS h
LEFT JOIN
history AS h2
ON (
h2.contacts_id = h.contacts_id
AND h2.history_status_id = 59
/*AND h2.delete_datetime IS NULL*/
)
WHERE
h.history_status_id = 58
AND h2.contacts_id IS NULL
AND h.delete_datetime IS NULL
AND h2.delete_datetime IS NULL
ORDER BY h.history_date DESC
VS壞:
SELECT
count(*),
min(h.history_date) AS history_date
FROM
history AS h
LEFT JOIN
history AS h2
ON (
h2.contacts_id = h.contacts_id
AND h2.history_status_id = 59
AND h2.delete_datetime IS NULL
)
WHERE
h.history_status_id = 58
AND h2.contacts_id IS NULL
AND h.delete_datetime IS NULL
/*AND h2.delete_datetime IS NULL*/
ORDER BY h.history_date DESC
原則上,它應該不重要,因爲優化器應該處理這個問題(如果查詢是可比較的)。 – MrTux