I am facing a hard time optimizing a query like
SELECT RESULT_ID FROM RESULTS
WHERE SOURCE = 1 AND GROUP=2 AND SCORE1 BETWEEN 20 AND 100
ORDER BY SCORE2 LIMIT 450;
on a 40 million rows innodb table. The query may have to sort upto 15 million results to get the top 450. So far, I have tried :
- Defining indexes but those don't get used to sort because MySQL ignores any columns in the index after the range condition. Since we have a bunch of score columns, we could get range conditions on a number of them followed by sorting on a particular score and the limiting the result set to top 450.
- Using memory tables, but those don't perform well when sorting such large results.
- Sphinx, but I am not sure if it will help in these kinds of queries.
Also, Is there any OLAP cube implementation that can optimize these kind of queries ?