Optimizing the MySQL IN() Comparison Operations Which Include the Indexed Field – MySQL Diary
When a MySQL IN() clause references a field that's part of the query's index, the database evaluates the full result set for each IN value before merging and limiting results, which is expensive on large tables. Replacing the IN clause with a UNION ALL of separate queries—each with its own LIMIT—forces MySQL to limit results at each step before merging, dramatically improving performance. The post demonstrates this with a concrete example where `WHERE b=4 AND c IN (1,3,5,7,9)` becomes five separate queries joined with UNION ALL, each limited to 100 rows before the final merge and sort.