Optimizing SQL Select: Using ‘IN’ in MySQL

April 10th, 2008  |  Published in Tips

I’ve known for a bit that using the ‘IN’ condition for MySQL queries is much much slower then it should be. I didn’t realize how much will I was browsing the website Common Queries for MySQL and came across the section called The unbearable slowness of IN().

According to this page, using IN is two times slower then using EXISTS which is 50 times slower then using a JOIN. Wow! I don’t think I’ll be using IN anymore.

Conclusion, if you want to make your query faster don’t use IN. Take a look at The unbearable slowness of IN() for how to write the query using an EXIST or, if possible, a JOIN.

Leave a Response

Subscribe via RSS

If you like the content of this website and are looking for a way to be notified of new content, look no further. Just click the orange icon to your right and subscribe using your favorite feed reader.