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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM