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
此处以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
{
“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((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((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((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((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((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((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((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”: [
]
}
}
]
}
}
]
}