I found this query in our web app that has been made by hired programmers. It performs like a dog and needs optimization, but I'm no MySQL wizard (yet), unfortunately. Please help, anything is appreciated.
This is the query:
SELECT DISTINCT test_user_info_test.ID
FROM test_user_info_test, test_user_saver_test, test_user_filter_test
WHERE test_user_info_test.ACTIVE = '1'
AND (
( test_user_info_test.BALANCE >= '1' AND test_user_info_test.ID IN
(
SELECT USERID
FROM test_user_filter_test
WHERE symbol = 'x'
AND INCLUDE ='1')
)
OR
(
HAS_IT = '1' AND EXPIRE > '1337500000'
AND
test_user_info_test.ID IN
(
SELECT USERID
FROM test_user_saver_test
WHERE symbol = 'x'
AND INCLUDE = '1'
)
)
)
Currently, the tables don't have any indexes.
Description of what the query is supposed to do: it needs to select the user id's from active users that have a positive balance and have set a filter for symbol X, in this example. Also it needs to select the user id's from users who have a special subscription that hasn't expired yet and have set a filter for symbol X.
I think the execution of the query can take up to quite a few minutes, which is clearly unacceptable.
FROM test_user_info_test, test_user_saver_test, test_user_filter_testto the simpleFROM test_user_info_test. And also remove theDISTINCT. And then of course test if the resulting query gives the same results as this one! – ypercube May 22 '12 at 20:13