# tract分析优化器执行计划
MySQL5.6提供了对SQL的跟踪trace, 通过trace文件能够进一步了解为什么优化器选择A计划, 而不是选择B计划。
## 1.打开trace
打开trace , 设置格式为 JSON,并设置trace最大能够使用的内存大小,避免解析过程中因为默认内存过小而不能够完整展示。
~~~
SET optimizer_trace="enabled=on",end_markers_in_json=on;
set optimizer_trace_max_mem_size=1000000;
~~~
**示例1:**
~~~
mysql> select * from city_innodb;
+---------+-----------+------------+
| city_id | city_name | country_id |
+---------+-----------+------------+
| 1 | Xian | 10 |
| 2 | NewYork | 2 |
| 3 | BeiJing | 10 |
+---------+-----------+------------+
3 rows in set (0.00 sec)
mysql> select * from information_schema.optimizer_trace\G;
*************************** 1. row ***************************
QUERY: select * from city_innodb
TRACE: {
"steps": [
{
"join_preparation": {
"select#": 1,
"steps": [
{
"expanded_query": "/* select#1 */ select `city_innodb`.`city_id` AS `city_id`,`city_innodb`.`city_name` AS `city_name`,`city_innodb`.`country_id` AS `country_id` from `city_innodb`"
}
] /* steps */
} /* join_preparation */
},
{
"join_optimization": {
"select#": 1,
"steps": [
{
"table_dependencies": [
{
"table": "`city_innodb`",
"row_may_be_null": false,
"map_bit": 0,
"depends_on_map_bits": [
] /* depends_on_map_bits */
}
] /* table_dependencies */
},
{
"rows_estimation": [
{
"table": "`city_innodb`",
"table_scan": {
"rows": 3,
"cost": 0.25
} /* table_scan */
}
] /* rows_estimation */
},
{
"considered_execution_plans": [
{
"plan_prefix": [
] /* plan_prefix */,
"table": "`city_innodb`",
"best_access_path": {
"considered_access_paths": [
{
"rows_to_scan": 3,
"access_type": "scan",
"resulting_rows": 3,
"cost": 0.55,
"chosen": true
}
] /* considered_access_paths */
} /* best_access_path */,
"condition_filtering_pct": 100,
"rows_for_plan": 3,
"cost_for_plan": 0.55,
"chosen": true
}
] /* considered_execution_plans */
},
{
"attaching_conditions_to_tables": {
"original_condition": null,
"attached_conditions_computation": [
] /* attached_conditions_computation */,
"attached_conditions_summary": [
{
"table": "`city_innodb`",
"attached": null
}
] /* attached_conditions_summary */
} /* attaching_conditions_to_tables */
},
{
"finalizing_table_conditions": [
] /* finalizing_table_conditions */
},
{
"refine_plan": [
{
"table": "`city_innodb`"
}
] /* 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
~~~
- 鸣谢
- 安装和配置
- Mac 安装MySql 8
- 授权用户远程登录
- MySql 命令加入系统命令
- 启动Mysql
- 索引
- 索引介绍与优势
- 索引结构
- 索引操作语法
- 索引设计原则
- 存储引擎
- MySql的体系解构
- 存储引擎
- 各种存储引擎特性
- 存储引擎的选择
- 优化SQL
- 查看SQL执行效率
- 定为低效率执行SQL
- explain分析执行计划
- show profile分析SQL
- tract 分析优化器执行计划
- 索引的使用
- 验证索引可以提高查询效率
- 索引使用
- 查看索引的使用情况
- SQL优化
- 大批量插入数据
- 优化insert语句
- 优化order by语句
- 优化group by的优化
- 优化嵌套索引
- 优化OR条件
- 优化分页查询
- 使用SQL提示