MySQL5.6
版本开始,推出了对SQL的跟踪工具trace
,通过使用trace
,用户可以查看MySQL优化器对SQL语句的分析结果,以及生成了怎样的执行计划。
1、开启/关闭trace工具
开启:set session optimizer_trace="enabled=on",end_markers_in_json=on;
关闭:set session optimizer_trace="enabled=off";
开启trace工具会对MySQL的性能造成一定的影响,所以建议在需要分析SQL语句的执行计划生成过程时才开启trace工具;
2、执行需要进行分析的SQL语句
select name, age, job from emp where name = "hu" and age > 20 and job = "产品";
3、查询执行计划
执行SQL
语句:SELECT * FROM information_schema.OPTIMIZER_TRACE;
{
"steps": [
{
"join_preparation": {
"select#": 1,
"steps": [
{
"expanded_query": "/* select#1 */ select `emp`.`name` AS `name`,`emp`.`age` AS `age`,`emp`.`job` AS `job` from `emp` where ((`emp`.`name` = 'hu') and (`emp`.`age` > 20) and (`emp`.`job` = '产品')) limit 0,1000"
}
] /* steps */
} /* join_preparation */
},
{
"join_optimization": {
"select#": 1,
"steps": [
{
"condition_processing": {
"condition": "WHERE",
"original_condition": "((`emp`.`name` = 'hu') and (`emp`.`age` > 20) and (`emp`.`job` = '产品'))",
"steps": [
{
"transformation": "equality_propagation",
"resulting_condition": "((`emp`.`name` = 'hu') and (`emp`.`age` > 20) and (`emp`.`job` = '产品'))"
},
{
"transformation": "constant_propagation",
"resulting_condition": "((`emp`.`name` = 'hu') and (`emp`.`age` > 20) and (`emp`.`job` = '产品'))"
},
{
"transformation": "trivial_condition_removal",
"resulting_condition": "((`emp`.`name` = 'hu') and (`emp`.`age` > 20) and (`emp`.`job` = '产品'))"
}
] /* steps */
} /* condition_processing */
},
{
"substitute_generated_columns": {
} /* substitute_generated_columns */
},
{
"table_dependencies": [
{
"table": "`emp`",
"row_may_be_null": false,
"map_bit": 0,
"depends_on_map_bits": [
] /* depends_on_map_bits */
}
] /* table_dependencies */
},
{
"ref_optimizer_key_uses": [
{
"table": "`emp`",
"field": "name",
"equals": "'hu'",
"null_rejecting": false
}
] /* ref_optimizer_key_uses */
},
{
"rows_estimation": [
{
"table": "`emp`",
"range_analysis": {
"table_scan": {
"rows": 651300,
"cost": 227957
} /* table_scan */,
"potential_range_indexes": [
{
"index": "PRIMARY",
"usable": false,
"cause": "not_applicable"
},
{
"index": "idx_name_age_job",
"usable": true,
"key_parts": [
"name",
"age",
"job",
"id"
] /* key_parts */
}
] /* potential_range_indexes */,
"best_covering_index_scan": {
"index": "idx_name_age_job",
"cost": 73074,
"chosen": true
} /* best_covering_index_scan */,
"setup_range_conditions": [
] /* setup_range_conditions */,
"group_index_range": {
"chosen": false,
"cause": "not_group_by_or_distinct"
} /* group_index_range */,
"skip_scan_range": {
"potential_skip_scan_indexes": [
{
"index": "idx_name_age_job",
"usable": false,
"cause": "prefix_not_const_equality"
}
] /* potential_skip_scan_indexes */
} /* skip_scan_range */,
"analyzing_range_alternatives": {
"range_scan_alternatives": [
{
"index": "idx_name_age_job",
"ranges": [
"hu <= name <= hu AND 20 < age"
] /* ranges */,
"index_dives_for_eq_ranges": true,
"rowid_ordered": false,
"using_mrr": false,
"index_only": true,
"rows": 1,
"cost": 1.11,
"chosen": true
}
] /* range_scan_alternatives */,
"analyzing_roworder_intersect": {
"usable": false,
"cause": "too_few_roworder_scans"
} /* analyzing_roworder_intersect */
} /* analyzing_range_alternatives */,
"chosen_range_access_summary": {
"range_access_plan": {
"type": "range_scan",
"index": "idx_name_age_job",
"rows": 1,
"ranges": [
"hu <= name <= hu AND 20 < age"
] /* ranges */
} /* range_access_plan */,
"rows_for_plan": 1,
"cost_for_plan": 1.11,
"chosen": true
} /* chosen_range_access_summary */
} /* range_analysis */
}
] /* rows_estimation */
},
{
"considered_execution_plans": [
{
"plan_prefix": [
] /* plan_prefix */,
"table": "`emp`",
"best_access_path": {
"considered_access_paths": [
{
"access_type": "ref",
"index": "idx_name_age_job",
"chosen": false,
"cause": "range_uses_more_keyparts"
},
{
"rows_to_scan": 1,
"access_type": "range",
"range_details": {
"used_index": "idx_name_age_job"
} /* range_details */,
"resulting_rows": 1,
"cost": 1.21,
"chosen": true
}
] /* considered_access_paths */
} /* best_access_path */,
"condition_filtering_pct": 100,
"rows_for_plan": 1,
"cost_for_plan": 1.21,
"chosen": true
}
] /* considered_execution_plans */
},
{
"attaching_conditions_to_tables": {
"original_condition": "((`emp`.`name` = 'hu') and (`emp`.`age` > 20) and (`emp`.`job` = '产品'))",
"attached_conditions_computation": [
] /* attached_conditions_computation */,
"attached_conditions_summary": [
{
"table": "`emp`",
"attached": "((`emp`.`name` = 'hu') and (`emp`.`age` > 20) and (`emp`.`job` = '产品'))"
}
] /* attached_conditions_summary */
} /* attaching_conditions_to_tables */
},
{
"finalizing_table_conditions": [
{
"table": "`emp`",
"original_table_condition": "((`emp`.`name` = 'hu') and (`emp`.`age` > 20) and (`emp`.`job` = '产品'))",
"final_table_condition ": "((`emp`.`name` = 'hu') and (`emp`.`age` > 20) and (`emp`.`job` = '产品'))"
}
] /* finalizing_table_conditions */
},
{
"refine_plan": [
{
"table": "`emp`"
}
] /* refine_plan */
}
] /* steps */
} /* join_optimization */
},
{
"join_execution": {
"select#": 1,
"steps": [
] /* steps */
} /* join_execution */
}
] /* steps */
}
steps
-> json_optimization
-> rows_estimation
"rows_estimation": [
{
"table": "`emp`",
"range_analysis": {
"table_scan": {
/* 全表扫描预估扫描651300条记录,花费是227957*/
"rows": 651300,
"cost": 227957
},
"potential_range_indexes": [
/* 可能用到的索引 */
{
/* 主键索引 =》 不适用*/
"index": "PRIMARY",
"usable": false,
"cause": "not_applicable"
},
{
/* 联合索引idx_name_age_job =》 适用*/
"index": "idx_name_age_job",
"usable": true,
"key_parts": [
"name",
"age",
"job",
"id"
] /* key_parts */
}
] /* potential_range_indexes */,
"best_covering_index_scan": {
/* 最优选择,使用联合索引idx_name_age_job */
"index": "idx_name_age_job",
"cost": 73074,
"chosen": true
} /* best_covering_index_scan */,
"setup_range_conditions": [
] /* setup_range_conditions */,
"group_index_range": {
"chosen": false,
"cause": "not_group_by_or_distinct"
} /* group_index_range */,
"skip_scan_range": {
"potential_skip_scan_indexes": [
{
"index": "idx_name_age_job",
"usable": false,
"cause": "prefix_not_const_equality"
}
] /* potential_skip_scan_indexes */
} /* skip_scan_range */,
"analyzing_range_alternatives": {
"range_scan_alternatives": [
{
"index": "idx_name_age_job",
"ranges": [
"hu <= name <= hu AND 20 < age"
] /* ranges */,
"index_dives_for_eq_ranges": true,
"rowid_ordered": false,
"using_mrr": false,
"index_only": true,
"rows": 1,
"cost": 1.11,
"chosen": true
}
] /* range_scan_alternatives */,
"analyzing_roworder_intersect": {
"usable": false,
"cause": "too_few_roworder_scans"
} /* analyzing_roworder_intersect */
} /* analyzing_range_alternatives */,
"chosen_range_access_summary": {
"range_access_plan": {
"type": "range_scan",
"index": "idx_name_age_job",
"rows": 1,
"ranges": [
"hu <= name <= hu AND 20 < age"
] /* ranges */
} /* range_access_plan */,
"rows_for_plan": 1,
"cost_for_plan": 1.11,
"chosen": true
} /* chosen_range_access_summary */
} /* range_analysis */
}
] /* rows_estimation */