一、表字段
二、索引状态
三、语句执行计划
四、所走索引大图
疑惑,优化器为什么不选择新建的idx_courseId索引呢,
开启mysql optimizer_trace,追踪下
mysql> SHOW variables like 'optimizer_trace';
+-----------------+--------------------------+
| Variable_name | Value |
+-----------------+--------------------------+
| optimizer_trace | enabled=off,one_line=off |
+-----------------+--------------------------+
1 row in set (0.00 sec)
mysql> set optimizer_trace='enabled=on';
Query OK, 0 rows affected (0.00 sec)
mysql>select count(*)from t_person_clazz_cheat_record where person_id=13053411;
mysql>select * from information_schema.optimizer_trace\G;
mysql> select * from information_schema.optimizer_trace\G;
*************************** 1. row ***************************
QUERY: select count(*)from t_person_clazz_cheat_record where person_id=13053411
TRACE: {
"steps": [
{
"join_preparation": {
"select#": 1,
"steps": [
{
"expanded_query": "/* select#1 */ select count(0) AS `count(*)` from `t_person_clazz_cheat_record` where (`t_person_clazz_cheat_record`.`person_id` = 13053411)"
}
] /* steps */
} /* join_preparation */
},
{
"join_optimization": {
"select#": 1,
"steps": [
{
"condition_processing": {
"condition": "WHERE",
"original_condition": "(`t_person_clazz_cheat_record`.`person_id` = 13053411)",
"steps": [
{
"transformation": "equality_propagation",
"resulting_condition": "multiple equal(13053411, `t_person_clazz_cheat_record`.`person_id`)"
},
{
"transformation": "constant_propagation",
"resulting_condition": "multiple equal(13053411, `t_person_clazz_cheat_record`.`person_id`)"
},
{
"transformation": "trivial_condition_removal",
"resulting_condition": "multiple equal(13053411, `t_person_clazz_cheat_record`.`person_id`)"
}
] /* steps */
} /* condition_processing */
},
{
"table_dependencies": [
{
"table": "`t_person_clazz_cheat_record`",
"row_may_be_null": false,
"map_bit": 0,
"depends_on_map_bits": [
] /* depends_on_map_bits */
}
] /* table_dependencies */
},
{
"ref_optimizer_key_uses": [
{
"table": "`t_person_clazz_cheat_record`",
"field": "person_id",
"equals": "13053411",
"null_rejecting": false
},
{
"table": "`t_person_clazz_cheat_record`",
"field": "person_id",
"equals": "13053411",
"null_rejecting": false
},
{
"table": "`t_person_clazz_cheat_record`",
"field": "person_id",
"equals": "13053411",
"null_rejecting": false
}
] /* ref_optimizer_key_uses */
},
{
"rows_estimation": [
{
"table": "`t_person_clazz_cheat_record`",
"range_analysis": {
"table_scan": {
"rows": 1746512,
"cost": 356768
} /* table_scan */,
"potential_range_indices": [
{
"index": "PRIMARY",
"usable": false,
"cause": "not_applicable"
},
{
"index": "idx_personid_clazzid_dtype",
"usable": true,
"key_parts": [
"person_id",
"clazz_id",
"dtype"
] /* key_parts */
},
{
"index": "idx_fid_dtype",
"usable": false,
"cause": "not_applicable"
},
{
"index": "idx_courseId",
"usable": false,
"cause": "not_applicable"
},
{
"index": "idx_personid",
"usable": true,
"key_parts": [
"person_id",
"id"
] /* key_parts */
},
{
"index": "idx_person_id_status",
"usable": true,
"key_parts": [
"person_id",
"status",
"id"
] /* key_parts */
}
] /* potential_range_indices */,
"best_covering_index_scan": {
"index": "idx_personid",
"cost": 351861,
"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 */,
"analyzing_range_alternatives": {
"range_scan_alternatives": [
{
"index": "idx_personid_clazzid_dtype",
"ranges": [
"13053411 <= person_id <= 13053411"
] /* ranges */,
"index_dives_for_eq_ranges": true,
"rowid_ordered": false,
"using_mrr": false,
"index_only": true,
"rows": 1,
"cost": 2.21,
"chosen": true
},
{
"index": "idx_personid",
"ranges": [
"13053411 <= person_id <= 13053411"
] /* ranges */,
"index_dives_for_eq_ranges": true,
"rowid_ordered": true,
"using_mrr": false,
"index_only": true,
"rows": 1,
"cost": 2.21,
"chosen": false,
"cause": "cost"
},
{
"index": "idx_person_id_status",
"ranges": [
"13053411 <= person_id <= 13053411"
] /* ranges */,
"index_dives_for_eq_ranges": true,
"rowid_ordered": false,
"using_mrr": false,
"index_only": true,
"rows": 1,
"cost": 2.21,
"chosen": false,
"cause": "cost"
}
] /* 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_personid_clazzid_dtype",
"rows": 1,
"ranges": [
"13053411 <= person_id <= 13053411"
] /* ranges */
} /* range_access_plan */,
"rows_for_plan": 1,
"cost_for_plan": 2.21,
"chosen": true
} /* chosen_range_access_summary */
} /* range_analysis */
}
] /* rows_estimation */
},
{
"considered_execution_plans": [
{
"plan_prefix": [
] /* plan_prefix */,
"table": "`t_person_clazz_cheat_record`",
"best_access_path": {
"considered_access_paths": [
{
"access_type": "ref",
"index": "idx_personid_clazzid_dtype",
"rows": 1,
"cost": 1.2,
"chosen": true
},
{
"access_type": "ref",
"index": "idx_personid",
"rows": 1,
"cost": 1.2,
"chosen": false
},
{
"access_type": "ref",
"index": "idx_person_id_status",
"rows": 1,
"cost": 1.2,
"chosen": false
},
{
"access_type": "range",
"cause": "heuristic_index_cheaper",
"chosen": false
}
] /* considered_access_paths */
} /* best_access_path */,
"cost_for_plan": 1.2,
"rows_for_plan": 1,
"chosen": true
}
] /* considered_execution_plans */
},
{
"attaching_conditions_to_tables": {
"original_condition": "(`t_person_clazz_cheat_record`.`person_id` = 13053411)",
"attached_conditions_computation": [
] /* attached_conditions_computation */,
"attached_conditions_summary": [
{
"table": "`t_person_clazz_cheat_record`",
"attached": null
}
] /* attached_conditions_summary */
} /* attaching_conditions_to_tables */
},
{
"refine_plan": [
{
"table": "`t_person_clazz_cheat_record`"
}
] /* refine_plan */
}
] /* steps */
} /* join_optimization */
},
{
"join_execution": {
"select#": 1,
"steps": [
] /* steps */
} /* join_execution */
}
] /* steps */
}
MISSING_BYTES_BEYOND_MAX_MEM_SIZE: 0
INSUFFICIENT_PRIVILEGES: 0
1 row in set (0.00 sec)
ERROR:
No query specified
总结:mysql优化器选择索引的时候,在cost花费一样的时候是有选择顺序的,主键>唯一索引>旧normal索引>新normal索引