O
O
Optimus2016-08-10 14:00:04
MySQL
Optimus, 2016-08-10 14:00:04

Slow condition in sql query?

In a table with 3 million records. The first query is very fast:

SELECT SQL_NO_CACHE `id`,`users`
FROM `table` WHERE `users` > 3000 ORDER BY `users` DESC LIMIT 0,10

But it is worth adding the WHERE `date` IS NULL condition (there is an index on date, but there are half repetitions in it), as the execution time drops to 8 seconds.
SELECT SQL_NO_CACHE `id`,`users`
FROM `table` WHERE `date` IS NULL AND `users` > 3000 ORDER BY `users` DESC LIMIT 0,10

explain of the second query shows the possible keys users,date is used by users. The type is range, and rows is 259k.
What is the best way to proceed in this case?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question