Jquery中文网 www.jquerycn.cn
Jquery中文网 >  数据库  >  mysql  >  正文 mysql 不同版本的优化器差异

mysql 不同版本的优化器差异

发布时间:2015-09-23   编辑:www.jquerycn.cn
mysql 版本带来的优化器差异

昨天在日志中发现一个慢查询的语句:
 

SELECT sql_no_cache * FROM `news`.`v9_news`   WHERE `catid` = '15'  AND `status`=99 AND `id`<'201846' ORDER BY id DEsc ;

explain 后
 

+----+-------------+---------+------+---------------------------------------------+---------------------+---------+-------------+-------+-------------+
| id | select_type | table   | type | possible_keys                               | key                 | key_len | ref         | rows  | Extra       |
+----+-------------+---------+------+---------------------------------------------+---------------------+---------+-------------+-------+-------------+
|  1 | SIMPLE      | v9_news | ref  | PRIMARY,catid_2,catid_3,idx_catid_status_id | idx_catid_status_id | 3       | const,const | 56502 | Using where |
+----+-------------+---------+------+---------------------------------------------+---------------------+---------+-------------+-------+-------------+

把catid和status加到order by
 

mysql> explain SELECT sql_no_cache * FROM `news`.`v9_news`   WHERE `catid` = '15'  AND `status`=99 AND `id`<'201846' ORDER BY catid,status,id DEsc limit 1;
+----+-------------+---------+------+---------------------------------------------+---------------------+---------+-------------+-------+-----------------------------+
| id | select_type | table   | type | possible_keys                               | key                 | key_len | ref         | rows  | Extra                       |
+----+-------------+---------+------+---------------------------------------------+---------------------+---------+-------------+-------+-----------------------------+
|  1 | SIMPLE      | v9_news | ref  | PRIMARY,catid_2,catid_3,idx_catid_status_id | idx_catid_status_id | 3       | const,const | 56502 | Using where; Using filesort |
+----+-------------+---------+------+---------------------------------------------+---------------------+---------+-------------+-------+-----------------------------+

虽然有filesort但是这样执行的速度快很多,
###
原来是catid 和id 都是smallint 和int,是以数值定义的,但是''包含的是属于字符串类型,所以会有filesort的出现:
 

mysql> explain SELECT sql_no_cache * FROM `news`.`v9_news`  WHERE `catid` = 15  AND `status`=99   ORDER BY catid,status,id  limit 1;
+----+-------------+---------+------+-------------------------------------+---------------------+---------+-------------+--------+-------------+
| id | select_type | table   | type | possible_keys                       | key                 | key_len | ref         | rows   | Extra       |
+----+-------------+---------+------+-------------------------------------+---------------------+---------+-------------+--------+-------------+
|  1 | SIMPLE      | v9_news | ref  | catid_2,catid_3,idx_catid_status_id | idx_catid_status_id | 3       | const,const | 254418 | Using where |
+----+-------------+---------+------+-------------------------------------+---------------------+---------+-------------+--------+-------------+
1 row in set (0.00 sec)

mysql> explain SELECT sql_no_cache * FROM `news`.`v9_news`  WHERE `catid` = '15'  AND `status`=99   ORDER BY catid,status,id  limit 1;
+----+-------------+---------+------+-------------------------------------+---------------------+---------+-------------+--------+-----------------------------+
| id | select_type | table   | type | possible_keys                       | key                 | key_len | ref         | rows   | Extra                       |
+----+-------------+---------+------+-------------------------------------+---------------------+---------+-------------+--------+-----------------------------+
|  1 | SIMPLE      | v9_news | ref  | catid_2,catid_3,idx_catid_status_id | idx_catid_status_id | 3       | const,const | 254418 | Using where; Using filesort |
+----+-------------+---------+------+-------------------------------------+---------------------+---------+-------------+--------+-----------------------------+

####
和其他人讨论了下,是因为虽然选择idx_catid_status_id  这个索引,但是根本没用这个索引中的id这个键,这点从key_len 为3 可以看出来,

把语句改成:
 

mysql> explain SELECT sql_no_cache * FROM `news`.`v9_news`  force index (idx_catid_status_id) WHERE `catid` = '15'  AND `status`=99 AND `id`<'201846' ORDER BY id DEsc limit 1;
+----+-------------+---------+-------+---------------------+---------------------+---------+------+-------+-----------------------------+
| id | select_type | table   | type  | possible_keys       | key                 | key_len | ref  | rows  | Extra                       |
+----+-------------+---------+-------+---------------------+---------------------+---------+------+-------+-----------------------------+
|  1 | SIMPLE      | v9_news | range | idx_catid_status_id | idx_catid_status_id | 6       | NULL | 56502 | Using where; Using filesort |
+----+-------------+---------+-------+---------------------+---------------------+---------+------+-------+-----------------------------+


会发现强制带上id这个键,
原来优化器算法错了,这个是在5.5.9上去得到的结果

又安装新版本的5.5.28后发现
 

mysql> explain SELECT sql_no_cache * FROM `news`.`v9_news`   WHERE `catid` = '15'  AND `status`=99 AND `id`<'201846' ORDER BY id DEsc limit 1;
+----+-------------+---------+-------+---------------------------------------------+---------------------+---------+------+-------+-------------+
| id | select_type | table   | type  | possible_keys                               | key                 | key_len | ref  | rows  | Extra       |
+----+-------------+---------+-------+---------------------------------------------+---------------------+---------+------+-------+-------------+
|  1 | SIMPLE      | v9_news | range | PRIMARY,catid_2,catid_3,idx_catid_status_id | idx_catid_status_id | 6       | NULL | 56548 | Using where |
+----+-------------+---------+-------+---------------------------------------------+---------------------+---------+------+-------+-------------+
1 row in set (0.00 sec)

mysql> select version();
+------------+
| version()  |
+------------+
| 5.5.28-log |
+------------+
1 row in set (0.00 sec)

总结:优化器可以自动识别,看来新版本的优化器的算法是经过改良的

您可能感兴趣的文章:
mysql 不同版本的优化器差异
Amoeba:开源的分布式数据库Porxy解决方案
六款帮助你实现惊艳视差滚动效果的jQuery插件
mysql开启慢查询以检查查询慢的语句
探讨js字符串数组拼接的性能问题
PHP程序员最容易犯的11个MySQL错误
windows下mysql主从复制配置
MySQL集群亿级数据量并发访问解决方案测试
asp.net性能优化方法-其它
javascript 鼠标按钮button属性

[关闭]