Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

sql

mysql

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 it 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)  
like image 731
Greg Avatar asked Jun 15 '10 06:06

Greg


People also ask

Does order matter in SQL where clause?

In SQL Server order does not matter in the WHERE condition. SQL Server does not short circuit conditions as well it does not help in performance.

Does order of conditions matter in WHERE clause?

No, that order doesn't matter (or at least: shouldn't matter). Any decent query optimizer will look at all the parts of the WHERE clause and figure out the most efficient way to satisfy that query.

Does order matter in MySQL?

So the order of columns in a multi-column index definitely matters. One type of query may need a certain column order for the index. If you have several types of queries, you might need several indexes to help them, with columns in different orders.

Does order of columns matter in MySQL?

Yes, column order does matter.


2 Answers

No, the order should not make a large difference. When finding which rows match the condition, the condition as a whole (all of the sub-conditions combined via boolean logic) is examined for each row.

Some intelligent DB engines will attempt to guess which parts of the condition can be evaluated faster (for instance, things that don't use built-in functions) and evaluate those first, and more complex (estimatedly) elements get evaluated later. This is something determined by the DB engine though, not the SQL.

like image 38
Amber Avatar answered Oct 03 '22 17:10

Amber


Here is a demo showing the order of WHERE clause conditions can make a difference due to short-circuiting. It runs the following queries:

-- query #1 SELECT myint FROM mytable WHERE myint >= 3 OR myslowfunction('query #1', myint) = 1;  -- query #2 SELECT myint FROM mytable WHERE myslowfunction('query #2', myint) = 1 OR myint >= 3; 

The only difference between these is the order of operands in the OR condition.

myslowfunction deliberately sleeps for a second and has the side effect of adding an entry to a log table each time it is run. Here are the results of what is logged when running the two queries:

myslowfunction called for query #1 with value 1 myslowfunction called for query #1 with value 2 myslowfunction called for query #2 with value 1 myslowfunction called for query #2 with value 2 myslowfunction called for query #2 with value 3 myslowfunction called for query #2 with value 4 

The above shows that a slow function is executed more times when it appears on the left side of an OR condition when the other operand isn't always true.

So IMO the answer to the question:

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

is "Sometimes it can do."

like image 156
Steve Chambers Avatar answered Oct 03 '22 16:10

Steve Chambers