mysql order by varchar_mysql order by 在索引项存在varchar时出现filesort分析

本文分析了一起在MySQL 5.6中由于ORDER BY VARCHAR字段未使用索引导致的慢查询问题。当表结构包含VARCHAR字段,并在ORDER BY中包含常量项,且排序与字符集相关时,查询会执行filesort操作。通过删除ORDER BY中的常量项或调整字符集设置,可以避免filesort,显著提升查询效率。
摘要由CSDN通过智能技术生成

1.背景

由于一个业务表越来越大,大概有550万。突然某天出现了慢查询13s+,排查下来,发现是因为order by 没有走索引,为什么,以及如何处理,请看接下来的分析

1.1 表结构

CREATE TABLE `test` (

`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT 'row id',

`content_id` bigint(20) unsigned NOT NULL DEFAULT '0' COMMENT 'contentID',

`type` varchar(50) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '',

`province` varchar(100) CHARACTER SET utf8 NOT NULL DEFAULT '' COMMENT '地区',

`time_created` bigint(20) NOT NULL DEFAULT '0' COMMENT '创建时间',

PRIMARY KEY (`id`),

KEY `idx_province_type_time_created_content_id` (`province`,`type`,`time_created`,`content_id`)

) ENGINE =InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='test表'

1.2 sql语句

select content_id,time_created from test where province = 'shanghai' and type = 'aa' and time_created > 12345 order by province,type,time_created limit 10

1.3 mysql版本

5.6

2 分析

拿到有问题的语句,首先explain一把

id|select_type|table|partitions|type |possible_keys |key |key_len|ref|rows|filtered|Extra |

--|-----------|-----|----------|-----|-----------------------------------------|-----------------------------------------|-------|---|----|--------|----------------------------------------|

1|SIMPLE |test | |index|idx_province_type_time_created_content_id|idx_province_type_time_created_content_id|520 | | 1| 100|Using where; Using index; Using filesort|

可以看到由于排序没有走索引,进行了file sort

If an index cannot be used to satisfy an ORDER BY clause, MySQL performs a filesort operation that reads table rows and sorts them. A filesort constitutes an extra sorting phase in query execution.

尝试修改语句,删除掉order by的常量项后,发现

id|select_type|table|partitions|type |possible_keys |key |key_len|ref|rows|filtered|Extra |

--|-----------|-----|----------|-----|-----------------------------------------|-----------------------------------------|-------|---|----|--------|------------------------|

1|SIMPLE |test | |index|idx_province_type_time_created_content_id|idx_province_type_time_created_content_id|520 | | 1| 100|Using where; Using index|

竟然走索引了,这是为啥呢,求助强大的google

搜到一个17年有人提出的bug

5f817df883955c9002268844ee1eb27a.png

于是怀疑,是否就是编码的问题,请看下面的测试结果

3 测试

1caf0783ed775277fdeda97c88658625.png

4 结论

从测试结果,可以看出在以下条件下,会导致order by不走索引

1.character_set_connection = utf8mb4

2.order by 索引有常数项,且 Collation =utf8mb4_unicode_ci

经过优化查询时间由13090ms降为5ms

经测试。该问题在最新版mysql中依然存在

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值