持续创作,加速成长!这是我参与「掘金日新计划 · 6 月更文挑战」的第19天,点击查看活动详情
确定执行计划(索引使用情况)
{
"considered_execution_plans": [
{
"plan_prefix": [
] /* plan_prefix */,
"table": "`test_index`",
"best_access_path": { -- 最优访问路径
"considered_access_paths": [ -- 最终选择的访问路径
{
"rows_to_scan": 106,
"access_type": "scan", -- 访问类型:为scan,全表扫描
"resulting_rows": 106,
"cost": 10.85,
"chosen": true, -- 确定选择
"use_tmp_table": true
}
] /* considered_access_paths */
} /* best_access_path */,
"condition_filtering_pct": 100,
"rows_for_plan": 106,
"cost_for_plan": 10.85,
"sort_cost": 106,
"new_cost_for_plan": 116.85,
"chosen": true
}
] /* considered_execution_plans */
}
第三阶段:SQL执行阶段 (此处判断filesort)
{
"join_execution": { -- 第三阶段:SQL执行阶段
"select#": 1,
"steps": [
{
"sorting_table": "test_index",
"filesort_information": [
{
"direction": "asc",
"expression": "`test_index`.`addr`"
}
] /* filesort_information */,
"filesort_priority_queue_optimization": {
"usable": false,
"cause": "not applicable (no LIMIT)"
} /* filesort_priority_queue_optimization */,
"filesort_execution": [
] /* filesort_execution */,
"filesort_summary": {
"memory_available": 262144,
"key_size": 61,
"row_size": 230,
"max_rows_per_buffer": 106,
"num_rows_estimate": 106,
"num_rows_found": 93,
"num_initial_chunks_spilled_to_disk": 0,
"peak_memory_used": 33792,
"sort_algorithm": "std::sort",
"sort_mode": "<fixed_sort_key, packed_additional_fields>"
} /* filesort_summary */
}
] /* steps */
} /* join_execution */
}
完整代码示例
{
"steps": [
{
"join_preparation": { -- 第一阶段:SQL准备阶段,格式化sql
"select#": 1,
"steps": [
{
"expanded_query": "/* select#1 */ select `test_index`.`id` AS `id`,`test_index`.`name` AS `name`,`test_index`.`score` AS `score`,`test_index`.`addr` AS `addr`,`test_index`.`ctime` AS `ctime` from `test_index` where (`test_index`.`name` > '北') order by `test_index`.`addr`"
}
] /* steps */
} /* join_preparation */
},
{
"join_optimization": { -- 第二阶段:SQL优化阶段(分析具体用哪个索引,以及分析使用的原因)
"select#": 1,
"steps": [
{
/* begin-condition_processing */
"condition_processing": { -- --条件处理
"condition": "WHERE",
"original_condition": "(`test_index`.`name` > '北')",
"steps": [
{
"transformation": "equality_propagation",
"resulting_condition": "(`test_index`.`name` > '北')"
},
{
"transformation": "constant_propagation",
"resulting_condition": "(`test_index`.`name` > '北')"
},
{
"transformation": "trivial_condition_removal",
"resulting_condition": "(`test_index`.`name` > '北')"
}
] /* steps */
} /* end-condition_processing */
},
{
"substitute_generated_columns": {
} /* substitute_generated_columns */
},
/* begin-table_dependencies */
{
"table_dependencies": [ -- 表依赖详情
{
"table": "`test_index`",
"row_may_be_null": false,
"map_bit": 0,
"depends_on_map_bits": [
] /* depends_on_map_bits */
}
] /* end-table_dependencies */
},
{
"ref_optimizer_key_uses": [
] /* ref_optimizer_key_uses */
},
/* begin-rows_estimation */
{
"rows_estimation": [ -- 预估表的访问成本
{
"table": "`test_index`",
"range_analysis": {
"table_scan": { -- 全表扫描情况
"rows": 106, -- 扫描行数
"cost": 12.95 -- 扫描成本
} /* table_scan */,
/* begin-potential_range_indexes */
"potential_range_indexes": [ -- 查询可能使用的索引
{
"index": "PRIMARY",
"usable": false,
"cause": "not_applicable"
},
{
"index": "union_name_score_addr", -- 辅助联合索引
"usable": true,
"key_parts": [
"name",
"score",
"addr",
"id"
] /* key_parts */
}
] /* end-potential_range_indexes */,
"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": "union_name_score_addr",
"usable": false,
"cause": "query_references_nonkey_column"
}
] /* potential_skip_scan_indexes */
} /* skip_scan_range */,
/* begin-analyzing_range_alternatives */
"analyzing_range_alternatives": { -- 分析各个索引使用成本
"range_scan_alternatives": [
{
"index": "union_name_score_addr",
"ranges": [
"北 < name" -- 索引使用范围
] /* ranges */,
"index_dives_for_eq_ranges": true,
"rowid_ordered": false, -- 使用该索引获取的记录是否按照主键排序
"using_mrr": false,
"index_only": false, -- 是否使用覆盖索引
"in_memory": 1,
"rows": 93, -- 索引扫描行数
"cost": 32.81, -- 索引使用成本
"chosen": false, -- 是否选择该索引
"cause": "cost"
}
] /* range_scan_alternatives */,
"analyzing_roworder_intersect": {
"usable": false,
"cause": "too_few_roworder_scans"
} /* analyzing_roworder_intersect */
} /* end-analyzing_range_alternatives */
} /* range_analysis */
}
] /* end-rows_estimation */
},
{
"considered_execution_plans": [
{
"plan_prefix": [
] /* plan_prefix */,
"table": "`test_index`",
"best_access_path": { -- 最优访问路径
"considered_access_paths": [ -- 最终选择的访问路径
{
"rows_to_scan": 106,
"access_type": "scan", -- 访问类型:为scan,全表扫描
"resulting_rows": 106,
"cost": 10.85,
"chosen": true, -- 确定选择
"use_tmp_table": true
}
] /* considered_access_paths */
} /* best_access_path */,
"condition_filtering_pct": 100,
"rows_for_plan": 106,
"cost_for_plan": 10.85,
"sort_cost": 106,
"new_cost_for_plan": 116.85,
"chosen": true
}
] /* considered_execution_plans */
},
{
"attaching_conditions_to_tables": {
"original_condition": "(`test_index`.`name` > '北')",
"attached_conditions_computation": [
] /* attached_conditions_computation */,
"attached_conditions_summary": [
{
"table": "`test_index`",
"attached": "(`test_index`.`name` > '北')"
}
] /* attached_conditions_summary */
} /* attaching_conditions_to_tables */
},
{
"optimizing_distinct_group_by_order_by": {
"simplifying_order_by": {
"original_clause": "`test_index`.`addr`",
"items": [
{
"item": "`test_index`.`addr`"
}
] /* items */,
"resulting_clause_is_simple": true,
"resulting_clause": "`test_index`.`addr`"
} /* simplifying_order_by */
} /* optimizing_distinct_group_by_order_by */
},
{
"reconsidering_access_paths_for_index_ordering": {
"clause": "ORDER BY",
"steps": [
] /* steps */,
"index_order_summary": {
"table": "`test_index`",
"index_provides_order": false,
"order_direction": "undefined",
"index": "unknown",
"plan_changed": false
} /* index_order_summary */
} /* reconsidering_access_paths_for_index_ordering */
},
{
"finalizing_table_conditions": [
{
"table": "`test_index`",
"original_table_condition": "(`test_index`.`name` > '北')",
"final_table_condition ": "(`test_index`.`name` > '北')"
}
] /* finalizing_table_conditions */
},
{
"refine_plan": [
{
"table": "`test_index`"
}
] /* refine_plan */
},
{
"considering_tmp_tables": [
{
"adding_sort_to_table": "test_index"
} /* filesort */
] /* considering_tmp_tables */
}
] /* steps */
} /* join_optimization */
},
{
"join_execution": { -- 第三阶段:SQL执行阶段
"select#": 1,
"steps": [
{
"sorting_table": "test_index",
"filesort_information": [
{
"direction": "asc",
"expression": "`test_index`.`addr`"
}
] /* filesort_information */,
"filesort_priority_queue_optimization": {
"usable": false,
"cause": "not applicable (no LIMIT)"
} /* filesort_priority_queue_optimization */,
"filesort_execution": [
] /* filesort_execution */,
"filesort_summary": {
"memory_available": 262144,
"key_size": 61,
"row_size": 230,
"max_rows_per_buffer": 106,
"num_rows_estimate": 106,
"num_rows_found": 93,
"num_initial_chunks_spilled_to_disk": 0,
"peak_memory_used": 33792,
"sort_algorithm": "std::sort",
"sort_mode": "<fixed_sort_key, packed_additional_fields>"
} /* filesort_summary */
}
] /* steps */
} /* join_execution */
}
] /* steps */
}