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:
Traduction non disponible
Le manuel MySQL n'est pas encore traduit en français sur l'infobrol. Seule la version anglaise est disponible pour l'instant.
Document créé le 26/06/2006, dernière modification le 26/10/2018
Source du document imprimé : https://www.gaudry.be/mysql-rf-optimizing-subqueries.html
L'infobrol est un site personnel dont le contenu n'engage que moi. Le texte est mis à disposition sous licence CreativeCommons(BY-NC-SA). Plus d'info sur les conditions d'utilisation et sur l'auteur.
Références
Ces références et liens indiquent des documents consultés lors de la rédaction de cette page, ou qui peuvent apporter un complément d'information, mais les auteurs de ces sources ne peuvent être tenus responsables du contenu de cette page.
L'auteur de ce site est seul responsable de la manière dont sont présentés ici les différents concepts, et des libertés qui sont prises avec les ouvrages de référence. N'oubliez pas que vous devez croiser les informations de sources multiples afin de diminuer les risques d'erreurs.