Rechercher dans le manuel MySQL
8.2.2.2 Optimizing Subqueries with Materialization
The optimizer uses materialization to enable more efficient subquery processing. Materialization speeds up query execution by generating a subquery result as a temporary table, normally in memory. The first time MySQL needs the subquery result, it materializes that result into a temporary table. Any subsequent time the result is needed, MySQL refers again to the temporary table. The optimizer may index the table with a hash index to make lookups fast and inexpensive. The index contains unique values to eliminate duplicates and make the table smaller.
Subquery materialization uses an in-memory temporary table when possible, falling back to on-disk storage if the table becomes too large. See Section 8.4.4, “Internal Temporary Table Use in MySQL”.
If materialization is not used, the optimizer sometimes
rewrites a noncorrelated subquery as a correlated subquery.
For example, the following IN
subquery is
noncorrelated (where_condition
involves only columns from t2
and not
t1
):
The optimizer might rewrite this as an
EXISTS
correlated subquery:
Subquery materialization using a temporary table avoids such rewrites and makes it possible to execute the subquery only once rather than once per row of the outer query.
For subquery materialization to be used in MySQL, the
optimizer_switch
system
variable materialization
flag must be
enabled. (See Section 8.9.2, “Switchable Optimizations”.)
With the materialization
flag enabled,
materialization applies to subquery predicates that appear
anywhere (in the select list, WHERE
,
ON
, GROUP BY
,
HAVING
, or ORDER BY
),
for predicates that fall into any of these use cases:
The predicate has this form, when no outer expression
oe_i
or inner expressionie_i
is nullable.N
is 1 or larger.The predicate has this form, when there is a single outer expression
oe
and inner expressionie
. The expressions can be nullable.The predicate is
IN
orNOT IN
and a result ofUNKNOWN
(NULL
) has the same meaning as a result ofFALSE
.
The following examples illustrate how the requirement for
equivalence of UNKNOWN
and
FALSE
predicate evaluation affects whether
subquery materialization can be used. Assume that
where_condition
involves columns
only from t2
and not t1
so that the subquery is noncorrelated.
This query is subject to materialization:
Here, it does not matter whether the IN
predicate returns UNKNOWN
or
FALSE
. Either way, the row from
t1
is not included in the query result.
An example where subquery materialization is not used is the
following query, where t2.b
is a nullable
column:
The following restrictions apply to the use of subquery materialization:
The types of the inner and outer expressions must match. For example, the optimizer might be able to use materialization if both expressions are integer or both are decimal, but cannot if one expression is integer and the other is decimal.
The inner expression cannot be a
BLOB
.
Use of EXPLAIN
with a query
provides some indication of whether the optimizer uses
subquery materialization:
Compared to query execution that does not use materialization,
select_type
may change fromDEPENDENT SUBQUERY
toSUBQUERY
. This indicates that, for a subquery that would be executed once per outer row, materialization enables the subquery to be executed just once.For extended
EXPLAIN
output, the text displayed by a followingSHOW WARNINGS
includesmaterialize
andmaterialized-subquery
.
Document created the 26/06/2006, last modified the 26/10/2018
Source of the printed document:https://www.gaudry.be/en/mysql-rf-subquery-materialization.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.