In my Java application I have found a small performance issue, which is caused by such simple query:
SELECT DISTINCT a
FROM table
WHERE checked = 0
LIMIT 10000
I have index on the checked column.
In the beginning, the query is very fast (i.e. where almost all rows have checked = 0). But as I mark more and more rows as checked, the query becomes greatly inefficient (up to several minutes).
How can I improve the performance of this query ? Should I add a complex index
- a, checked
or rather
- checked, a?
My table has a lot of millions of rows, that is why I do not want to test it manually and hope to have lucky guess.
I have found a completely different solution which would do the trick. I will simple create a new table with all possible unique “a” values. This will allow me to avoid DISTINCT