I am wondering if there is any difference in regards to performance between the following
SELECT ... FROM ... WHERE someFIELD IN(1,2,3,4)
SELECT ... FROM ... WHERE someFIELD between 0 AND 5
SELECT ... FROM ... WHERE someFIELD = 1 OR someFIELD = 2 OR someFIELD = 3 ...
or will MySQL optimize the SQL in the same way compilers will optimize code ?
EDIT: Changed the AND
's to OR
's for the reason stated in the comments.
I know that, as long as you have an index on Field, the BETWEEN will use it to quickly find one end, then traverse to the other. This is most efficient.
Every EXPLAIN I've seen shows "IN ( ... )" and " ... OR ..." to be interchangeable and equally (in)efficient. Which you would expect, since the optimizer has no way to know whether or not they comprise an interval. It's also equivalent to a UNION ALL SELECT on the individual values.
I also did a test for future Googlers. Total count of returned results is 7264 out of 10000
This query took
0.1239
secondsThis query took
0.0433
secondsIN
is 3 times faster thanOR
I needed to know this for sure, so I benchmarked both methods. I consistenly found
IN
to be much faster than usingOR
.Do not believe people who give their "opinion", science is all about testing and evidence.
I ran a loop of 1000x the equivalent queries (for consistency, I used
sql_no_cache
):IN
: 2.34969592094sOR
: 5.83781504631sUpdate:
(I don't have the source code for the original test, as it was 6 years ago, though it returns a result in the same range as this test)
In request for some sample code to test this, here is the simplest possible use case. Using Eloquent for syntax simplicity, raw SQL equivalent executes the same.
The accepted answer doesn't explain the reason.
Below are quoted from High Performance MySQL, 3rd Edition.
OR will be slowest. Whether IN or BETWEEN is faster will depend on your data, but I'd expect BETWEEN to be faster normally as it can simple take a range from an index (assuming someField is indexed).
It depends on what you are doing; how wide is the range, what is the data type (I know your example uses a numeric data type but your question can also apply to a lot of different data types).
This is an instance where you want to write the query both ways; get it working and then use EXPLAIN to figure out the execution differences.
I'm sure there is a concrete answer to this but this is how I would, practically speaking, figure out the answer for my given question.
This might be of some help: http://forge.mysql.com/wiki/Top10SQLPerformanceTips
Regards,
Frank