We help IT Professionals succeed at work.
Get Started

sub select in 'in clause'

Turkish27
Turkish27 asked
on
328 Views
Last Modified: 2012-08-14
Hi All,

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
FROM        dbname.TRADES
WHERE       SYMBOL IN ( SELECT      DISTINCT SYMBOL
                        FROM        dbname.TRADES
                        WHERE       PREVIOUSTICKDIRECTION < 1
                        GROUP BY    SYMBOL
                        HAVING      COUNT(SELLBROKER) > 3)
ORDER BY    SYMBOL, timestamp(TRADETIMESTAMP) ASC;

Open in new window


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:

http://spin.atomicobject.com/2011/03/25/mysql-in-query-performance/

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.

Thank you!
Comment
Watch Question
This problem has been solved!
Unlock 2 Answers and 5 Comments.
See Answers
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE