Does the order of conditions in a WHERE clause affect MySQL performance?

Posted by Greg on Stack Overflow See other posts from Stack Overflow or by Greg
Published on 2010-06-15T06:44:28Z Indexed on 2010/06/15 6:52 UTC
Read the original article Hit count: 158

Filed under:
|

Say that I have a long, expensive query, packed with conditions, searching a large number of rows. I also have one particular condition, like a company id, that will limit the number of rows that need to be searched considerably, narrowing it down to dozens from hundreds of thousands.

Does make any difference to MySQL performance whether I do this:

 SELECT * FROM clients WHERE 
       (firstname LIKE :foo OR lastname LIKE :foo OR phone LIKE :foo) AND 
       (firstname LIKE :bar OR lastname LIKE :bar OR phone LIKE :bar) AND 
       company = :ugh

or this:

 SELECT * FROM clients WHERE 
       company = :ugh AND
       (firstname LIKE :foo OR lastname LIKE :foo OR phone LIKE :foo) AND 
       (firstname LIKE :bar OR lastname LIKE :bar OR phone LIKE :bar) 

© Stack Overflow or respective owner

Related posts about php

Related posts about mysql