mysql explain中的 “Select tables optimized away”

本文详细解读了SQL查询中,尤其是COUNT(*)操作在InnoDB和MyISAM表上的性能差异,以及explain命令中'Optimized Away'的含义。通过实例说明了InnoDB使用索引直接读取内部数据,而MyISAM则无需扫描表。
摘要由CSDN通过智能技术生成

今天在做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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

老帽爬新坡

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值