I’m using Hibernate’s JPA implementation with MySQL 5.0.67. MySQL is configured to use InnoDB.
In performing a JPA query (which is translated to SQL), I’ve discovered that using the IN clause is slower than performing individual queries. Example:
SELECT p FROM Person p WHERE p.name IN ('Joe', 'Jane', 'Bob', 'Alice')
is slower than four separate queries:
SELECT p FROM Person p WHERE p.name = 'Joe' SELECT p FROM Person p WHERE p.name = 'Jane' SELECT p FROM Person p WHERE p.name = 'Bob' SELECT p FROM Person p WHERE p.name = 'Alice'
Why is this? Is this a MySQL performance limitation?
This is a known deficiency in MySQL.
It is often true that using
UNIONperforms better than a range query like the one you show. MySQL doesn’t employ indexes very intelligently for expressions usingIN (...). A similar hole exists in the optimizer for boolean expressions withOR.See http://www.mysqlperformanceblog.com/2006/08/10/using-union-to-implement-loose-index-scan-to-mysql/ for some explanation and detailed benchmarks.
The optimizer is being improved all the time. A deficiency in one version of MySQL may be improved in a subsequent version. So it’s worth testing your queries on different versions.
It is also advantageous to use
UNION ALLinstead of simplyUNION. Both queries use a temporary table to store results, but the difference is thatUNIONappliesDISTINCTto the result set, which incurs an additional un-indexed sort.