Mysql – Using WHERE col IN with many data OR select all rows

MySQL

I have a query where I select many data at once like

WHERE id IN ( 1,2,3,4,5,6,7,8 ).

Imagine that we might have more than 10.000 Numbers inside the IN query. It's better to do the select as I'm doing it using IN() or just select all data and do the parse with PHP Code for the IDs I want?

Best Answer

You mentioned that the IN statement will include almost all possible values (90-95% based on your example numbers). Joe W is correct that depending on your setup it could go either way and it's impossible to say for sure without testing which of those two would be faster.

However, you will eventually run in to a diminishing return on a query like that. While one or the other will be faster than the other, neither will be fast. (by default MySQL will allow you up to max_allowed_packet for # of values, which is a lot). A faster option would be to use where NOT IN and just list the 5-10% of values you didn't want.