Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is MySQL logic evaluation lazy/short-circuiting in JOIN clause?

Take the following expression: FALSE AND (expression)

Will MySQL evaluate the expression or just move on as soon as it sees FALSE?

Some background context-- I wanted to speed up a query by doing:

JOIN... ON (indexed_column1=indexed_column2 AND non_indexed_column_a=non_indexed_column_b)

For background on why I'm doing this query see this answer

If it's going to always evaluate non_indexed_column_a=non_indexed_column_b then no time is saved with that.

like image 903
bgcode Avatar asked May 25 '12 00:05

bgcode


People also ask

Does SQL use short-circuit evaluation?

SQL Server does not short-circuit expressions.

Does MySQL short-circuit?

MySQL OR short-circuit evaluationMySQL uses short-circuit evaluation for the OR operator. In other words, MySQL stops evaluating the remaining parts of the statement when it can determine the result. See the following example. Because the expression 1 = 1 always returns true, MySQL does not evaluate the 1 / 0.


1 Answers

The MySQL query optimizer uses indexes whenever possible and to use the most restrictive index in order to eliminate as many rows as possible.

So in case of your query it will always filter the records based on first indexes columns and then filter the records from non-index columns.

Also before query execution, MySQL eliminates the code which is always going to be false (Dead Code) .

For more details see: http://www.informit.com/articles/article.aspx?p=377652&seqNum=2

like image 109
Omesh Avatar answered Oct 07 '22 01:10

Omesh