介绍
Optimizer Trace 是 MySQL 5.6.3 里新加的一个特性,可以把 MySQL Optimizer 的决策和执行过程输出成文本。输出使用 JSON 格式,便于程序分析和人类阅读。
使用方法
因为 Optimizer Trace 默认是关闭状态,先启用它。
SET optimizer_trace="enabled=on";
同时执行计划和 select trace 语句:
EXPLAIN SELECT create_time FROM tb_openemptycard_order WHERE create_time > UNIX_TIMESTAMP('2018-08-08')*1000;
select trace from information_schema.optimizer_trace;
输出中则会显示 trace 文本,以 json 的格式展示。
执行结果
{
"steps": [
{
"join_preparation": {
"select#": 1,
"steps": [
{
"expanded_query": "/* select#1 */ select `tb_openemptycard_order`.`create_time` AS `create_time` from `tb_openemptycard_order` where (`tb_openemptycard_order`.`create_time` > 1533657600000)"
}
]
}
},
{
"join_optimization": {
"select#": 1,
"steps": [
{
"condition_processing": {
"condition": "WHERE",
"original_condition": "(`tb_openemptycard_order`.`create_time` > 1533657600000)",
"steps": [
{
"transformation": "equality_propagation",
"resulting_condition": "(`tb_openemptycard_order`.`create_time` > 1533657600000)"
},
{
"transformation": "constant_propagation",
"resulting_condition": "(`tb_openemptycard_order`.`create_time` > 1533657600000)"
},
{
"transformation": "trivial_condition_removal",
"resulting_condition": "(`tb_openemptycard_order`.`create_time` > 1533657600000)"
}
]
}
},
{
"substitute_generated_columns": {}
},
{
"table_dependencies": [
{
"table": "`tb_openemptycard_order`",
"row_may_be_null": false,
"map_bit": 0,
"depends_on_map_bits": []
}
]
},
{
"ref_optimizer_key_uses": []
},
{
"rows_estimation": [
{
"table": "`tb_openemptycard_order`",
"range_analysis": {
"table_scan": {
"rows": 538619,
"cost": 213454
},
"potential_range_indexes": [
{
"index": "PRIMARY",
"usable": false,
"cause": "not_applicable"
},
{
"index": "sys_order_id",
"usable": false,
"cause": "not_applicable"
},
{
"index": "user_id",
"usable": false,
"cause": "not_applicable"
},
{
"index": "iccid",
"usable": false,
"cause": "not_applicable"
},
{
"index": "dealer_id",
"usable": false,
"cause": "not_applicable"
},
{
"index": "sys_order_id_pay",
"usable": false,
"cause": "not_applicable"
},
{
"index": "transaction_id",
"usable": false,
"cause": "not_applicable"
},
{
"index": "app_type",
"usable": false,
"cause": "not_applicable"
},
{
"index": "create_time",
"usable": true,
"key_parts": [
"create_time",
"id"
]
},
{
"index": "order_status",
"usable": false,
"cause": "not_applicable"
},
{
"index": "modify_time",
"usable": false,
"cause": "not_applicable"
},
{
"index": "phone_number",
"usable": false,
"cause": "not_applicable"
}
],
"best_covering_index_scan": {
"index": "create_time",
"cost": 108578,
"chosen": true
},
"setup_range_conditions": [],
"group_index_range": {
"chosen": false,
"cause": "not_group_by_or_distinct"
},
"analyzing_range_alternatives": {
"range_scan_alternatives": [
{
"index": "create_time",
"ranges": [
"1533657600000 < create_time"
],
"index_dives_for_eq_ranges": true,
"rowid_ordered": false,
"using_mrr": false,
"index_only": true,
"rows": 269309,
"cost": 54290,
"chosen": true
}
],
"analyzing_roworder_intersect": {
"usable": false,
"cause": "too_few_roworder_scans"
}
},
"chosen_range_access_summary": {
"range_access_plan": {
"type": "range_scan",
"index": "create_time",
"rows": 269309,
"ranges": [
"1533657600000 < create_time"
]
},
"rows_for_plan": 269309,
"cost_for_plan": 54290,
"chosen": true
}
}
}
]
},
{
"considered_execution_plans": [
{
"plan_prefix": [],
"table": "`tb_openemptycard_order`",
"best_access_path": {
"considered_access_paths": [
{
"rows_to_scan": 269309,
"access_type": "range",
"range_details": {
"used_index": "create_time"
},
"resulting_rows": 269309,
"cost": 108151,
"chosen": true
}
]
},
"condition_filtering_pct": 100,
"rows_for_plan": 269309,
"cost_for_plan": 108151,
"chosen": true
}
]
},
{
"attaching_conditions_to_tables": {
"original_condition": "(`tb_openemptycard_order`.`create_time` > 1533657600000)",
"attached_conditions_computation": [],
"attached_conditions_summary": [
{
"table": "`tb_openemptycard_order`",
"attached": "(`tb_openemptycard_order`.`create_time` > 1533657600000)"
}
]
}
},
{
"refine_plan": [
{
"table": "`tb_openemptycard_order`"
}
]
}
]
}
},
{
"join_explain": {
"select#": 1,
"steps": []
}
}
]
}
很多时候我们只看 considered_execution_plans 内容,然后分析执行计划得出的结论。
