1. 程式人生 > >通過trace分析優化器如何選擇執行計劃

通過trace分析優化器如何選擇執行計劃

col get sel 選擇 line info quic mys max

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_TRACE\G;
*************************** 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分析優化器如何選擇執行計劃