Mysql repetition of column in Criteria

innodbMySQL

What happens when I repeat the same column in the criteria part but with different values . For example

col1 ='val1' or col1 = 'val2' or col1 = 'val3' .

Assume col1 is not indexed. Will mysql perform entire table scan for each comparison? What will be its behaviour?

Best Answer

If the column is not indexed then it does not matter how many times (or with how many values) it is used, a table scan seems inevitable (unless there is some other indexed condition).

But there is no need to scan multiple times. MySQL optimizer tries to find the most effective plan, if nothing better is possible, it iterates over the table and checks each row for all conditions at the same time.

If an index is available (and statistics say it is effective), then ORs (on the same column) and IN are treated the same way - a "range" scan on the index is executed, fetching only matching rows.