How MySQL Optimizes OR Clauses

The Merge Index method is used to retrieve rows with several ref, ref_or_null or range scans and merge the results into one. This method is employed when the table condition is a disjunction of conditions for which ref, ref_or_null, or range could be used with different keys. The key column contains a list of used indexes. key_len contains a list of the longest key parts of the used indexes.

Example:

SELECT * FROM table WHERE key1column = 10 OR key2column = 20;
SELECT * FROM table WHERE 
  (key1column = 10 OR key2column = 20) AND nonkeycolumn=30;
SELECT * FROM t1,t2 WHERE 
  (t1.key1 IN (1,2) OR t1.key2 LIKE 'value%') AND t2.key1=t1.somefield
SELECT * FROM t1,t2 WHERE 
  t1.key1=1 AND (t2.key1=t1.somefield OR t2.key2=t1.somefield2)

This ``join'' type optimization is new in MySQL 5.0.0, and represents a significant change in behaviour with regard to indexes since the old rule was that the server is only ever able to use at most one index for each referenced table.