1 MySQL(十五)分析优化器的查询计划:Trace
OPTIMIZER_TRACE
是mysql 5.6引入的一项追踪功能,它可以追踪优化器做出的各种决策(比如访问表的方法、各种开销计算和各种转换等等),并将结果记录到表INFORMATION_SCHEMA.OPTIMIZER_TRACE
表中。
Trace功能默认是关闭的,需要开启trace,设置JSON格式,并设置最大能使用内存为1000000,防止使用过程中因内存过小而不能够使用
SET optimizer_trace = 'enabled=on', end_markers_in_json=on;
set optimizer_trace_max_mem_size=1000000;
测试 并查看对应计划
SELECT * FROM student WHERE id < 2042366;
mysql> SELECT * FROM information_schema.OPTIMIZER_TRACE\G;
*************************** 1. row ***************************
# 第一部分:查询的SQL
QUERY: SELECT * FROM student WHERE id < 2042366
# 第二部分:追踪的信息
TRACE: {
"steps": [
{
"join_preparation": {
"select#": 1,
"steps": [
{
"expanded_query": "/* select#1 */ select `student`.`id` AS `id`,`student`.`stuno` AS `stuno`,`student`.`name` AS `name`,`student`.`age` AS `age`,`student`.`classid` AS `classid` from `student` where (`student`.`id` < 2042366)"
}
] /* steps */
} /* join_preparation */
},
{
"join_optimization": {
"select#": 1,
"steps": [
{
"condition_processing": {
"condition": "WHERE",
"original_condition": "(`student`.`id` < 2042366)",
"steps": [
{
"transformation": "equality_propagation",
"resulting_condition": "(`student`.`id` < 2042366)"
},
{
"transformation": "constant_propagation",
"resulting_condition": "(`student`.`id` < 2042366)"
},
{
"transformation": "trivial_condition_removal",
"resulting_condition": "(`student`.`id` < 2042366)"
}
] /* steps */
} /* condition_processing */
},
{
"substitute_generated_columns": {
} /* substitute_generated_columns */
},
{
"table_dependencies": [
{
"table": "`student`",
"row_may_be_null": false,
"map_bit": 0,
"depends_on_map_bits": [
] /* depends_on_map_bits */
}
] /* table_dependencies */
},
{
"ref_optimizer_key_uses": [
] /* ref_optimizer_key_uses */
},
{
"rows_estimation": [
{
"table": "`student`",
"range_analysis": {
"table_scan": {
"rows": 3989929,
"cost": 406388
} /* table_scan */,
"potential_range_indexes": [
{
"index": "PRIMARY",
"usable": true,
"key_parts": [
"id"
] /* key_parts */
}
] /* 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": "PRIMARY",
"usable": false,
"cause": "query_references_nonkey_column"
}
] /* potential_skip_scan_indexes */
} /* skip_scan_range */,
"analyzing_range_alternatives": {
"range_scan_alternatives": [
{
"index": "PRIMARY",
"ranges": [
"id < 2042366"
] /* ranges */,
"index_dives_for_eq_ranges": true,
"rowid_ordered": true,
"using_mrr": false,
"index_only": false,
"in_memory": 0.386539,
"rows": 19,
"cost": 2.635,
"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": "PRIMARY",
"rows": 19,
"ranges": [
"id < 2042366"
] /* ranges */
} /* range_access_plan */,
"rows_for_plan": 19,
"cost_for_plan": 2.635,
"chosen": true
} /* chosen_range_access_summary */
} /* range_analysis */
}
] /* rows_estimation */
},
{
"considered_execution_plans": [
{
"plan_prefix": [
] /* plan_prefix */,
"table": "`student`",
"best_access_path": {
"considered_access_paths": [
{
"rows_to_scan": 19,
"access_type": "range",
"range_details": {
"used_index": "PRIMARY"
} /* range_details */,
"resulting_rows": 19,
"cost": 4.535,
"chosen": true
}
] /* considered_access_paths */
} /* best_access_path */,
"condition_filtering_pct": 100,
"rows_for_plan": 19,
"cost_for_plan": 4.535,
"chosen": true
}
] /* considered_execution_plans */
},
{
"attaching_conditions_to_tables": {
"original_condition": "(`student`.`id` < 2042366)",
"attached_conditions_computation": [
] /* attached_conditions_computation */,
"attached_conditions_summary": [
{
"table": "`student`",
"attached": "(`student`.`id` < 2042366)"
}
] /* attached_conditions_summary */
} /* attaching_conditions_to_tables */
},
{
"finalizing_table_conditions": [
{
"table": "`student`",
"original_table_condition": "(`student`.`id` < 2042366)",
"final_table_condition ": "(`student`.`id` < 2042366)"
}
] /* finalizing_table_conditions */
},
{
"refine_plan": [
{
"table": "`student`"
}
] /* 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)
2 mysql监控分析视图-sys schema
索引情况
-- 查询冗余索引
SELECT * FROM sys.schema_redundant_indexes;
-- 查询未使用过的索引
SELECT * FROM sys.schema_unused_indexes;
-- 查询索引的使用情况
SELECT index_name, rows_selected, rows_inserted, rows_updated, rows_deleted
FROM sys.schema_index_statistics
WHERE table_schema = 'atguigudb1'
表情况
-- 查询表的访问量
SELECT table_schema, table_name, SUM(io_read_requests + io_write_requests) AS io
FROM sys.schema_table_statistics GROUP BY table_schema, table_name ORDER BY io;
-- 查询占用 buffer pool较多的表
SELECT object_schema, object_name, allocated, DATA
FROM sys.innodb_buffer_stats_by_table ORDER BY allocated LIMIT 10;
-- 查看表的全表扫描情况
SELECT * FROM sys.statements_with_full_table_scans WHERE db='atguigudb1'
语句相关
-- 监控SQL执行的频率
SELECT db, exec_count, query from sys.statement_analysis
ORDER BY exec_count DESC
-- 监控使用了排序的SQL
SELECT db, exec_count, first_seen, last_seen, query
FROM sys.statements_with_sorting limit 1
-- 监控使用了临时表或者磁盘临时表的SQL
SELECT db, exec_count, tmp_tables, tmp_disk_tables, query
from sys.statement_analysis
WHERE tmp_tables > 0 or tmp_disk_tables > 0
ORDER BY (tmp_tables + tmp_disk_tables) DESC
注意
标签:rows,Trace,查询,range,student,MySQL,table,2042366,id From: https://www.cnblogs.com/tod4/p/17561843.html通过sys库去查询的时候,mysql会耗费大量的资源去收集信息,严重的情况可能会导致业务请求被阻塞,从而引起故障,生产的时候尽量不要去频繁查看sys或者performance_schema、information_schema来完成监控、巡检等工作