介绍
对于 MySQL 5.6 以及之前的版本来说,查询优化器就像是一个黑盒子一样,
只能通过 explain 语句查看最后优化器决定使用的执行计划,却无法知道查询优化器为什么做这个决策。
在 MySQL 5.6 以及之后的版本中,设计 MySQL 的人提出了一个 optimizer trace 的功能,
这个功能可以方便的查看查询优化器生成执行计划的整个过程。
这个功能的开启与关闭由系统变量 optimizer_trace 决定:show variables like ‘optimizer_trace’;
enabled 的值为 off,表明这个功能默认是关闭的。
one_line 的值是控制输出格式的,如果值为 on,那么所有输出都将在一行中展示。
如果想打开这个功能,必须首先把 enabled 的值改为 on:set optimizer_trace=”enabled=on”;
然后就可以输入要查看优化过程的查询语句,当该查询语句执行完成后,就可以到 information_schema 数据库下的 optimizer_trace 表中查看完整的优化过程。
这个 optimizer_trace 表共有 4 个列,分别是:
- query:表示我们的查询语句。
- trace:表示优化过程的 json 格式文本。
- missing_bytes_beyound_max_mem_size:由于优化过程可能会输出很多,如果超过某个限制,多余的文本将不会被显示,这个字段展示了被忽略文本的字节数。
- insufficient_privileges:表示是否没有权限查看优化过程,默认值是 0,只有某些特殊情况下才会是 1。0 代表有权限,1 代表无权限。(单词的意思为:权限不够)
完整的使用 optimizer trace 功能的步骤如下:
json 文本
# 1. 打开 optimizer trace 功能 (默认是关闭的):set optimizer_trace="enabled=on";# 2. 这里输入你自己的查询语句select ...;# 3. 从 optimizer_trace 表中查看上一个查询的优化过程select * from information_schema.OPTIMIZER_TRACE;# 4. 如果需要观察其他语句执行的优化过程,重复上边的第 2、3 步# 5. 停止查看语句的优化过程,把 optimizer trace 功能关闭set optimizer_trace="enabled=off";
比如:explain select * from s1 where key1 > ‘a’ and key2 < 1000000 and key3 in ( ‘a’, ‘b’, ‘c’ ) and common_field = ‘abc’;
执行计划如下表:
| id | select_type | table | type | possible_keys | key | rows | filtered |
|---|---|---|---|---|---|---|---|
| 1 | simple | s1 | range | idx_key2,idx_key1,idx_key3 | idx_key2 | 12 | 0.42 |
可以看到该查询可能使用到的索引有 3 个,
那么为什么查询优化器最终选择了 idx_key2 而不选择其他的索引或者全表扫描呢?
这时候就可以通过 otpimzer trace 功能来查看查询优化器的具体工作过程来寻找答案:
// 分析的查询语句是什么QUERY: SELECT * FROM s1 WHEREkey1 > 'z' ANDkey2 < 1000000 ANDkey3 IN ('a', 'b', 'c') ANDcommon_field = 'abc'// 优化的具体过程TRACE: {"steps": [{// prepare 阶段"join_preparation": {"select#": 1,"steps": [{"IN_uses_bisection": true},{"expanded_query": "/* select#1 */ select `s1`.`id` AS `id`,`s1`.`key1` AS `key1`,`s1`.`key2` AS `key2`,`s1`.`key3` AS `key3`,`s1`.`key_part1` AS `key_part1`,`s1`.`key_part2` AS `key_part2`,`s1`.`key_part3` AS `key_part3`,`s1`.`common_field` AS `common_field` from `s1` where ((`s1`.`key1` > 'z') and (`s1`.`key2` < 1000000) and (`s1`.`key3` in ('a','b','c')) and (`s1`.`common_field` = 'abc'))"}]}},{// optimize 阶段"join_optimization": {"select#": 1,"steps": [{// 处理搜索条件"condition_processing": {"condition": "WHERE",// 原始的搜索条件"original_condition": "((`s1`.`key1` > 'z') and (`s1`.`key2` < 1000000) and (`s1`.`key3` in ('a','b','c')) and (`s1`.`common_field` = 'abc'))","steps": [{// 等值传递转换"transformation": "equality_propagation","resulting_condition": "((`s1`.`key1` > 'z') and (`s1`.`key2` < 1000000) and (`s1`.`key3` in ('a','b','c')) and (`s1`.`common_field` = 'abc'))"},{// 常量传递转换"transformation": "constant_propagation","resulting_condition": "((`s1`.`key1` > 'z') and (`s1`.`key2` < 1000000) and (`s1`.`key3` in ('a','b','c')) and (`s1`.`common_field` = 'abc'))"},{// 去除没用的条件"transformation": "trivial_condition_removal","resulting_condition": "((`s1`.`key1` > 'z') and (`s1`.`key2` < 1000000) and (`s1`.`key3` in ('a','b','c')) and (`s1`.`common_field` = 'abc'))"}]}},{// 替换虚拟生成列"substitute_generated_columns": {}},{// 表的依赖信息"table_dependencies": [{"table": "`s1`","row_may_be_null": false,"map_bit": 0,"depends_on_map_bits": []}]},{"ref_optimizer_key_uses": []},{// 预估不同单表访问方法的访问成本"rows_estimation": [{"table": "`s1`","range_analysis": {// 全表扫描的行数以及成本"table_scan": {"rows": 9688,"cost": 2036.7},// 分析可能使用的索引"potential_range_indexes": [{// 主键索引不可用"index": "PRIMARY","usable": false,"cause": "not_applicable"},{// idx_key2 索引可能被使用"index": "idx_key2","usable": true,"key_parts": ["key2"]},{// idx_key1 索引可能被使用"index": "idx_key1","usable": true,"key_parts": ["key1","id"]},{// idx_key3 索引可能被使用"index": "idx_key3","usable": true,"key_parts": ["key3","id"]},{// idx_key_part 联合索引不可用"index": "idx_key_part","usable": false,"cause": "not_applicable"}]"setup_range_conditions": []"group_index_range": {"chosen": false,"cause": "not_group_by_or_distinct"}// 分析各种可能使用的索引的成本"analyzing_range_alternatives": {"range_scan_alternatives": [{// 使用 idx_key2 索引的成本分析"index": "idx_key2",// 使用 idx_key2 索引的范围区间"ranges": ["NULL < key2 < 1000000"],// 是否使用 index dive"index_dives_for_eq_ranges": true,// 使用该索引获取的记录是否按照主键排序"rowid_ordered": false,// 是否使用 mrr"using_mrr": false,// 是否是索引覆盖访问"index_only": false,// 使用该索引获取的记录条数"rows": 12,// 使用该索引的成本"cost": 15.41,// 是否选择该索引"chosen": true},{// 使用 idx_key1 索引的成本分析"index": "idx_key1",// 使用 idx_key1 索引的范围区间"ranges": ["z < key1"],"index_dives_for_eq_ranges": true,"rowid_ordered": false,"using_mrr": false,"index_only": false,"rows": 266,"cost": 320.21,"chosen": false,// 不选择该索引的原因:成本太大"cause": "cost"},{// 使用 idx_key3 索引的成本分析"index": "idx_key3",// 使用 idx_key3 索引的范围区间"ranges": ["a <= key3 <= a","b <= key3 <= b","c <= key3 <= c"],"index_dives_for_eq_ranges": true,"rowid_ordered": false,"using_mrr": false,"index_only": false,"rows": 21,"cost": 28.21,"chosen": false,"cause": "cost"}]// 分析使用索引合并的成本"analyzing_roworder_intersect": {"usable": false,"cause": "too_few_roworder_scans"}},// 对于上述 s1 单表查询最优的访问方法"chosen_range_access_summary": {"range_access_plan": {"type": "range_scan","index": "idx_key2","rows": 12,"ranges": ["NULL < key2 < 1000000"]},"rows_for_plan": 12,"cost_for_plan": 15.41,"chosen": true}}}]},{// 分析各种可能的执行计划// 对多表查询可能有很多种不同的方案// 单表查询的方案上边已经分析过了,直接选取 idx_key2 索引"considered_execution_plans": [{"plan_prefix": [],"table": "`s1`","best_access_path": {"considered_access_paths": [{"rows_to_scan": 12,"access_type": "range","range_details": {"used_index": "idx_key2"},"resulting_rows": 12,"cost": 17.81,"chosen": true}]},"condition_filtering_pct": 100,"rows_for_plan": 12,"cost_for_plan": 17.81,"chosen": true}]},{// 尝试给查询添加一些其他的查询条件"attaching_conditions_to_tables": {"original_condition": "((`s1`.`key1` > 'z') and (`s1`.`key2` < 1000000) and (`s1`.`key3` in ('a','b','c')) and (`s1`.`common_field` = 'abc'))","attached_conditions_computation": [],"attached_conditions_summary": [{"table": "`s1`","attached": "((`s1`.`key1` > 'z') and (`s1`.`key2` < 1000000) and (`s1`.`key3` in ('a','b','c')) and (`s1`.`common_field` = 'abc'))"}]}},{// 再稍微改进执行计划"refine_plan": [{"table": "`s1`","pushed_index_condition": "(`s1`.`key2` < 1000000)","table_condition_attached": "((`s1`.`key1` > 'z') and (`s1`.`key3` in ('a','b','c')) and (`s1`.`common_field` = 'abc'))"}]}]}},{// execute 阶段"join_execution": {"select#": 1,"steps": []}}]}// 因优化过程文本太多而丢弃的文本字节大小,值为0时表示并没有丢弃MISSING_BYTES_BEYOND_MAX_MEM_SIZE: 0// 权限字段(权限不足)INSUFFICIENT_PRIVILEGES: 0
总结
这只是优化器执行过程中的一小部分,
设计 MySQL 的人可能会在之后的版本中添加更多的优化过程信息。
虽然复杂但是有规律,优化过程大致分了三个阶段:
- prepare 阶段
- optimize 阶段
- execute 阶段
我们所说的基于成本的优化主要集中在 optimize 阶段。
对于单表查询来说,我们主要关注 optimize 阶段的 “rows_estimation” 这个过程,
“rows_estimation” 深入分析了对单表查询的各种执行方案的成本。
对于多表连接查询来说,我们更多需要关注 “considered_execution_plans” 这个过程,
“considered_execution_plans” 里会写明各种不同的连接方式所对应的成本。
查询优化器最终会选择成本最低的那种方案来作为最终的执行计划,
也就是使用 explain 语句所展现出的那种方案。
