其中distinct使用了using temporary(临时表)
MySQL使用临时表保存临时的结构,以用于后续的处理,MySQL首先创建heap引擎的临时表,如果临时的数据过多,超过max_heap_table_size的大小,会自动把临时表转换成MyISAM引擎的表来使用。(引用http://mysql.taobao.org/monthly/2015/03/04/)
{
"steps": [
{
"join_preparation": {
"select#": 1,
"steps": [
{
"expanded_query": "/* select#1 */ select `customer`.`c_discount` AS `c_discount` from `customer` where (`customer`.`c_id` = 1)"
}
]
}
},
{
"join_optimization": {
"select#": 1,
"steps": [
{
"condition_processing": {
"condition": "WHERE",
"original_condition": "(`customer`.`c_id` = 1)",
"steps": [
{
"transformation": "equality_propagation",
"resulting_condition": "multiple equal(1, `customer`.`c_id`)"
},
{
"transformation": "constant_propagation",
"resulting_condition": "multiple equal(1, `customer`.`c_id`)"
},
{
"transformation": "trivial_condition_removal",
"resulting_condition": "multiple equal(1, `customer`.`c_id`)"
}
]
}
},
{
"substitute_generated_columns": {
}
},
{
"table_dependencies": [
{
"table": "`customer`",
"row_may_be_null": false,
"map_bit": 0,
"depends_on_map_bits": [
]
}
]
},
{
"ref_optimizer_key_uses": [
]
},
{
"rows_estimation": [
{
"table": "`customer`",
"table_scan": {
"rows": 289371,
"cost": 8114.6
}
}
]
},
{
"considered_execution_plans": [
{
"plan_prefix": [
],
"table": "`customer`",
"best_access_path": {
"considered_access_paths": [
{
"rows_to_scan": 289371,
"filtering_effect": [
],
"final_filtering_effect": 0.1,
"access_type": "scan",
"resulting_rows": 28937,
"cost": 37052,
"chosen": true
}
]
},
"condition_filtering_pct": 100,
"rows_for_plan": 28937,
"cost_for_plan": 37052,
"chosen": true
}
]
},
{
"attaching_conditions_to_tables": {
"original_condition": "(`customer`.`c_id` = 1)",
"attached_conditions_computation": [
],
"attached_conditions_summary": [
{
"table": "`customer`",
"attached": "(`customer`.`c_id` = 1)"
}
]
}
},
{
"refine_plan": [
{
"table": "`customer`"
}
]
}
]
}
},
{
"join_explain": {
"select#": 1,
"steps": [
]
}
}
]
}
{
"steps": [
{
"join_preparation": {
"select#": 1,
"steps": [
{
"expanded_query": "/* select#1 */ select distinct `customer`.`c_discount` AS `c_discount` from `customer` where (`customer`.`c_id` = 1)"
}
]
}
},
{
"join_optimization": {
"select#": 1,
"steps": [
{
"condition_processing": {
"condition": "WHERE",
"original_condition": "(`customer`.`c_id` = 1)",
"steps": [
{
"transformation": "equality_propagation",
"resulting_condition": "multiple equal(1, `customer`.`c_id`)"
},
{
"transformation": "constant_propagation",
"resulting_condition": "multiple equal(1, `customer`.`c_id`)"
},
{
"transformation": "trivial_condition_removal",
"resulting_condition": "multiple equal(1, `customer`.`c_id`)"
}
]
}
},
{
"substitute_generated_columns": {
}
},
{
"table_dependencies": [
{
"table": "`customer`",
"row_may_be_null": false,
"map_bit": 0,
"depends_on_map_bits": [
]
}
]
},
{
"ref_optimizer_key_uses": [
]
},
{
"rows_estimation": [
{
"table": "`customer`",
"table_scan": {
"rows": 289371,
"cost": 8114.6
}
}
]
},
{
"considered_execution_plans": [
{
"plan_prefix": [
],
"table": "`customer`",
"best_access_path": {
"considered_access_paths": [
{
"rows_to_scan": 289371,
"filtering_effect": [
],
"final_filtering_effect": 0.1,
"access_type": "scan",
"resulting_rows": 28937,
"cost": 37052,
"chosen": true
}
]
},
"condition_filtering_pct": 100,
"rows_for_plan": 28937,
"cost_for_plan": 37052,
"chosen": true
}
]
},
{
"attaching_conditions_to_tables": {
"original_condition": "(`customer`.`c_id` = 1)",
"attached_conditions_computation": [
],
"attached_conditions_summary": [
{
"table": "`customer`",
"attached": "(`customer`.`c_id` = 1)"
}
]
}
},
{
"optimizing_distinct_group_by_order_by": {
"changed_distinct_to_group_by": true,
"simplifying_group_by": {
"original_clause": "`customer`.`c_discount`",
"items": [
{
"item": "`customer`.`c_discount`"
}
],
"resulting_clause_is_simple": true,
"resulting_clause": "`customer`.`c_discount`"
}
}
},
{
"refine_plan": [
{
"table": "`customer`"
}
]
},
{
"considering_tmp_tables": [
{
"adding_tmp_table_in_plan_at_position": 1,
"write_method": "write_all_rows"
}
]
}
]
}
},
{
"join_explain": {
"select#": 1,
"steps": [
]
}
}
]
}
在"select distinct c_discount from customer where c_id = 1;"中多出optimizing_distinct_group_by_order_by,
经过多次运行后,distinct语句略高于原语句的运行时间;
ps:在300000行数据的情况下
optimizer_trace对比
{
"steps": [
{
"join_preparation": {
"select#": 1,
"steps": [
{
"expanded_query": "/* select#1 */ select `customer`.`c_discount` AS `c_discount` from `customer` where (`customer`.`c_w_id` = 1)"
}
]
}
},
{
"join_optimization": {
"select#": 1,
"steps": [
{
"condition_processing": {
"condition": "WHERE",
"original_condition": "(`customer`.`c_w_id` = 1)",
"steps": [
{
"transformation": "equality_propagation",
"resulting_condition": "multiple equal(1, `customer`.`c_w_id`)"
},
{
"transformation": "constant_propagation",
"resulting_condition": "multiple equal(1, `customer`.`c_w_id`)"
},
{
"transformation": "trivial_condition_removal",
"resulting_condition": "multiple equal(1, `customer`.`c_w_id`)"
}
]
}
},
{
"substitute_generated_columns": {
}
},
{
"table_dependencies": [
{
"table": "`customer`",
"row_may_be_null": false,
"map_bit": 0,
"depends_on_map_bits": [
]
}
]
},
{
"ref_optimizer_key_uses": [
{
"table": "`customer`",
"field": "c_w_id",
"equals": "1",
"null_rejecting": false
}
]
},
{
"rows_estimation": [
{
"table": "`customer`",
"range_analysis": {
"table_scan": {
"rows": 289371,
"cost": 35271
},
"potential_range_indexes": [
{
"index": "PRIMARY",
"usable": true,
"key_parts": [
"c_w_id",
"c_d_id",
"c_id"
]
}
],
"setup_range_conditions": [
],
"group_index_range": {
"chosen": false,
"cause": "not_group_by_or_distinct"
},
"analyzing_range_alternatives": {
"range_scan_alternatives": [
{
"index": "PRIMARY",
"ranges": [
"1 <= c_w_id <= 1"
],
"index_dives_for_eq_ranges": true,
"rowid_ordered": true,
"using_mrr": false,
"index_only": false,
"rows": 61884,
"cost": 6261.9,
"chosen": true
}
],
"analyzing_roworder_intersect": {
"usable": false,
"cause": "too_few_roworder_scans"
}
},
"chosen_range_access_summary": {
"range_access_plan": {
"type": "range_scan",
"index": "PRIMARY",
"rows": 61884,
"ranges": [
"1 <= c_w_id <= 1"
]
},
"rows_for_plan": 61884,
"cost_for_plan": 6261.9,
"chosen": true
}
}
}
]
},
{
"considered_execution_plans": [
{
"plan_prefix": [
],
"table": "`customer`",
"best_access_path": {
"considered_access_paths": [
{
"access_type": "ref",
"index": "PRIMARY",
"rows": 61884,
"cost": 6261.8,
"chosen": true
},
{
"access_type": "range",
"range_details": {
"used_index": "PRIMARY"
},
"chosen": false,
"cause": "heuristic_index_cheaper"
}
]
},
"condition_filtering_pct": 100,
"rows_for_plan": 61884,
"cost_for_plan": 6261.8,
"chosen": true
}
]
},
{
"attaching_conditions_to_tables": {
"original_condition": "(`customer`.`c_w_id` = 1)",
"attached_conditions_computation": [
],
"attached_conditions_summary": [
{
"table": "`customer`",
"attached": null
}
]
}
},
{
"refine_plan": [
{
"table": "`customer`"
}
]
}
]
}
},
{
"join_explain": {
"select#": 1,
"steps": [
]
}
}
]
}
{
"steps": [
{
"join_preparation": {
"select#": 1,
"steps": [
{
"expanded_query": "/* select#1 */ select distinct `customer`.`c_discount` AS `c_discount` from `customer` where (`customer`.`c_w_id` = 1)"
}
]
}
},
{
"join_optimization": {
"select#": 1,
"steps": [
{
"condition_processing": {
"condition": "WHERE",
"original_condition": "(`customer`.`c_w_id` = 1)",
"steps": [
{
"transformation": "equality_propagation",
"resulting_condition": "multiple equal(1, `customer`.`c_w_id`)"
},
{
"transformation": "constant_propagation",
"resulting_condition": "multiple equal(1, `customer`.`c_w_id`)"
},
{
"transformation": "trivial_condition_removal",
"resulting_condition": "multiple equal(1, `customer`.`c_w_id`)"
}
]
}
},
{
"substitute_generated_columns": {
}
},
{
"table_dependencies": [
{
"table": "`customer`",
"row_may_be_null": false,
"map_bit": 0,
"depends_on_map_bits": [
]
}
]
},
{
"ref_optimizer_key_uses": [
{
"table": "`customer`",
"field": "c_w_id",
"equals": "1",
"null_rejecting": false
}
]
},
{
"rows_estimation": [
{
"table": "`customer`",
"range_analysis": {
"table_scan": {
"rows": 289371,
"cost": 34501
},
"potential_range_indexes": [
{
"index": "PRIMARY",
"usable": true,
"key_parts": [
"c_w_id",
"c_d_id",
"c_id"
]
}
],
"setup_range_conditions": [
],
"group_index_range": {
"distinct_query": true,
"potential_group_range_indexes": [
{
"index": "PRIMARY",
"usable": false,
"cause": "not_covering"
}
]
},
"analyzing_range_alternatives": {
"range_scan_alternatives": [
{
"index": "PRIMARY",
"ranges": [
"1 <= c_w_id <= 1"
],
"index_dives_for_eq_ranges": true,
"rowid_ordered": true,
"using_mrr": false,
"index_only": false,
"rows": 61884,
"cost": 6252.9,
"chosen": true
}
],
"analyzing_roworder_intersect": {
"usable": false,
"cause": "too_few_roworder_scans"
}
},
"chosen_range_access_summary": {
"range_access_plan": {
"type": "range_scan",
"index": "PRIMARY",
"rows": 61884,
"ranges": [
"1 <= c_w_id <= 1"
]
},
"rows_for_plan": 61884,
"cost_for_plan": 6252.9,
"chosen": true
}
}
}
]
},
{
"considered_execution_plans": [
{
"plan_prefix": [
],
"table": "`customer`",
"best_access_path": {
"considered_access_paths": [
{
"access_type": "ref",
"index": "PRIMARY",
"rows": 61884,
"cost": 6252.9,
"chosen": true
},
{
"access_type": "range",
"range_details": {
"used_index": "PRIMARY"
},
"chosen": false,
"cause": "heuristic_index_cheaper"
}
]
},
"condition_filtering_pct": 100,
"rows_for_plan": 61884,
"cost_for_plan": 6252.9,
"chosen": true
}
]
},
{
"attaching_conditions_to_tables": {
"original_condition": "(`customer`.`c_w_id` = 1)",
"attached_conditions_computation": [
],
"attached_conditions_summary": [
{
"table": "`customer`",
"attached": null
}
]
}
},
{
"optimizing_distinct_group_by_order_by": {
"changed_distinct_to_group_by": true,
"simplifying_group_by": {
"original_clause": "`customer`.`c_discount`",
"items": [
{
"item": "`customer`.`c_discount`"
}
],
"resulting_clause_is_simple": true,
"resulting_clause": "`customer`.`c_discount`"
}
}
},
{
"added_back_ref_condition": "((`customer`.`c_w_id` <=> 1))"
},
{
"refine_plan": [
{
"table": "`customer`"
}
]
},
{
"considering_tmp_tables": [
{
"adding_tmp_table_in_plan_at_position": 1,
"write_method": "write_all_rows"
}
]
}
]
}
},
{
"join_explain": {
"select#": 1,
"steps": [
]
}
}
]
}
其中distinct和原语句对比,cost不同
group_index_range对比
range_scan_alternatives、range_access_plan、considered_access_paths和cost_for_plan中的cost也不同