通过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)

原文地址:https://www.cnblogs.com/chinaops/p/10373687.html