暂无图片
分享
曾经的zeng
2019-04-12
selec a from tab_t where b=0 order by c limit 10;

表情况:tab_t 数据库20w,b=0无结果,表上无索引。

现象:语句频繁执行,show processlist看到执行时间为0,但是state:creating sort index。explain Extra : filesort,通过profile看到creating sort index耗时最长,而不加order by c 时看到sending data耗时最长(此sending data位于query end之前)

疑问:请问下 b=0 并无匹配值为何还有排序消耗,难道order by在where之前执行吗?

收藏
分享
7条回答
默认
最新
Cui Hulong

我理解creating sort index是mysql 要通过内部临时表的方案来处理这次Select 可能性比较高。

最好能提供一下 sql 语句和 执行计划共享出来看下。



暂无图片 评论
暂无图片 有用 0
曾经的zeng

tmp_bc746aab6ab921300fde791454851995.jpg

暂无图片 评论
暂无图片 有用 0
曾经的zeng

这个临时表方案也是用于排序吧

暂无图片 评论
暂无图片 有用 0
Cui Hulong

就是 排序导致的。create_time 加个索引看看。

暂无图片 评论
暂无图片 有用 0
曾经的zeng

但是该select是无匹配值的,为何还有order消耗,order by在where之前执行了吗,为什么会这样?

暂无图片 评论
暂无图片 有用 0
Cui Hulong

顺序应该是如下:where->order by->limit,消耗实在order by 上 用 file sorting 部分。

确认下来应该是bug,参考下面

1.sql语句如下

image.png

2.show profile for query 15;确认问题在于 order  by 部分

3.sql\filresort.cc 里 看了源代码,没有判断rowcount=0值的情况下,停止处理的部分

   merge_buffers部分 ,大概是这部分代码。

image.png










暂无图片 评论
暂无图片 有用 0
章芋文
问题已关闭: 问题已经得到解决
暂无图片 评论
暂无图片 有用 0
回答交流
提交
问题信息
请登录之后查看
邀请回答
暂无人订阅该标签,敬请期待~~
暂无图片墨值悬赏