mysql优化追踪示例
# 示例
```sql
-- 查看优化器状态
show variables like 'optimizer_trace';
-- 会话级别临时开启
set session optimizer_trace="enabled=on",end_markers_in_json=on;
-- 设置优化器追踪的内存大小
set OPTIMIZER_TRACE_MAX_MEM_SIZE=1000000;
-- 执行自己的SQL
SELECT * FROM t_daf_im_bills;
SELECT trace FROM information_schema.OPTIMIZER_TRACE;
--关闭跟踪
SET session optimizer_trace="enabled=off";
```
执行上述语句,把结果三输出的json文件复制到josn格式化工具里格式化一下
![image.png](https://cos.easydoc.net/44929674/files/llblmrup.png)
则可以查看到sql执行的详细情况,进行针对性的优化
![image.png](https://cos.easydoc.net/44929674/files/llblt5gs.png)
![image.png](https://cos.easydoc.net/44929674/files/llbltnuy.png)