MYSQL 执行计划追踪 + json数据

mysql优化三板斧(explain, profiling,optimizer_trace)
explain是各种执行计划选择的结果(select ,update, delete)

想看整个执行计划以及对于多种索引方案之间是如何选择的,MySQL5.6中支持这个功能,optimizer_trace
此功能默认是关闭的,因为开启会带来性能损耗

[dbname: information_schema] 22:53:04 mysql> show variables like ‘%trace%’;
±-----------------------------±---------------------------------------------------------------------------+
| Variable_name | Value |
±-----------------------------±---------------------------------------------------------------------------+
| optimizer_trace | enabled=off,one_line=off |用来开启此功能
| optimizer_trace_features | greedy_search=on,range_optimizer=on,dynamic_range=on,repeated_subselect=on |
| optimizer_trace_limit | 1 |记录的条数
| optimizer_trace_max_mem_size | 16384 |占用内存大小限制
| optimizer_trace_offset | -1 |与limit配合使用
±-----------------------------±---------------------------------------------------------------------------+

开启此功能
use information_schema;
set optimizer_trace = ‘enabled=on’;
SET OPTIMIZER_TRACE_MAX_MEM_SIZE=1000000

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-J0X2zLP9-1588942623862)(https://img-blog.csdn.net/20170821231059555?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvY2pxaF9oYW8=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)]

此处以order_list表为例,表中有14w条数据,id是primary key
explain select id from order_list where id not in (select id from order_list where id =12345)

执行计划三个阶段:
join_prepareation
join_optimization
join_explain

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-wDrnjSyv-1588942623867)(https://img-blog.csdn.net/20170821231125124?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvY2pxaF9oYW8=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)]

{
“steps”: [
{
“join_preparation”: {
“select#”: 1,
“steps”: [
{
“join_preparation”: {
“select#”: 2,
“steps”: [
{
“expanded_query”: “/* select#2 / select order_list.id from order_list where (order_list.id = 12345)"
},
{
“transformation”: {
“select#”: 2,
“from”: “IN (SELECT)”,
“to”: “semijoin”,
“chosen”: false
}
},
{
“transformation”: {
“select#”: 2,
“from”: “IN (SELECT)”,
“to”: “EXISTS (CORRELATED SELECT)”,
“chosen”: true,
“evaluating_constant_where_conditions”: [
]
}
}
]
}
},
{
“expanded_query”: "/
select#1 / select order_list.id AS id from order_list where (not(<in_optimizer>(order_list.id,(/ select#2 / select order_list.id from order_list where ((order_list.id = 12345) and ((order_list.id) = order_list.id))))))"
}
]
}
},
{
“join_optimization”: {
“select#”: 1,
“steps”: [
{
“condition_processing”: {
“condition”: “WHERE”,
“original_condition”: "(not(<in_optimizer>(order_list.id,(/
select#2 / select order_list.id from order_list where ((order_list.id = 12345) and ((order_list.id) = order_list.id))))))",
“steps”: [
{
“transformation”: “equality_propagation”,
“subselect_evaluation”: [
],
“resulting_condition”: "(not(<in_optimizer>(order_list.id,(/
select#2 / select order_list.id from order_list where ((order_list.id = 12345) and ((order_list.id) = order_list.id))))))"
},
{
“transformation”: “constant_propagation”,
“subselect_evaluation”: [
],
“resulting_condition”: "(not(<in_optimizer>(order_list.id,(/
select#2 / select order_list.id from order_list where ((order_list.id = 12345) and ((order_list.id) = order_list.id))))))"
},
{
“transformation”: “trivial_condition_removal”,
“subselect_evaluation”: [
],
“resulting_condition”: "(not(<in_optimizer>(order_list.id,(/
select#2 / select order_list.id from order_list where ((order_list.id = 12345) and ((order_list.id) = order_list.id))))))"
}
]
}
},
{
“table_dependencies”: [
{
“table”: “order_list”,
“row_may_be_null”: false,
“map_bit”: 0,
“depends_on_map_bits”: [
]
}
]
},
{
“ref_optimizer_key_uses”: [
]
},
{
“rows_estimation”: [
{
“table”: “order_list”,
“table_scan”: {
“rows”: 144323,
“cost”: 6957
}
}
]
},
{
“considered_execution_plans”: [
{
“plan_prefix”: [
],
“table”: “order_list”,
“best_access_path”: {
“considered_access_paths”: [
{
“access_type”: “scan”,
“rows”: 144323,
“cost”: 35822,
“chosen”: true
}
]
},
“cost_for_plan”: 35822,
“rows_for_plan”: 144323,
“chosen”: true
}
]
},
{
“attaching_conditions_to_tables”: {
“original_condition”: "(not(<in_optimizer>(order_list.id,(/
select#2 / select order_list.id from order_list where ((order_list.id = 12345) and ((order_list.id) = order_list.id))))))",
“attached_conditions_computation”: [
],
“attached_conditions_summary”: [
{
“table”: “order_list”,
“attached”: "(not(<in_optimizer>(order_list.id,(/
select#2 */ select order_list.id from order_list where ((order_list.id = 12345) and ((order_list.id) = order_list.id))))))”
}
]
}
},
{
“refine_plan”: [
{
“table”: “order_list”,
“access_type”: “index_scan”
}
]
}
]
}
},
{
“join_explain”: {
“select#”: 1,
“steps”: [
{
“join_optimization”: {
“select#”: 2,
“steps”: [
{
“condition_processing”: {
“condition”: “WHERE”,
“original_condition”: “((order_list.id = 12345) and ((order_list.id) = order_list.id))”,
“steps”: [
{
“transformation”: “equality_propagation”,
“resulting_condition”: “(((order_list.id) = 12345) and multiple equal(12345, order_list.id))”
},
{
“transformation”: “constant_propagation”,
“resulting_condition”: “(((order_list.id) = 12345) and multiple equal(12345, order_list.id))”
},
{
“transformation”: “trivial_condition_removal”,
“resulting_condition”: “(((order_list.id) = 12345) and multiple equal(12345, order_list.id))”
}
]
}
},
{
“table_dependencies”: [
{
“table”: “order_list”,
“row_may_be_null”: false,
“map_bit”: 0,
“depends_on_map_bits”: [
]
}
]
},
{
“ref_optimizer_key_uses”: [
{
“table”: “order_list”,
“field”: “id”,
“equals”: “12345”,
“null_rejecting”: false
}
]
},
{
“rows_estimation”: [
{
“table”: “order_list”,
“rows”: 1,
“cost”: 1,
“table_type”: “const”,
“empty”: true
}
]
},
{
“transformation”: {
“select#”: 2,
“from”: “IN (SELECT)”,
“to”: “materialization”,
“has_nullable_expressions”: false,
“treat_UNKNOWN_as_FALSE”: false,
“possible”: true
}
},
{
“execution_plan_for_potential_materialization”: {
“steps”: [
],
“subq_mat_decision”: {
“parent_fanouts”: [
{
“select#”: 1,
“subq_attached_to_table”: true,
“table”: “order_list”,
“fanout”: 144323,
“cacheable”: true
}
],
“cost_to_create_and_fill_materialized_table”: 3.2,
“cost_of_one_EXISTS”: 1,
“number_of_subquery_evaluations”: 144323,
“cost_of_materialization”: 28868,
“cost_of_EXISTS”: 144323,
“chosen”: true
}
}
},
{
“transformation”: {
“select#”: 2,
“from”: “IN (SELECT)”,
“to”: “materialization”,
“chosen”: true,
“unknown_key_1”: {
“creating_tmp_table”: {
“tmp_table_info”: {
“row_length”: 9,
“key_length”: 8,
“unique_constraint”: false,
“location”: “memory (heap)”,
“row_limit_estimate”: 1864135
}
}
}
}
},
{
“condition_on_constant_tables”: “0”,
“condition_value”: false
}
],
“empty_result”: {
“cause”: “Impossible WHERE noticed after reading const tables”
}
}
},
{
“join_explain”: {
“select#”: 2,
“steps”: [
]
}
}
]
}
}
]
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值