当前位置: 首页 > news >正文

Mysql之 optimizer_trace 相关总结

Mysql之 optimizer_trace 相关总结

MySQL官网介绍:https://dev.mysql.com/doc/dev/mysql-server/latest/PAGE_OPT_TRACE.html


1. 简介

MySQL优化器可以生成Explain执行计划,通过执行计划查看sql是否使用了索引,使用了哪种索;
但是有些时候,你会发现为什么没想按照我们所想的思路执行:
为什么会使用这个索引 ?!
为什么没有使用添加的索引 ?!

于是,MySQL5.6版本之后开始引入 optimizer trace(优化器追踪),它可以查看优化器生成执行计划的整个过程,以及做出的各种决策,包括访问表的方法、各种开销计算、各种转换等等,帮助我们更好的去优化sql。

另外,optimizer_trace的开关默认是关闭的 ,开启trace工具会影响mysql性能,所以只适合临时分析sql使用,用完之后最好及时关闭。


2. 使用方法

1. 查看optimizer trace配置

show variables like '%optimizer_trace%';

查询结果:

在这里插入图片描述
查询结果字段说明:

  • optimizer_trace: 主配置,enabled的on表示开启,off表示关闭,one_line表示是否展示成一行
  • optimizer_trace_features: 表示优化器的可选特性,包括贪心搜索、范围优化等
  • optimizer_trace_limit: 表示优化器追踪最大显示数目,默认是1条
  • optimizer_trace_max_mem_size: 表示优化器追踪占用的最大容量
  • optimizer_trace_offset: 表示显示的第一个优化器追踪的偏移量

2. 开启/关闭 optimizer trace

#开启trace
set session optimizer_trace="enabled=on",end_markers_in_json=on;
#关闭trace
set session optimizer_trace="enabled=off";

3. 执行需要进行分析的SQL语句

select * from test0816 where name > 'a' order by remark;

4. 使用optimizer trace查看优化器的选择过程

SELECT * FROM information_schema.OPTIMIZER_TRACE;

查询结果:
在这里插入图片描述
查询结果对应字段说明:

  • QUERY: 表示我们执行的查询语句
  • TRACE: 优化器生成执行计划的过程(重点关注)
  • MISSING_BYTES_BEYOND_MAX_MEM_SIZE: 优化过程其余的信息会被显示在这一列
  • INSUFFICIENT_PRIVILEGES: 表示是否有权限查看优化过程,0是,1否

5. 分析

trace的内容:

