深入剖析索引

目录

示例表结构

示例索引

联合索引首字段范围查找失效

强制索引

in和or在表数据量比较大的情况会走索引,在表记录不多的情况下会选择全表扫描

​编辑

LIKE 'XX%'走索引(数据量大时)

索引下推

认识trace工具

第一阶段:SQL准备阶段,格式化sql

第二阶段:SQL优化阶段(分析具体用哪个索引,以及分析使用的原因)

条件处理

表依赖详情

预估表的访问成本

确定执行计划(索引使用情况)

第三阶段:SQL执行阶段    (此处判断filesort)

完整分析代码

FileSort两种排序方式 

单路排序

双路排序

选择依据

结束语


示例表结构

mysql-version : [8.0.27]

CREATE TABLE `test_index` (
  `id` int NOT NULL,
  `name` varchar(20) DEFAULT NULL,
  `score` int DEFAULT NULL,
  `addr` varchar(30) DEFAULT NULL,
  `ctime` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  KEY `union_name_score_addr` (`name`,`score`,`addr`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3;

示例索引

  • 联合索引首字段范围查找失效


正常情况下,范围之后的索引失效,但是如果范围查询的是首个索引字段,则所有索引均失效
 

  • 强制索引

我们有时可以强制某个查询用索引。用法如下:force index;虽然查询由上面的全表扫描变成了范围查询,而且扫描的行数也减少了,但是最终cost的时间未必最少。其内部有成本分析。

explain select * from test_index t force index (union_name_score_addr) where t.name > '北京' and Score=1 and addr='010北京' 


 

  • in和or在表数据量比较大的情况会走索引,在表记录不多的情况下会选择全表扫描

    explain select * from test_index t where t.name in( '北京' )and Score=1 and addr='010北京' -- ken_len=161
    explain select * from test_index t where t.name in( '北京' ,'上海')and Score=1 and addr='010北京' -- ken_len=161
    
    explain select * from test_index t where t.name ='北京' or name='上海' and Score=1 and addr='010北京' -- ken_len=161
    
    


     我们复制一份表,表记录仅保留10行记录,看效果

  • LIKE 'XX%'走索引(数据量大时)

当联合索引的首字段为like时,走索引

索引下推

首先该理论仅限于二级索引。(可能由于InnoDb引擎库中聚簇索引包含了所有数据,所以效率不高)

索引下推:即在联合索引中根据第一个索引值筛选出结果后,同时在接下来的索引中进行条件判断,同时满足条件时进行回表,从而减少回表的次数。

下图为5.6版本前未索引下推以及索引下推的差异。

认识trace工具

我们正常一个sql语句如何走的索引,为什么同样的语句不同的数据量,有的走索引有的不走?

带着这个疑惑我们来认识一下trace工具,最后附上完成分析代码。

首先看一下如何使用:

set session optimizer_trace="enabled=on",end_markers_in_json=on;  -- 开启trace。切记该开关仅临时使用,测试完记得关闭,影响性能。
select * from test_index where name > '北' order by addr; -- 执行语句

SELECT * FROM information_schema.OPTIMIZER_TRACE; -- 分析数据

第一阶段:SQL准备阶段,格式化sql

    {
      "join_preparation": { -- 第一阶段:SQL准备阶段,格式化sql
        "select#": 1,
        "steps": [
          {
            "expanded_query": "/* select#1 */ select `test_index`.`id` AS `id`,`test_index`.`name` AS `name`,`test_index`.`score` AS `score`,`test_index`.`addr` AS `addr`,`test_index`.`ctime` AS `ctime` from `test_index` where (`test_index`.`name` > '北') order by `test_index`.`addr`"
          }
        ] /* steps */
      } /* join_preparation */
    }

第二阶段:SQL优化阶段(分析具体用哪个索引,以及分析使用的原因)

条件处理

          {
					/* begin-condition_processing */
            "condition_processing": { -- --条件处理
              "condition": "WHERE",
              "original_condition": "(`test_index`.`name` > '北')",
              "steps": [
                {
                  "transformation": "equality_propagation",
                  "resulting_condition": "(`test_index`.`name` > '北')"
                },
                {
                  "transformation": "constant_propagation",
                  "resulting_condition": "(`test_index`.`name` > '北')"
                },
                {
                  "transformation": "trivial_condition_removal",
                  "resulting_condition": "(`test_index`.`name` > '北')"
                }
              ] /* steps */
            } /* end-condition_processing */
          }

表依赖详情

	/* begin-table_dependencies */
          {
            "table_dependencies": [ -- 表依赖详情
              {
                "table": "`test_index`",
                "row_may_be_null": false,
                "map_bit": 0,
                "depends_on_map_bits": [
                ] /* depends_on_map_bits */
              }
            ] /* end-table_dependencies */
          }

预估表的访问成本

					/* begin-rows_estimation */
          {
            "rows_estimation": [  -- 预估表的访问成本
              {
                "table": "`test_index`",
                "range_analysis": {
                  "table_scan": { -- 全表扫描情况
                    "rows": 106, --  扫描行数
                    "cost": 12.95 -- 扫描成本
                  } /* table_scan */,
									
									/* begin-potential_range_indexes */
                  "potential_range_indexes": [ -- 查询可能使用的索引
                    {
                      "index": "PRIMARY",
                      "usable": false,
                      "cause": "not_applicable"
                    },
                    {
                      "index": "union_name_score_addr", -- 辅助联合索引
                      "usable": true,
                      "key_parts": [
                        "name",
                        "score",
                        "addr",
                        "id"
                      ] /* key_parts */	
                    }
                  ] /* end-potential_range_indexes */,
									
									
									
                  "setup_range_conditions": [
                  ] /* setup_range_conditions */,
                  "group_index_range": {
                    "chosen": false,
                    "cause": "not_group_by_or_distinct"
                  } /* group_index_range */,
                  "skip_scan_range": {
                    "potential_skip_scan_indexes": [
                      {
                        "index": "union_name_score_addr",
                        "usable": false,
                        "cause": "query_references_nonkey_column"
                      }
                    ] /* potential_skip_scan_indexes */
                  } /* skip_scan_range */,
									
									/* begin-analyzing_range_alternatives */
                  "analyzing_range_alternatives": { -- 分析各个索引使用成本
                    "range_scan_alternatives": [
                      {
                        "index": "union_name_score_addr",
                        "ranges": [
                          "北 < name" -- 索引使用范围
                        ] /* ranges */,
                        "index_dives_for_eq_ranges": true,
                        "rowid_ordered": false, -- 使用该索引获取的记录是否按照主键排序
                        "using_mrr": false,
                        "index_only": false, -- 是否使用覆盖索引
                        "in_memory": 1,
                        "rows": 93, -- 索引扫描行数
                        "cost": 32.81, -- 索引使用成本
                        "chosen": false, -- 是否选择该索引
                        "cause": "cost"
                      }
                    ] /* range_scan_alternatives */,
                    "analyzing_roworder_intersect": {
                      "usable": false,
                      "cause": "too_few_roworder_scans"
                    } /* analyzing_roworder_intersect */
                  } /* end-analyzing_range_alternatives */
                } /* range_analysis */
              }
            ] /* end-rows_estimation */
          }

确定执行计划(索引使用情况)

{
            "considered_execution_plans": [
              {
                "plan_prefix": [
                ] /* plan_prefix */,
                "table": "`test_index`",
                "best_access_path": { -- 最优访问路径
                  "considered_access_paths": [ -- 最终选择的访问路径
                    {
                      "rows_to_scan": 106,
                      "access_type": "scan", -- 访问类型:为scan,全表扫描
                      "resulting_rows": 106,
                      "cost": 10.85,
                      "chosen": true, -- 确定选择
                      "use_tmp_table": true
                    }
                  ] /* considered_access_paths */
                } /* best_access_path */,
                "condition_filtering_pct": 100,
                "rows_for_plan": 106,
                "cost_for_plan": 10.85,
                "sort_cost": 106,
                "new_cost_for_plan": 116.85,
                "chosen": true
              }
            ] /* considered_execution_plans */
          }

第三阶段:SQL执行阶段    (此处判断filesort)

    {
      "join_execution": { -- 第三阶段:SQL执行阶段
        "select#": 1,
        "steps": [
          {
            "sorting_table": "test_index",
            "filesort_information": [
              {
                "direction": "asc",
                "expression": "`test_index`.`addr`"
              }
            ] /* filesort_information */,
            "filesort_priority_queue_optimization": {
              "usable": false,
              "cause": "not applicable (no LIMIT)"
            } /* filesort_priority_queue_optimization */,
            "filesort_execution": [
            ] /* filesort_execution */,
            "filesort_summary": {
              "memory_available": 262144,
              "key_size": 61,
              "row_size": 230,
              "max_rows_per_buffer": 106,
              "num_rows_estimate": 106,
              "num_rows_found": 93,
              "num_initial_chunks_spilled_to_disk": 0,
              "peak_memory_used": 33792,
              "sort_algorithm": "std::sort",
              "sort_mode": "<fixed_sort_key, packed_additional_fields>"
            } /* filesort_summary */
          }
        ] /* steps */
      } /* join_execution */
    }

完整分析代码

{
  "steps": [
    {
      "join_preparation": { -- 第一阶段:SQL准备阶段,格式化sql
        "select#": 1,
        "steps": [
          {
            "expanded_query": "/* select#1 */ select `test_index`.`id` AS `id`,`test_index`.`name` AS `name`,`test_index`.`score` AS `score`,`test_index`.`addr` AS `addr`,`test_index`.`ctime` AS `ctime` from `test_index` where (`test_index`.`name` > '北') order by `test_index`.`addr`"
          }
        ] /* steps */
      } /* join_preparation */
    },
    {
      "join_optimization": { -- 第二阶段:SQL优化阶段(分析具体用哪个索引,以及分析使用的原因)
        "select#": 1,
        "steps": [
          {
					/* begin-condition_processing */
            "condition_processing": { -- --条件处理
              "condition": "WHERE",
              "original_condition": "(`test_index`.`name` > '北')",
              "steps": [
                {
                  "transformation": "equality_propagation",
                  "resulting_condition": "(`test_index`.`name` > '北')"
                },
                {
                  "transformation": "constant_propagation",
                  "resulting_condition": "(`test_index`.`name` > '北')"
                },
                {
                  "transformation": "trivial_condition_removal",
                  "resulting_condition": "(`test_index`.`name` > '北')"
                }
              ] /* steps */
            } /* end-condition_processing */
          },
					
					
          {
            "substitute_generated_columns": {
            } /* substitute_generated_columns */
          },
					
					/* begin-table_dependencies */
          {
            "table_dependencies": [ -- 表依赖详情
              {
                "table": "`test_index`",
                "row_may_be_null": false,
                "map_bit": 0,
                "depends_on_map_bits": [
                ] /* depends_on_map_bits */
              }
            ] /* end-table_dependencies */
          },
					
					
					
          {
            "ref_optimizer_key_uses": [
            ] /* ref_optimizer_key_uses */
          },
					
					/* begin-rows_estimation */
          {
            "rows_estimation": [  -- 预估表的访问成本
              {
                "table": "`test_index`",
                "range_analysis": {
                  "table_scan": { -- 全表扫描情况
                    "rows": 106, --  扫描行数
                    "cost": 12.95 -- 扫描成本
                  } /* table_scan */,
									
									/* begin-potential_range_indexes */
                  "potential_range_indexes": [ -- 查询可能使用的索引
                    {
                      "index": "PRIMARY",
                      "usable": false,
                      "cause": "not_applicable"
                    },
                    {
                      "index": "union_name_score_addr", -- 辅助联合索引
                      "usable": true,
                      "key_parts": [
                        "name",
                        "score",
                        "addr",
                        "id"
                      ] /* key_parts */	
                    }
                  ] /* end-potential_range_indexes */,
									
									
									
                  "setup_range_conditions": [
                  ] /* setup_range_conditions */,
                  "group_index_range": {
                    "chosen": false,
                    "cause": "not_group_by_or_distinct"
                  } /* group_index_range */,
                  "skip_scan_range": {
                    "potential_skip_scan_indexes": [
                      {
                        "index": "union_name_score_addr",
                        "usable": false,
                        "cause": "query_references_nonkey_column"
                      }
                    ] /* potential_skip_scan_indexes */
                  } /* skip_scan_range */,
									
									/* begin-analyzing_range_alternatives */
                  "analyzing_range_alternatives": { -- 分析各个索引使用成本
                    "range_scan_alternatives": [
                      {
                        "index": "union_name_score_addr",
                        "ranges": [
                          "北 < name" -- 索引使用范围
                        ] /* ranges */,
                        "index_dives_for_eq_ranges": true,
                        "rowid_ordered": false, -- 使用该索引获取的记录是否按照主键排序
                        "using_mrr": false,
                        "index_only": false, -- 是否使用覆盖索引
                        "in_memory": 1,
                        "rows": 93, -- 索引扫描行数
                        "cost": 32.81, -- 索引使用成本
                        "chosen": false, -- 是否选择该索引
                        "cause": "cost"
                      }
                    ] /* range_scan_alternatives */,
                    "analyzing_roworder_intersect": {
                      "usable": false,
                      "cause": "too_few_roworder_scans"
                    } /* analyzing_roworder_intersect */
                  } /* end-analyzing_range_alternatives */
                } /* range_analysis */
              }
            ] /* end-rows_estimation */
          },
					
					
          {
            "considered_execution_plans": [
              {
                "plan_prefix": [
                ] /* plan_prefix */,
                "table": "`test_index`",
                "best_access_path": { -- 最优访问路径
                  "considered_access_paths": [ -- 最终选择的访问路径
                    {
                      "rows_to_scan": 106,
                      "access_type": "scan", -- 访问类型:为scan,全表扫描
                      "resulting_rows": 106,
                      "cost": 10.85,
                      "chosen": true, -- 确定选择
                      "use_tmp_table": true
                    }
                  ] /* considered_access_paths */
                } /* best_access_path */,
                "condition_filtering_pct": 100,
                "rows_for_plan": 106,
                "cost_for_plan": 10.85,
                "sort_cost": 106,
                "new_cost_for_plan": 116.85,
                "chosen": true
              }
            ] /* considered_execution_plans */
          },
          {
            "attaching_conditions_to_tables": {
              "original_condition": "(`test_index`.`name` > '北')",
              "attached_conditions_computation": [
              ] /* attached_conditions_computation */,
              "attached_conditions_summary": [
                {
                  "table": "`test_index`",
                  "attached": "(`test_index`.`name` > '北')"
                }
              ] /* attached_conditions_summary */
            } /* attaching_conditions_to_tables */
          },
          {
            "optimizing_distinct_group_by_order_by": {
              "simplifying_order_by": {
                "original_clause": "`test_index`.`addr`",
                "items": [
                  {
                    "item": "`test_index`.`addr`"
                  }
                ] /* items */,
                "resulting_clause_is_simple": true,
                "resulting_clause": "`test_index`.`addr`"
              } /* simplifying_order_by */
            } /* optimizing_distinct_group_by_order_by */
          },
          {
            "reconsidering_access_paths_for_index_ordering": {
              "clause": "ORDER BY",
              "steps": [
              ] /* steps */,
              "index_order_summary": {
                "table": "`test_index`",
                "index_provides_order": false,
                "order_direction": "undefined",
                "index": "unknown",
                "plan_changed": false
              } /* index_order_summary */
            } /* reconsidering_access_paths_for_index_ordering */
          },
          {
            "finalizing_table_conditions": [
              {
                "table": "`test_index`",
                "original_table_condition": "(`test_index`.`name` > '北')",
                "final_table_condition   ": "(`test_index`.`name` > '北')"
              }
            ] /* finalizing_table_conditions */
          },
          {
            "refine_plan": [
              {
                "table": "`test_index`"
              }
            ] /* refine_plan */
          },
          {
            "considering_tmp_tables": [
              {
                "adding_sort_to_table": "test_index"
              } /* filesort */
            ] /* considering_tmp_tables */
          }
        ] /* steps */
      } /* join_optimization */
    },
    {
      "join_execution": { -- 第三阶段:SQL执行阶段
        "select#": 1,
        "steps": [
          {
            "sorting_table": "test_index",
            "filesort_information": [
              {
                "direction": "asc",
                "expression": "`test_index`.`addr`"
              }
            ] /* filesort_information */,
            "filesort_priority_queue_optimization": {
              "usable": false,
              "cause": "not applicable (no LIMIT)"
            } /* filesort_priority_queue_optimization */,
            "filesort_execution": [
            ] /* filesort_execution */,
            "filesort_summary": {
              "memory_available": 262144,
              "key_size": 61,
              "row_size": 230,
              "max_rows_per_buffer": 106,
              "num_rows_estimate": 106,
              "num_rows_found": 93,
              "num_initial_chunks_spilled_to_disk": 0,
              "peak_memory_used": 33792,
              "sort_algorithm": "std::sort",
              "sort_mode": "<fixed_sort_key, packed_additional_fields>"
            } /* filesort_summary */
          }
        ] /* steps */
      } /* join_execution */
    }
  ] /* steps */
}

FileSort两种排序方式 

 select * from test_index where name > '北' order by addr;

单路排序

将我们通过where筛序出的结果集,全部放入到sort buffer中,按照order by 的条件进行排序,最终直接返回数据集合。

缺点:所有字段全部放入sort buffer中占用空间

-- mysql 8.0.27 版本
            
            "filesort_summary": { -- 文件排序信息
              "memory_available": 262144, 
              "key_size": 61,
              "row_size": 230, 
              "max_rows_per_buffer": 106,
              "num_rows_estimate": 106,
              "num_rows_found": 93,
              "num_initial_chunks_spilled_to_disk": 0,
              "peak_memory_used": 33792,
              "sort_algorithm": "std::sort",
              "sort_mode": "<fixed_sort_key, packed_additional_fields>"
            } /* filesort_summary */


-- mysql 5.6 版本
            "filesort_summary": {
              "rows": 93,   -- 预计扫描行数
              "examined_rows": 106, -- 参与排序的行
              "number_of_tmp_files": 0,-- 使用临时文件的个数,这个值如果为0代表全部使用的sort_buffer内存排序,否则使用的磁盘文件排序
              "sort_buffer_size": 234112,-- 排序缓存的大小,单位Byte
              "sort_mode": "<sort_key, additional_fields>" -- 排序方式,这里用的单路排序
            } /* filesort_summary */

双路排序

将我们通过where筛序出的结果集当中的主键id(或许是伪列rowid)以及order by的字段放入sort buffer当中,按照order by 的条件进行排序,然后按照排序后的主键id(或许是伪列rowid)进行回表查询出满足所有条件的记录,最终直接返回数据集合。

优点:当sort buffer空间有限时适合使用,因为放入的字段少,占用空间小。

-- mysql 8.0.27 版本 效果等同同mysql版本的单路排序(已失效)

     "filesort_summary": {
              "memory_available": 262144,
              "key_size": 61,
              "row_size": 230,
              "max_rows_per_buffer": 106,
              "num_rows_estimate": 106,
              "num_rows_found": 93,
              "num_initial_chunks_spilled_to_disk": 0,
              "peak_memory_used": 33792,
              "sort_algorithm": "std::sort",
              "sort_mode": "<fixed_sort_key, packed_additional_fields>"
            }

-- mysql 5.6版本

           "filesort_summary": {
              "rows": 93,
              "examined_rows": 106,
              "number_of_tmp_files": 0,
              "sort_buffer_size": 72416,
              "sort_mode": "<sort_key, rowid>" -- 双路排序
            } /* filesort_summary */

选择依据

MySQL 通过比较系统变量 max_length_for_sort_data(mysql8.0默认4096字节,mysql5.6默认1024字节) 的大小和需要查询的字段总大小来判断使用哪种排序模式。

  • 如果字段的总长度小于max_length_for_sort_data ,那么使用 单路排序模式。
  • 如果字段的总长度大于max_length_for_sort_data ,那么使用 双路排序模式。

 

 

结束语

记得最后关闭trace工具哟!

-- 关闭trace
set session optimizer_trace="enabled=off",end_markers_in_json=OFF;  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值