zoukankan      html  css  js  c++  java
  • mysql explain中的 “Select tables optimized away”

    mysql explain中的 “Select tables optimized away”

    http://blog.chinaunix.net/uid-10449864-id-2956845.html
    2009年


    今天在做SQL语句优化的时候,在explain的时候,有这样一个提示:

    mysql> explain SELECT max( up_start ) AS up_start FROM test WHERE up_start > '2008-01-19 00:00:00' and up_start < '2008-01-19 23:59:59';
    +----+-------------+-------+------+---------------+------+---------+------+------+------------------------------+
    | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
    +----+-------------+-------+------+---------------+------+---------+------+------+------------------------------+
    | 1 | SIMPLE | NULL | NULL | NULL | NULL | NULL | NULL | NULL | Select tables optimized away |
    +----+-------------+-------+------+---------------+------+---------+------+------+------------------------------+
    1 row in set (0.00 sec)

    我们还可以做这样一个测试:
    用一个innodb表和一个myisam表进行select count(*)测试:

    myisam表测试
    mysql> explain select count(*) from myisam;
    +----+-------------+-------+------+---------------+------+---------+------+------+------------------------------+
    | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
    +----+-------------+-------+------+---------------+------+---------+------+------+------------------------------+
    | 1 | SIMPLE | NULL | NULL | NULL | NULL | NULL | NULL | NULL | Select tables optimized away |
    +----+-------------+-------+------+---------------+------+---------+------+------+------------------------------+
    1 row in set (0.00 sec)


    innodb表测试
    mysql> explain select count(*) from innodb;
    +----+-------------+-----------+-------+---------------+---------+---------+------+------+-------------+
    | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
    +----+-------------+-----------+-------+---------------+---------+---------+------+------+-------------+
    | 1 | SIMPLE | innodb | index | NULL | PRIMARY | 4 | NULL | 4 | Using index |
    +----+-------------+-----------+-------+---------------+---------+---------+------+------+-------------+
    1 row in set (0.00 sec)

    这2个输出的结果里,Extra列输出了"Select tables optimized away"语句。
    第2个很明显,myisam已经保存了记录的总数,直接返回结果,,而innodb还需要全表扫描。

    这个在MySQL的手册里面没有任何提及,不过看其他各列的数据大概能猜到意思:SELECT操作已经优化到不能再优化了(MySQL根本没有遍历表或索引就返回数据了)。

    在MySQL官方站点翻到两段相关的描述,印证了上述观点,原文如下:
    For explains on simple count queries (i.e. explain select count(*) from people) the extra section will read "Select tables optimized away." This is due to the fact that MySQL can read the result directly from the table internals and therefore does not need to perform the select.


    官方地址如下:
    http://mysql2.mirrors-r-us.net/doc/refman/5.0/en/explain.html

  • 相关阅读:
    十七、springboot配置FastJson为Spring Boot默认JSON解析框架
    十六、springboot整合Spring-data-jpa(二)之通用DAO接口与添加自定义方法
    十五、springboot集成定时任务(Scheduling Tasks)(二)之(线程配置)
    JavaBean的实用工具Lombok(省去get、set等方法)
    十四、springboot全局处理异常(@ControllerAdvice + @ExceptionHandler)
    项目中遇到的问题:Gradle传递性依赖冲突
    hibernate的枚举注解@Enumerated
    Sublime Text 3 注册码失效(被移除)解决方法
    SpringBoot整合dubbo
    Intellij IDEA实现SpringBoot项目多端口启动
  • 原文地址:https://www.cnblogs.com/MYSQLZOUQI/p/7021732.html
Copyright © 2011-2022 走看看