Rechercher dans le manuel MySQL
8.9.4 Index Hints
Index hints give the optimizer information about how to choose indexes during query processing. Index hints, described here, differ from optimizer hints, described in Section 8.9.3, “Optimizer Hints”. Index and optimizer hints may be used separately or together.
Index hints apply only to SELECT
statements. (They are accepted by the parser for
UPDATE
statements but are ignored
and have no effect.)
Index hints are specified following a table name. (For the
general syntax for specifying tables in a
SELECT
statement, see
Section 13.2.10.2, “JOIN Syntax”.) The syntax for referring to an
individual table, including index hints, looks like this:
- index_hint_list:
- index_hint [index_hint] ...
- index_hint:
- index_list:
- index_name [, index_name] ...
The USE INDEX
(
hint tells
MySQL to use only one of the named indexes to find rows in the
table. The alternative syntax index_list
)IGNORE INDEX
(
tells MySQL to
not use some particular index or indexes. These hints are useful
if index_list
)EXPLAIN
shows that MySQL is
using the wrong index from the list of possible indexes.
The FORCE INDEX
hint acts like USE
INDEX (
, with
the addition that a table scan is assumed to be
very expensive. In other words, a table
scan is used only if there is no way to use one of the named
indexes to find rows in the table.
index_list
)
Each hint requires index names, not column names. To refer to a
primary key, use the name PRIMARY
. To see the
index names for a table, use the SHOW
INDEX
statement or the
INFORMATION_SCHEMA.STATISTICS
table.
An index_name
value need not be a
full index name. It can be an unambiguous prefix of an index
name. If a prefix is ambiguous, an error occurs.
Examples:
The syntax for index hints has the following characteristics:
It is syntactically valid to omit
index_list
forUSE INDEX
, which means “use no indexes.” Omittingindex_list
forFORCE INDEX
orIGNORE INDEX
is a syntax error.You can specify the scope of an index hint by adding a
FOR
clause to the hint. This provides more fine-grained control over optimizer selection of an execution plan for various phases of query processing. To affect only the indexes used when MySQL decides how to find rows in the table and how to process joins, useFOR JOIN
. To influence index usage for sorting or grouping rows, useFOR ORDER BY
orFOR GROUP BY
.You can specify multiple index hints:
It is not an error to name the same index in several hints (even within the same hint):
However, it is an error to mix
USE INDEX
andFORCE INDEX
for the same table:
If an index hint includes no FOR
clause, the
scope of the hint is to apply to all parts of the statement. For
example, this hint:
is equivalent to this combination of hints:
In MySQL 5.0, hint scope with no FOR
clause
was to apply only to row retrieval. To cause the server to use
this older behavior when no FOR
clause is
present, enable the old
system
variable at server startup. Take care about enabling this
variable in a replication setup. With statement-based binary
logging, having different modes for the master and slaves might
lead to replication errors.
When index hints are processed, they are collected in a single
list by type (USE
, FORCE
,
IGNORE
) and by scope (FOR
JOIN
, FOR ORDER BY
, FOR
GROUP BY
). For example:
is equivalent to:
The index hints then are applied for each scope in the following order:
{USE|FORCE} INDEX
is applied if present. (If not, the optimizer-determined set of indexes is used.)IGNORE INDEX
is applied over the result of the previous step. For example, the following two queries are equivalent:
For FULLTEXT
searches, index hints work as
follows:
For natural language mode searches, index hints are silently ignored. For example,
IGNORE INDEX(i1)
is ignored with no warning and the index is still used.For boolean mode searches, index hints with
FOR ORDER BY
orFOR GROUP BY
are silently ignored. Index hints withFOR JOIN
or noFOR
modifier are honored. In contrast to how hints apply for non-FULLTEXT
searches, the hint is used for all phases of query execution (finding rows and retrieval, grouping, and ordering). This is true even if the hint is given for a non-FULLTEXT
index.For example, the following two queries are equivalent:
Nederlandse vertaling
U hebt gevraagd om deze site in het Nederlands te bezoeken. Voor nu wordt alleen de interface vertaald, maar nog niet alle inhoud.Als je me wilt helpen met vertalingen, is je bijdrage welkom. Het enige dat u hoeft te doen, is u op de site registreren en mij een bericht sturen waarin u wordt gevraagd om u toe te voegen aan de groep vertalers, zodat u de gewenste pagina's kunt vertalen. Een link onderaan elke vertaalde pagina geeft aan dat u de vertaler bent en heeft een link naar uw profiel.
Bij voorbaat dank.
Document heeft de 26/06/2006 gemaakt, de laatste keer de 26/10/2018 gewijzigd
Bron van het afgedrukte document:https://www.gaudry.be/nl/mysql-rf-index-hints.html
De infobrol is een persoonlijke site waarvan de inhoud uitsluitend mijn verantwoordelijkheid is. De tekst is beschikbaar onder CreativeCommons-licentie (BY-NC-SA). Meer info op de gebruiksvoorwaarden en de auteur.
Referenties
Deze verwijzingen en links verwijzen naar documenten die geraadpleegd zijn tijdens het schrijven van deze pagina, of die aanvullende informatie kunnen geven, maar de auteurs van deze bronnen kunnen niet verantwoordelijk worden gehouden voor de inhoud van deze pagina.
De auteur Deze site is als enige verantwoordelijk voor de manier waarop de verschillende concepten, en de vrijheden die met de referentiewerken worden genomen, hier worden gepresenteerd. Vergeet niet dat u meerdere broninformatie moet doorgeven om het risico op fouten te verkleinen.