我的數據庫表name_info有30K行&條款表60K行,當選擇不在表中時,服務器CPU上升。MySQL選擇不從30K行快速
如何最好地編寫一個快速選擇1行的查詢?
SELECT slug FROM terms WHERE slug LIKE 'nm%' AND slug NOT IN
(SELECT imdb_id FROM name_info) LIMIT 1
我的數據庫表name_info有30K行&條款表60K行,當選擇不在表中時,服務器CPU上升。MySQL選擇不從30K行快速
如何最好地編寫一個快速選擇1行的查詢?
SELECT slug FROM terms WHERE slug LIKE 'nm%' AND slug NOT IN
(SELECT imdb_id FROM name_info) LIMIT 1
確保在查詢中使用的列是建立索引的。
SELECT `slug`
FROM `terms`
LEFT JOIN `name_info`
ON `slug` = `imdb_id`
WHERE slug LIKE 'nm%'
AND `imdb_id` IS NULL
LIMIT 0,1;
謝謝@ sloan-thrasher此解決方案完美工作 – Parsa
@Parsa,不客氣。請將答案標記爲已接受。 –
嘗試使用不存在
SELECT slug FROM terms WHERE slug LIKE 'nm%' AND not exists
(SELECT 1 FROM name_info where imdb_id = slug) LIMIT 1
謝謝@Vecchiasignora,但行爲就像我的sql代碼 – Parsa
什麼數據類型是蛞蝓和immdb_id? – Jens
String/varchar like nm1084488 – Parsa
嘗試使用name.info.imdb_id和terms.slug上的外部聯接將名詞連接到name_info。連接可能會比子查詢更好。 –