I have just started using mySQL after a few years using MSSQL and I have hit a road block. For some reason the performance on a subselect within an 'in' clause in mySQL is abysmal.
I have a fairly simple query:
SELECT SYMBOL, PRICE, SELLBROKER, TRADETIMESTAMP
WHERE SYMBOL IN ( SELECT DISTINCT SYMBOL
WHERE PREVIOUSTICKDIRECTION < 1
GROUP BY SYMBOL
HAVING COUNT(SELLBROKER) > 3)
ORDER BY SYMBOL, timestamp(TRADETIMESTAMP) ASC;
As it stands this takes 35 SEC (omg!) to run. If i run the subselect separately it only takes .03s to run! If I grab the 200 records from it in a comma delimited list (i.e. 'AAA', 'BBB', 'CCC') and throw it in the IN clause manually the query runs super fast. So it is the subquery within the in clause itself that it does not like. MS SQL would handle this no problem. The only article on the net that I could actually find to explain this problem is:
I don't have the option (from what I know about the nature of this query) to do any type of join. I mean, it seems pretty straight forward, and I am surprised on the lack of documentation on this. Can anyone help please!!
The SYMBOL column is indexed as well. Originally I thought it was the problem but the fact that I can enter a comma delimited string explicitly and it runs fine shows that it isnt an index problem.
My table currently has 10k records in it but will have many more.