8.4 中文镜像

MySQL 8.4 参考手册 · 中文镜像

正文为中文,英文原文默认隐藏。点击任意中文段落即可展开 / 收起对应英文原文。

打开官方英文原文 ↗

译文由机器翻译生成,仅供理解参考;有疑问请以官方英文原文为准。

MySQL 8.4 参考手册 / 10 优化 / 10.8 理解查询执行计划 / 10.8.1 使用 EXPLAIN 优化查询
中文点击中文段落可显示 / 隐藏英文原文本页译文覆盖 100%

10.8.1 使用 EXPLAIN 优化查询10.8.1 Optimizing Queries with EXPLAIN

EXPLAIN 语句提供有关 MySQL 如何执行语句的信息:The EXPLAIN statement provides information about how MySQL executes statements:

借助 EXPLAIN,您可以查看应在表中添加索引的位置,从而通过使用索引查找行使语句执行得更快。您还可以使用 EXPLAIN 检查优化器是否以最佳顺序连接表。要向优化器提供提示,使其使用与 SELECT 语句中表名顺序对应的连接顺序,请以 SELECT STRAIGHT_JOIN 开始语句,而不仅仅是 SELECT。(参见 第 15.2.13 节,“SELECT 语句”。)然而,STRAIGHT_JOIN 可能会阻止索引的使用,因为它禁用了半连接转换。请参阅 使用半连接转换优化 IN 和 EXISTS 子查询谓词。 With the help of EXPLAIN, you can see where you should add indexes to tables so that the statement executes faster by using indexes to find rows. You can also use EXPLAIN to check whether the optimizer joins the tables in an optimal order. To give a hint to the optimizer to use a join order corresponding to the order in which the tables are named in a SELECT statement, begin the statement with SELECT STRAIGHT_JOIN rather than just SELECT. (See Section 15.2.13, “SELECT Statement”.) However, STRAIGHT_JOIN may prevent indexes from being used because it disables semijoin transformations. See Optimizing IN and EXISTS Subquery Predicates with Semijoin Transformations.

优化器跟踪有时可能提供与 EXPLAIN 补充的信息。然而,优化器跟踪的格式和内容可能会随着版本的不同而发生变化。有关详细信息,请参见 第 10.15 节,“跟踪优化器”。The optimizer trace may sometimes provide information complementary to that of EXPLAIN. However, the optimizer trace format and content are subject to change between versions. For details, see Section 10.15, “Tracing the Optimizer”.

如果您遇到索引未被使用的问题,而您认为它们应该被使用,请运行 ANALYZE TABLE 来更新表统计信息,例如键的基数,这些信息可能会影响优化器的选择。请参见 第 15.7.3.1 节,“ANALYZE TABLE 语句”。If you have a problem with indexes not being used when you believe that they should be, run ANALYZE TABLE to update table statistics, such as cardinality of keys, that can affect the choices the optimizer makes. See Section 15.7.3.1, “ANALYZE TABLE Statement”.

Note

EXPLAIN 也可以用来获取表中列的信息。EXPLAIN tbl_name 等同于 DESCRIBE tbl_name 和 SHOW COLUMNS FROM tbl_name。更多信息,请参见 第 15.8.1 节,“DESCRIBE 语句” 和 第 15.7.7.6 节,“SHOW COLUMNS 语句”。EXPLAIN can also be used to obtain information about the columns in a table. EXPLAIN tbl_name is synonymous with DESCRIBE tbl_name and SHOW COLUMNS FROM tbl_name. For more information, see Section 15.8.1, “DESCRIBE Statement”, and Section 15.7.7.6, “SHOW COLUMNS Statement”.