MySQL 执行计划 (一)
在工作中,我们经常需要使用到数据库在存储数据,在查询这些数据时,我们可能会遇到查询比较慢的情况,这个时候我们一般需要对这个SQL进行分析。
在MySQL里我们一般会Explain 命令来查看该SQL语句的执行计划。
执行计划官方文档:https://dev.mysql.com/doc/refman/5.7/en/explain.html
‘
Explain 语法如下(MySQL5.7为例):’
{EXPLAIN | DESCRIBE | DESC} tbl_name [col_name | wild] {EXPLAIN | DESCRIBE | DESC} [explain_type] {explainable_stmt | FOR CONNECTION connection_id} explain_type: { EXTENDED | PARTITIONS | FORMAT = format_name } format_name: { TRADITIONAL | JSON } explainable_stmt: { SELECT statement | DELETE statement | INSERT statement | REPLACE statement | UPDATE statement }
一般我们都不会用到这么多参数,都是直接expain + SQL 语句,看输出的结果
输出结果一般有如下几列
Table 8.1 EXPLAIN Output Columns
Column JSON Name Meaning 中文描述 idselect_idThe SELECTidentifier执行计划的 标识符
select_typeNone The SELECTtype查询的类型 tabletable_nameThe table for the output row 输出结果集的表 partitionspartitionsThe matching partitions 匹配的分区 typeaccess_typeThe join type 表连接类型 possible_keyspossible_keysThe possible indexes to choose 查询时可能使用的索引 keykeyThe index actually chosen 实际使用的索引 key_lenkey_lengthThe length of the chosen key 索引字段的长度 refrefThe columns compared to the index 进行比较的索引列
rowsrowsEstimate of rows to be examined 优化器预估的记录数量
filteredfilteredPercentage of rows filtered by table condition 根据条件过滤得到的记录的百分比
ExtraNone Additional information 额外的显示选项
                    
                
                
            
        
浙公网安备 33010602011771号