有些蛙友可能觉得凭什麽mysql最佳化工具定出的执行计画跟我不一样,我可能觉得我预想的执行计画比较快!
所以mysql5.7以後推出optimizer trace可以让你看到mysql为什麽订出这个执行计画的过程,看你服不服啦~
这功能预设是关闭,由系统变数optimizer_trace控制
mysql> show variables like 'optimizer_trace';
+-----------------+--------------------------+
| Variable_name | Value |
+-----------------+--------------------------+
| optimizer_trace | enabled=off,one_line=off |
+-----------------+--------------------------+
1 row in set (0.01 sec)
我们把它打开跑跑看
mysql> set optimizer_trace = "enabled=on";
Query OK, 0 rows affected (0.04 sec)
透过查询information_schema.optimizer_trace来看到制定计画的过程
我在里面直接注释说明每个步骤是干啥的
mysql> select * from single_table s1 where
-> key1 > 'z' and
-> key2 < 1000000 and
-> key3 in ('a','b','c') and
-> common_field = 'abc';
Empty set (0.08 sec)
mysql> select * from information_schema.optimizer_trace\G
*************************** 1. row ***************************
QUERY: select * from single_table s1 where
key1 > 'z' and
key2 < 1000000 and
key3 in ('a','b','c') and
common_field = 'abc'
TRACE: {
"steps": [
{
"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 `single_table` `s1` where ((`s1`.`key1` > 'z') and (`s1`.`key2` < 1000000) and (`s1`.`key3` in ('a','b','c')) and (`s1`.`common_field` = 'abc'))"
}
]
}
},
{
"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": "`single_table` `s1`",
"row_may_be_null": false,
"map_bit": 0,
"depends_on_map_bits": [
]
}
]
},
{
"ref_optimizer_key_uses": [
]
},
{
"rows_estimation": [
{
"table": "`single_table` `s1`",
"range_analysis": {
"table_scan": {
"rows": 10146,
"cost": 4084.75
},
"potential_range_indexes": [
{
"index": "PRIMARY",
"usable": false,//主键不能用
"cause": "not_applicable"
},
{
"index": "uk_key2",
"usable": true,
"key_parts": [
"key2"
]
},
{
"index": "idx_key1",
"usable": true,
"key_parts": [
"key1",
"id"
]
},
{
"index": "idx_key3",
"usable": true,
"key_parts": [
"key3",
"id"
]
},
{
"index": "idx_key_part",
"usable": false,
"cause": "not_applicable"
}
],
"setup_range_conditions": [
],
"group_index_range": {
"chosen": false,
"cause": "not_group_by_or_distinct"
},
"skip_scan_range": {
"potential_skip_scan_indexes": [
{
"index": "uk_key2",
"usable": false,
"cause": "query_references_nonkey_column"
},
{
"index": "idx_key1",
"usable": false,
"cause": "query_references_nonkey_column"
},
{
"index": "idx_key3",
"usable": false,
"cause": "query_references_nonkey_column"
}
]
},
"analyzing_range_alternatives": {//分析各种可能索引的使用成本
"range_scan_alternatives": [
{
"index": "uk_key2",
"ranges": [
"NULL < key2 < 1000000"
],
"index_dives_for_eq_ranges": true,
"rowid_ordered": false,//使用该索引获取的纪录是否按主键值排序
"using_mrr": false,
"index_only": false,
"rows": 10000,
"cost": 6500.26,
"chosen": false,
"cause": "cost"
},
{
"index": "idx_key1",
"ranges": [
"z < key1"
],
"index_dives_for_eq_ranges": true,
"rowid_ordered": false,
"using_mrr": false,
"index_only": false,
"rows": 1,
"cost": 0.91,
"chosen": true
},
{
"index": "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": 3,
"cost": 2.71,
"chosen": false,
"cause": "cost"
}
],
"analyzing_roworder_intersect": {//分析使用索引合并的成本
"usable": false,
"cause": "too_few_roworder_scans"
}
},
"chosen_range_access_summary": {
"range_access_plan": {
"type": "range_scan",
"index": "idx_key1",
"rows": 1,
"ranges": [
"z < key1"
]
},
"rows_for_plan": 1,
"cost_for_plan": 0.91,
"chosen": true
}
}
}
]
},
{
"considered_execution_plans": [//分析各种可能的执行计画
{
"plan_prefix": [
],
"table": "`single_table` `s1`",
"best_access_path": {
"considered_access_paths": [
{
"rows_to_scan": 1,
"access_type": "range",
"range_details": {
"used_index": "idx_key1"
},
"resulting_rows": 1,
"cost": 1.31,
"chosen": true
}
]
},
"condition_filtering_pct": 100,
"rows_for_plan": 1,
"cost_for_plan": 1.31,
"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": "`single_table` `s1`",
"attached": "((`s1`.`key1` > 'z') and (`s1`.`key2` < 1000000) and (`s1`.`key3` in ('a','b','c')) and (`s1`.`common_field` = 'abc'))"
}
]
}
},
{
"finalizing_table_conditions": [
{
"table": "`single_table` `s1`",
"original_table_condition": "((`s1`.`key1` > 'z') and (`s1`.`key2` < 1000000) and (`s1`.`key3` in ('a','b','c')) and (`s1`.`common_field` = 'abc'))",
"final_table_condition ": "((`s1`.`key1` > 'z') and (`s1`.`key2` < 1000000) and (`s1`.`key3` in ('a','b','c')) and (`s1`.`common_field` = 'abc'))"
}
]
},
{
"refine_plan": [
{
"table": "`single_table` `s1`",
"pushed_index_condition": "(`s1`.`key1` > 'z')",
"table_condition_attached": "((`s1`.`key2` < 1000000) and (`s1`.`key3` in ('a','b','c')) and (`s1`.`common_field` = 'abc'))"
}
]
}
]
}
},
{
"join_execution": {//执行阶段
"select#": 1,
"steps": [
]
}
}
]
}
MISSING_BYTES_BEYOND_MAX_MEM_SIZE: 0//可能此过程文字太多而忽略的bytes
INSUFFICIENT_PRIVILEGES: 0
1 row in set (0.40 sec)
>>: 30天零负担轻松学会制作APP介面及设计【DAY 18】
一、前言 在 JavaScript 的世界里有变数,那初学者们也知道 CSS (阶层式样式表,C...
引言 昨天介绍了套件管理软件以及图形化使用者界面的安装,也是指令介绍的最後一篇文章。学习了这麽多指令...
库存只剩 1 件,但却有 10 个人买到? 网路商城特卖会常常会推出特定商品限量 1 组的抢购活动...
看完这篇文章你会得到的成果图 此篇文章的范例程序码 github https://github.co...
简单来说,TeamCity 的运作方式是 Server + Agent 的架构。平常我们看到的 Te...