{"steps": [{"join_preparation": {"select#": 1,"steps": [{"expanded_query": "/* select#1 */ select `test0816`.`id` AS `id`,`test0816`.`name` AS `name`,`test0816`.`age` AS `age`,`test0816`.`remark` AS `remark`,`test0816`.`create_time` AS `create_time` from `test0816` where (`test0816`.`name` > 'a') order by `test0816`.`remark`"}] /* steps */} /* join_preparation */},{"join_optimization": {"select#": 1,"steps": [{"condition_processing": {"condition": "WHERE","original_condition": "(`test0816`.`name` > 'a')","steps": [{"transformation": "equality_propagation","resulting_condition": "(`test0816`.`name` > 'a')"},{"transformation": "constant_propagation","resulting_condition": "(`test0816`.`name` > 'a')"},{"transformation": "trivial_condition_removal","resulting_condition": "(`test0816`.`name` > 'a')"}] /* steps */} /* condition_processing */},{"substitute_generated_columns": {} /* substitute_generated_columns */},{"table_dependencies": [{"table": "`test0816`","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": "`test0816`","range_analysis": {"table_scan": {"rows": 3,"cost": 2.65} /* table_scan */,"potential_range_indexes": [{"index": "PRIMARY","usable": false,"cause": "not_applicable"},{"index": "idx_name_age_remark","usable": true,"key_parts": ["name","age","remark","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": "idx_name_age_remark","usable": false,"cause": "query_references_nonkey_column"}] /* potential_skip_scan_indexes */} /* skip_scan_range */,"analyzing_range_alternatives": {"range_scan_alternatives": [{"index": "idx_name_age_remark","ranges": ["a < name"] /* ranges */,"index_dives_for_eq_ranges": true,"rowid_ordered": false,"using_mrr": false,"index_only": false,"in_memory": 1,"rows": 3,"cost": 1.31,"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_remark","rows": 3,"ranges": ["a < name"] /* ranges */} /* range_access_plan */,"rows_for_plan": 3,"cost_for_plan": 1.31,"chosen": true} /* chosen_range_access_summary */} /* range_analysis */}] /* rows_estimation */},{"considered_execution_plans": [{"plan_prefix": [] /* plan_prefix */,"table": "`test0816`","best_access_path": {"considered_access_paths": [{"rows_to_scan": 3,"access_type": "range","range_details": {"used_index": "idx_name_age_remark"} /* range_details */,"resulting_rows": 3,"cost": 1.61,"chosen": true,"use_tmp_table": true}] /* considered_access_paths */} /* best_access_path */,"condition_filtering_pct": 100,"rows_for_plan": 3,"cost_for_plan": 1.61,"sort_cost": 3,"new_cost_for_plan": 4.61,"chosen": true}] /* considered_execution_plans */},{"attaching_conditions_to_tables": {"original_condition": "(`test0816`.`name` > 'a')","attached_conditions_computation": [] /* attached_conditions_computation */,"attached_conditions_summary": [{"table": "`test0816`","attached": "(`test0816`.`name` > 'a')"}] /* attached_conditions_summary */} /* attaching_conditions_to_tables */},{"optimizing_distinct_group_by_order_by": {"simplifying_order_by": {"original_clause": "`test0816`.`remark`","items": [{"item": "`test0816`.`remark`"}] /* items */,"resulting_clause_is_simple": true,"resulting_clause": "`test0816`.`remark`"} /* simplifying_order_by */} /* optimizing_distinct_group_by_order_by */},{"reconsidering_access_paths_for_index_ordering": {"clause": "ORDER BY","steps": [] /* steps */,"index_order_summary": {"table": "`test0816`","index_provides_order": false,"order_direction": "undefined","index": "idx_name_age_remark","plan_changed": false} /* index_order_summary */} /* reconsidering_access_paths_for_index_ordering */},{"finalizing_table_conditions": [{"table": "`test0816`","original_table_condition": "(`test0816`.`name` > 'a')","final_table_condition   ": "(`test0816`.`name` > 'a')"}] /* finalizing_table_conditions */},{"refine_plan": [{"table": "`test0816`","pushed_index_condition": "(`test0816`.`name` > 'a')","table_condition_attached": null}] /* refine_plan */},{"considering_tmp_tables": [{"adding_sort_to_table": "test0816"} /* filesort */] /* considering_tmp_tables */}] /* steps */} /* join_optimization */},{"join_execution": {"select#": 1,"steps": [{"sorting_table": "test0816","filesort_information": [{"direction": "asc","expression": "`test0816`.`remark`"}] /* 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": 400,"row_size": 1091,"max_rows_per_buffer": 15,"num_rows_estimate": 15,"num_rows_found": 3,"num_initial_chunks_spilled_to_disk": 0,"peak_memory_used": 32800,"sort_algorithm": "std::sort","sort_mode": "<fixed_sort_key, packed_additional_fields>"} /* filesort_summary */}] /* steps */} /* join_execution */}] /* steps */
}

一共是3个阶段:

  • join_preparation:sql准备阶段,sql格式化;
  • join_optimization: sql分析优化阶段,是分析OPTIMIZER TRACE的重点。这段一般都比较长,分很多步,需要细看;
  • join_execution: sql执行阶段;

其中的相关关键字解析:
//TODO

结论:全表扫描的成本低于索引扫描,所以MySQL最终选择全表扫描。

http://www.lryc.cn/news/126899.html

相关文章:

  • 【Linux命令详解 | wget命令】 wget命令用于从网络下载文件,支持HTTP、HTTPS和FTP协议
  • DockePod信号处理机制与僵尸进程优化
  • NetApp StorageGRID 对象存储,使您能够跨公有、私有云和混合多云环境管理非结构化数据
  • 使用Java服务器实现UDP消息的发送和接收(多线程)
  • Linux--查看端口占用情况
  • 微信小程序|自定义弹窗组件
  • 【数据结构】实现顺序表
  • 【嵌入式环境下linux内核及驱动学习笔记-(19)LCD驱动框架2-FrameBuffer】
  • 自己动手写数据库系统:实现一个小型SQL解释器(中)
  • HTML 与 XHTML 二者有什么区别
  • fiddler抓包问题记录,支持https、解决 tunnel to 443
  • Kubesphere中DevOps流水线无法部署/部署失败
  • 使用Nginx解决跨域问题
  • 在 OpenCV 中使用深度学习进行年龄检测-附源码
  • 【BASH】回顾与知识点梳理(三十一)
  • Linux 终端命令之文件浏览(3) less
  • 【精通性能优化:解锁JMH微基准测试】一基本用法
  • .Net程序调试时接受外部命令行参数方式
  • Mariadb高可用MHA (四十二)
  • Vue3 setup中使用$refs
  • 什么是React的上下文(Context)?如何使用和传递上下文信息?
  • CentOS Linux 78安全基线检查
  • Java之SpringCloud Alibaba【四】【微服务 Sentinel服务熔断】
  • Kubernetes 企业级高可用部署
  • 8.1 C++ STL 变易拷贝算法
  • 攻击LNMP架构Web应用
  • 深度学习入门-3-计算机视觉-图像分类
  • shopee运营新手入门教程!Shopee运营技巧!
  • Python Web框架:Django、Flask和FastAPI巅峰对决
  • 机器学习线性代数基础