mysql 优化器不准_mysql 版本带来的优化器差异

昨天看到慢查询里面

SELECT sql_no_cache * FROM `news`.`v9_news`   WHERE `catid` = '15'  AND `status`=99 AND `id`

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`+----+-------------+---------+------+---------------------------------------------+---------------------+---------+-------------+-------+-----------------------------+

| 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`+----+-------------+---------+-------+---------------------+---------------------+---------+------+-------+-----------------------------+

| 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`+----+-------------+---------+-------+---------------------------------------------+---------------------+---------+------+-------+-------------+

| 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)

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

阅读(1473) | 评论(0) | 转发(1) |

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值