MySQL 5.7-8.9.4 Index Hints
2021/10/27 19:10:56
本文主要是介绍MySQL 5.7-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.
索引提示向优化器提供关于在查询处理期间如何选择索引的信息。这里描述的索引提示不同于8.9.3节“优化器提示”中描述的优化器提示。索引和优化器提示可以单独使用,也可以一起使用。
Index hints apply only to SELECT and UPDATE statements.
索引提示仅适用于SELECT和UPDATE语句。
Index hints are specified following a table name. (For the general syntax for specifying tables in a SELECT statement, see Section 13.2.9.2, “JOIN Clause”.) The syntax for referring to an individual table, including index hints, looks like this:
索引提示在表名之后指定。(在SELECT语句中指定表的一般语法,请参见13.2.9.2节“JOIN子句”。)引用单个表(包括索引提示)的语法如下:
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 EXPLAIN shows that MySQL is using the wrong index from the list of possible indexes. index_list
)
USE INDEX (index_list)提示告诉MySQL只使用其中一个指定索引来查找表中的行。另一种语法IGNORE INDEX (index_list)告诉MySQL不要使用某些特定的索引。如果EXPLAIN显示MySQL从可能的索引列表中使用了错误的索引,那么这些提示是有用的。
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
)
FORCE INDEX提示的作用类似于USE INDEX (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.
每个提示都需要索引名,而不是列名。要引用主键,请使用primary名称。要查看表的索引名称,可以使用SHOW index语句或INFORMATION_SCHEMA。统计数据表。
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.
index_name值不需要是完整的索引名称。它可以是索引名的明确前缀。如果前缀具有二义性,则会发生错误。
The syntax for index hints has the following characteristics:
索引提示的语法有以下特点:
It is syntactically valid to omit index_list
for USE INDEX
, which means “use no indexes.” Omitting index_list
for FORCE INDEX
or IGNORE INDEX
is a syntax error.
在USE INDEX中省略index_list在语法上是有效的,这意味着“不使用索引”。对于FORCE INDEX或IGNORE INDEX省略index_list是一个语法错误。
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.
可以通过在提示中添加FOR子句来指定索引提示的范围。这为查询处理的各个阶段提供了对优化器执行计划选择的更细粒度的控制。
To affect only the indexes used when MySQL decides how to find rows in the table and how to process joins, use FOR JOIN
. To influence index usage for sorting or grouping rows, use FOR ORDER BY
or FOR GROUP BY
.
为了只影响MySQL决定如何查找表中的行和如何处理连接时使用的索引,请使用FOR JOIN。若要影响索引的使用以对行进行排序或分组,请使用for ORDER BY或for 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
and FORCE INDEX
for the same table:
但是,在同一个表中混淆USE INDEX和FORCE INDEX是错误的:
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:
如果索引提示不包含FOR子句,则提示的作用域是应用于语句的所有部分。例如,这个提示:
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 source and replicas might lead to replication errors.
在MySQL 5.0中,没有FOR子句的提示作用域只应用于行检索。要使服务器在没有FOR子句时使用旧的行为,请在服务器启动时启用旧的系统变量。请注意在复制设置中启用该变量。对于基于语句的二进制日志记录,对源和副本使用不同的模式可能会导致复制错误。
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:
处理索引提示时,会按类型(USE、FORCE、IGNORE)和范围(FOR JOIN、FOR ORDER by、FOR GROUP by)在单个列表中收集索引提示。例如:
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.)如果存在,则应用INDEX。(如果不是,则使用优化器决定的索引集。) -
IGNORE INDEX
is applied over the result of the previous step. For example, the following two queries are equivalent:对上一步的结果应用IGNORE INDEX。例如,下面两个查询是等价的:
For FULLTEXT
searches, index hints work as follows:
对于FULLTEXT搜索,索引提示如下:
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.
对于自然语言模式搜索,索引提示会被静默地忽略。例如,IGNORE INDEX(i1)被忽略,没有警告,索引仍然被使用。
For boolean mode searches, index hints with FOR ORDER BY
or FOR GROUP BY
are silently ignored. Index hints with FOR JOIN
or no FOR
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 ORDER BY或For GROUP BY的索引提示将被静默地忽略。索引提示使用FOR JOIN或没有FOR修饰符。与提示应用于非fulltext搜索不同,提示用于查询执行的所有阶段(查找行和检索、分组和排序)。即使提示非fulltext索引也是如此。
这篇关于MySQL 5.7-8.9.4 Index Hints的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-11-16MySQL资料:新手入门教程
- 2024-11-16MySQL资料:新手入门教程
- 2024-11-15MySQL教程:初学者必备的MySQL数据库入门指南
- 2024-11-15MySQL教程:初学者必看的MySQL入门指南
- 2024-11-04部署MySQL集群项目实战:新手入门教程
- 2024-11-04如何部署MySQL集群资料:新手入门指南
- 2024-11-02MySQL集群项目实战:新手入门指南
- 2024-11-02初学者指南:部署MySQL集群资料
- 2024-11-01部署MySQL集群教程:新手入门指南
- 2024-11-01如何部署MySQL集群:新手入门教程