MySQL从快速30K行中选择NOT IN

时间:2017-04-11 07:19:36

标签: mysql sql select cpu-usage

我的数据库表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

2 个答案:

答案 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