Rechercher dans le manuel MySQL
8.2.1.14 IS NULL Optimization
MySQL can perform the same optimization on
col_name
IS
NULL
that it can use for
col_name
=
constant_value
. For example, MySQL
can use indexes and ranges to search for
NULL
with IS
NULL
.
Examples:
If a WHERE
clause includes a
col_name
IS
NULL
condition for a column that is declared as
NOT NULL
, that expression is optimized
away. This optimization does not occur in cases when the
column might produce NULL
anyway (for
example, if it comes from a table on the right side of a
LEFT JOIN
).
MySQL can also optimize the combination
, a form
that is common in resolved subqueries.
col_name
=
expr
OR
col_name
IS NULLEXPLAIN
shows
ref_or_null
when this
optimization is used.
This optimization can handle one IS
NULL
for any key part.
Some examples of queries that are optimized, assuming that
there is an index on columns a
and
b
of table t2
:
ref_or_null
works by first
doing a read on the reference key, and then a separate search
for rows with a NULL
key value.
The optimization can handle only one IS
NULL
level. In the following query, MySQL uses key
lookups only on the expression (t1.a=t2.a AND t2.a IS
NULL)
and is not able to use the key part on
b
:
Document created the 26/06/2006, last modified the 26/10/2018
Source of the printed document:https://www.gaudry.be/en/mysql-rf-is-null-optimization.html
The infobrol is a personal site whose content is my sole responsibility. The text is available under CreativeCommons license (BY-NC-SA). More info on the terms of use and the author.
References
These references and links indicate documents consulted during the writing of this page, or which may provide additional information, but the authors of these sources can not be held responsible for the content of this page.
The author This site is solely responsible for the way in which the various concepts, and the freedoms that are taken with the reference works, are presented here. Remember that you must cross multiple source information to reduce the risk of errors.