通过trace分析优化器如何选择执行计划

Posted chinaops

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了通过trace分析优化器如何选择执行计划相关的知识,希望对你有一定的参考价值。

1.

mysql> show variables like "optimizer_trace%"G;
*************************** 1. row ***************************
Variable_name: optimizer_trace
Value: enabled=off,one_line=off
*************************** 2. row ***************************
Variable_name: optimizer_trace_features
Value: greedy_search=on,range_optimizer=on,dynamic_range=on,repeated_subselect=on
*************************** 3. row ***************************
Variable_name: optimizer_trace_limit
Value: 1
*************************** 4. row ***************************
Variable_name: optimizer_trace_max_mem_size
Value: 16384
*************************** 5. row ***************************
Variable_name: optimizer_trace_offset
Value: -1
5 rows in set (0.01 sec)

2.打开trace,设置格式为json,设置trace最大能够使用的内存大小,避免解析过程中因为默认内存过小而不能够完整显示。

mysql> use sakila
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> select rental_id from rental where 1=1 and rental_date >=‘2005-05-25 04:00:00‘ and rental_date <=‘2005-05-25 05:00:00‘ and inventory_id=4466;
+-----------+
| rental_id |
+-----------+
| 39 |
+-----------+
1 row in set (0.00 sec)

3.检查MySQL是如何执行SQL的。

mysql> use information_schema 

mysql> select * from OPTIMIZER_TRACEG;
*************************** 1. row ***************************
QUERY: SELECT DATABASE()
TRACE: {
"steps": [
{
"join_preparation": {
"select#": 1,
"steps": [
{
"expanded_query": "/* select#1 */ select database() AS `DATABASE()`"
}
] /* steps */
} /* join_preparation */
},
{
"join_optimization": {
"select#": 1,
"steps": [
] /* 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)



























































以上是关于通过trace分析优化器如何选择执行计划的主要内容,如果未能解决你的问题,请参考以下文章

MySQL SQL trace

通过分析SQL语句的执行计划优化SQL

mysql的优化措施,从sql优化做起

mysql中如何查看优化器优化后的执行计划

Explain 执行计划 和 SQL优化

mysql Tracing the Optimizer