Rechercher dans le manuel MySQL
13.2.11.11 Optimizing Subqueries
Development is ongoing, so no optimization tip is reliable for the long term. The following list provides some interesting tricks that you might want to play with. See also Section 8.2.2, “Optimizing Subqueries, Derived Tables, View References, and Common Table Expressions”.
Use subquery clauses that affect the number or order of the rows in the subquery. For example:
Replace a join with a subquery. For example, try this:
Instead of this:
Some subqueries can be transformed to joins for compatibility with older versions of MySQL that do not support subqueries. However, in some cases, converting a subquery to a join may improve performance. See Section 13.2.11.12, “Rewriting Subqueries as Joins”.
Move clauses from outside to inside the subquery. For example, use this query:
Instead of this query:
For another example, use this query:
Instead of this query:
Use a row subquery instead of a correlated subquery. For example, use this query:
Instead of this query:
Use
NOT (a = ANY (...))
rather thana <> ALL (...)
.Use
x = ANY (
rather thantable containing (1,2)
)x=1 OR x=2
.Use
= ANY
rather thanEXISTS
.For uncorrelated subqueries that always return one row,
IN
is always slower than=
. For example, use this query:Instead of this query:
These tricks might cause programs to go faster or slower. Using
MySQL facilities like the
BENCHMARK()
function, you can get
an idea about what helps in your own situation. See
Section 12.15, “Information Functions”.
Some optimizations that MySQL itself makes are:
MySQL executes uncorrelated subqueries only once. Use
EXPLAIN
to make sure that a given subquery really is uncorrelated.MySQL rewrites
IN
,ALL
,ANY
, andSOME
subqueries in an attempt to take advantage of the possibility that the select-list columns in the subquery are indexed.MySQL replaces subqueries of the following form with an index-lookup function, which
EXPLAIN
describes as a special join type (unique_subquery
orindex_subquery
):MySQL enhances expressions of the following form with an expression involving
MIN()
orMAX()
, unlessNULL
values or empty sets are involved:For example, this
WHERE
clause:might be treated by the optimizer like this:
Document created the 26/06/2006, last modified the 26/10/2018
Source of the printed document:https://www.gaudry.be/en/mysql-rf-optimizing-subqueries.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.