MySQL5.6的optimizer_trace
阿新 • • 發佈:2017-06-27
eve com size lai 語句 mit code ring comment
MySQL的explain是各種執行計劃選擇的結果,如果想看整個執行計劃以及對於多種索引方案之間是如何選擇的?
MySQL5.6中支持這個功能,optimizer_trace
這個是mysql的參數,默認是關閉的
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
mysql> set optimizer_trace= "enabled=on" ;
Query OK, 0 rows affected (0.00 sec)
mysql> show variables like ‘%optimizer_trace%‘ ;
+ ------------------------------+----------------------------------------------------------------------------+
| Variable_name | Value |
+ ------------------------------+----------------------------------------------------------------------------+ | optimizer_trace | enabled= on ,one_line= off |
| optimizer_trace_features | greedy_search= on ,range_optimizer= on ,dynamic_range= on ,repeated_subselect= on |
| optimizer_trace_limit | 1 | | optimizer_trace_max_mem_size | 16384 |
| optimizer_trace_offset | -1 |
+ ------------------------------+----------------------------------------------------------------------------+
5 rows in set (0.00 sec)
|
具體這麽使用呢?
需要設置如下:
1、開啟optimizer_trace,默認是關閉的
1 |
SET optimizer_trace= "enabled=on" ;
|
2、設置optimizer_trace內存的大小
1 |
SET OPTIMIZER_TRACE_MAX_MEM_SIZE=1000000
|
3、explain查詢語句
1 2 3 4 5 6 7 8 9 10 11 12 13 |
mysql> explain SELECT * FROM atomuser WHERE `uid` =28778731 AND ptype = "photo" LIMIT 0 , 1\G
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table : atomuser
type: ref
possible_keys: uid
key : uid
key_len: 15
ref: const,const
rows : 1
Extra: Using index condition
1 row in set (0.01 sec)
|
4、查找對於的結果
1 |
mysql> select * from information_schema.optimizer_trace\G
|
這個表包括4個字段
1 2 3 4 5 6 7 8 9 |
mysql> show create table information_schema.optimizer_trace\G
*************************** 1. row ***************************
Table : OPTIMIZER_TRACE
Create Table : CREATE TEMPORARY TABLE `OPTIMIZER_TRACE` (
`QUERY` longtext NOT NULL ,
`TRACE` longtext NOT NULL ,
`MISSING_BYTES_BEYOND_MAX_MEM_SIZE` int (20) NOT NULL DEFAULT ‘0‘ ,
`INSUFFICIENT_PRIVILEGES` tinyint(1) NOT NULL DEFAULT ‘0‘
) ENGINE=MyISAM DEFAULT CHARSET=utf8
|
主要看TRACE字段,是json串,json解析結果如下:
包括join_preparation,join_optimization,join_explain
join_preparation
1 |
"/* select#1 */ select `atomuser`.`id` AS `id`,`atomuser`.`uid` AS `uid`,`atomuser`.`ptype` AS `ptype`,`atomuser`.`regtime` AS `regtime` from `atomuser` where ((`atomuser`.`uid` = 28778731) and (`atomuser`.`ptype` = ‘photo‘)) limit 0,1"
|
這個是使用extend看到的結果
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
mysql> explain extended SELECT * FROM atomuser WHERE `uid` =28778731 AND ptype = "photo" LIMIT 0 , 1\G
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table : atomuser
type: ref
possible_keys: uid
key : uid
key_len: 15
ref: const,const
rows : 1
filtered: 100.00
Extra: Using index condition
1 row in set , 1 warning (0.01 sec)
mysql> show warnings\G
*************************** 1. row ***************************
Level : Note
Code: 1003
Message: /* select#1 */ select `test`.`atomuser`.`id` AS `id`,`test`.`atomuser`.`uid` AS `uid`,`test`.`atomuser`.`ptype` AS `ptype`,`test`.`atomuser`.`regtime` AS `regtime` from `test`.`atomuser` where ((`test`.`atomuser`.`ptype` = ‘photo‘ ) and (`test`.`atomuser`.`uid` = 28778731)) limit 0,1
1 row in set (0.00 sec)
|
[參考資料]
1、http://guilhembichot.blogspot.com/2011/09/optimizer-tracing-how-to-configure-it.html
MySQL5.6的optimizer_trace