The EXPLAIN statement provides information about how MySQL executes statements:EXPLAIN语句提供有关MySQL如何执行语句的信息:
EXPLAIN works with SELECT, DELETE, INSERT, REPLACE, and UPDATE statements.EXPLAIN使用SELECT、DELETE、INSERT、REPLACE和UPDATE语句。
When 当EXPLAIN is used with an explainable statement, MySQL displays information from the optimizer about the statement execution plan. EXPLAIN与可解释语句一起使用时,MySQL显示来自优化器的关于语句执行计划的信息。That is, MySQL explains how it would process the statement, including information about how tables are joined and in which order. 也就是说,MySQL解释了它将如何处理该语句,包括有关如何联接表以及以何种顺序联接表的信息。For information about using 有关使用EXPLAIN获取执行计划信息的信息,请参阅第8.8.2节,“解释输出格式”。EXPLAIN to obtain execution plan information, see Section 8.8.2, “EXPLAIN Output Format”.
When 当EXPLAIN is used with FOR CONNECTION rather than an explainable statement, it displays the execution plan for the statement executing in the named connection. connection_idEXPLAIN与FOR CONNECTION 而不是可解释语句一起使用时,它将显示在命名连接中执行的语句的执行计划。connection_idSee Section 8.8.4, “Obtaining Execution Plan Information for a Named Connection”.请参阅第8.8.4节,“获取命名连接的执行计划信息”。
For 对于SELECT statements, EXPLAIN produces additional execution plan information that can be displayed using SHOW WARNINGS. SELECT语句,EXPLAIN生成可以使用SHOW WARNINGS显示的其他执行计划信息。See Section 8.8.3, “Extended EXPLAIN Output Format”.请参阅第8.8.3节,“扩展解释输出格式”。
EXPLAIN is useful for examining queries involving partitioned tables. EXPLAIN用于检查涉及分区表的查询。See Section 24.3.5, “Obtaining Information About Partitions”.请参阅第24.3.5节,“获取分区信息”。
The FORMAT option can be used to select the output format. FORMAT选项可用于选择输出格式。TRADITIONAL presents the output in tabular format. TRADITIONAL以表格格式显示输出。This is the default if no 如果不存在FORMAT option is present. FORMAT选项,这是默认设置。JSON format displays the information in JSON format.JSON格式以JSON格式显示信息。
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. EXPLAIN的帮助下,您可以看到应该在哪里向表添加索引,以便通过使用索引查找行,语句执行得更快。You can also use 还可以使用EXPLAIN to check whether the optimizer joins the tables in an optimal order. EXPLAIN检查优化器是否以最佳顺序连接表。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. SELECT语句中表的命名顺序相对应的联接顺序,请在语句开头使用SELECT STRAIGHT_JOIN,而不仅仅是SELECT。(See Section 13.2.10, “SELECT Statement”.) (请参阅第13.2.10节,“SELECT语句”。)However, 但是,STRAIGHT_JOIN may prevent indexes from being used because it disables semijoin transformations. STRAIGHT_JOIN可能会阻止使用索引,因为它禁用半连接转换。See Section 8.2.2.1, “Optimizing IN and EXISTS Subquery Predicates with Semijoin Transformations”.请参阅第8.2.2.1节,“使用半联接转换优化IN和EXISTS子查询谓词”。
The optimizer trace may sometimes provide information complementary to that of 优化器跟踪有时可以提供与EXPLAIN. EXPLAIN补充的信息。However, the optimizer trace format and content are subject to change between versions. 但是,优化器跟踪格式和内容在版本之间可能会发生更改。For details, see MySQL Internals: Tracing the Optimizer.有关详细信息,请参阅MySQL内部:跟踪优化器。
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. ANALYZE TABLE来更新表统计信息,例如键的基数,这可能会影响优化器所做的选择。See Section 13.7.3.1, “ANALYZE TABLE Statement”.请参阅第13.7.3.1节,“ANALYZE TABLE语句”。
EXPLAIN can also be used to obtain information about the columns in a table. EXPLAIN还可用于获取有关表中列的信息。EXPLAIN is synonymous with tbl_nameDESCRIBE and tbl_nameSHOW COLUMNS FROM . tbl_nameEXPLAIN 与tbl_nameDESCRIBE 以及tbl_nameSHOW COLUMNS FROM 同义。tbl_nameFor more information, see Section 13.8.1, “DESCRIBE Statement”, and Section 13.7.7.5, “SHOW COLUMNS Statement”.有关更多信息,请参阅第13.8.1节,“DESCRIBE语句”和第13.7.7.5节,“SHOW COLUMNS语句”。