• MySQL5.6的optimizer_trace


    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                                                                         |
    +------------------------------+----------------------------------------------------------------------------+
    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 , 1G
    *************************** 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_traceG

     

    这个表包括4个字段 

    1
    2
    3
    4
    5
    6
    7
    8
    9
    mysql> show create table information_schema.optimizer_traceG
    *************************** 1. row ***************************
           Table: OPTIMIZER_TRACE
    Create TableCREATE 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 , 1G
    *************************** 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 warningsG
    *************************** 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

  • 相关阅读:
    采用多种算法,模拟摇奖:从1-36中随机抽出8个不重复的数字
    有一百匹马,驮一百担货,大马驮3担,中马驮2担,两只小马驮1担,问有大,中,小马各几匹?
    5文钱可以买一只公鸡,3文钱可以买一只母鸡,1文钱可以买3只雏鸡。现在用100文钱买100只鸡,那么各有公鸡、母鸡、雏鸡多少只?请编写程序实现。
    集合相关题目0927
    输入输出作业
    IO流,File类的测试........课堂加总结
    使用泛型............课堂
    Map 映射
    Set
    List相关知识点.......课堂加整理
  • 原文地址:https://www.cnblogs.com/mydriverc/p/7086531.html
Copyright © 2020-2023  润新知