我嘗試避免子查詢,因爲他們通常比正確的連接性能低得多。MySQL - 左連接和不在
這是我目前不工作查詢:
SELECT
a.`email_list_id`, a.`category_id`, a.`name`
FROM
`email_lists`AS a
LEFT JOIN `email_autoresponders` AS b
ON (a.`website_id` = b.`website_id`)
WHERE
a.`website_id` = [...]
AND a.`category_id` <> 0
AND a.`email_list_id` <> b.`email_list_id`
GROUP BY
a.`email_list_id`
ORDER BY a.`name`
此查詢的工作:
SELECT
`email_list_id`, `category_id`, `name`
FROM
`email_lists`
WHERE
`website_id` = [...]
AND `category_id` <> 0
AND `email_list_id` NOT IN (
SELECT
`email_list_id`
FROM
`email_autoresponders`
WHERE `website_id` = [...]
)
GROUP BY
`email_list_id`
ORDER BY
`name`
有什麼辦法用左連接做到這一點?我嘗試了許多不同的選擇。
永遠不要對性能的假設。工作查詢的時間是什麼? – 2011-06-14 21:49:00
.0014,但這是一個基本的例子,它總共搜索5條記錄。如果它搜索5,000條記錄,我懷疑它會執行相同的操作。 – 2011-06-14 21:51:17