1

My DB table name_info has it 30K row & terms table 60K row, When select not in table, server CPU goes up.

How can I best write a query that select 1 row fast?

SELECT slug FROM terms WHERE slug LIKE 'nm%' AND slug NOT IN 
(SELECT imdb_id FROM name_info) LIMIT 1
Parsa
  • 596
  • 1
  • 5
  • 15

2 Answers2

2

Make sure that the columns used in the query are indexed.

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
  • 4,953
  • 3
  • 22
  • 40
0

try to use not exists

SELECT slug FROM terms WHERE slug LIKE 'nm%' AND not exists
(SELECT 1 FROM name_info where imdb_id = slug) LIMIT 1
Vecchiasignora
  • 1,275
  • 7
  • 6