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

南楼画角 提交于 2020-01-13 08:37:06

问题


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.


回答1:


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



来源:https://stackoverflow.com/questions/10747047/is-mysql-logic-evaluation-lazy-short-circuiting-in-join-clause

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!