我的数据库表name_info有30K行&术语表60K行,当选择不在表中时,服务器CPU上升。
如何才能最好地编写一个快速选择1行的查询?
SELECT slug FROM terms WHERE slug LIKE 'nm%' AND slug NOT IN
(SELECT imdb_id FROM name_info) LIMIT 1
答案 0 :(得分:2)
确保将查询中使用的列编入索引。
SELECT `slug`
FROM `terms`
LEFT JOIN `name_info`
ON `slug` = `imdb_id`
WHERE slug LIKE 'nm%'
AND `imdb_id` IS NULL
LIMIT 0,1;
答案 1 :(得分:0)
尝试使用不存在
SELECT slug FROM terms WHERE slug LIKE 'nm%' AND not exists
(SELECT 1 FROM name_info where imdb_id = slug) LIMIT 1