首页
学习
活动
专区
圈层
工具
发布

生产SQL优化--Using filesort

场景:用户反馈,xx功能生产环境反应过慢,数据最长8秒响应

接口定位:无复杂逻辑,定位到具体SQL

SQL分析

  1. 无索引命中,a表全表扫描
  2. Extra Using filesort

Using filesort 是什么意思?

官方的定义是,MySQL must do an extra pass to find out how to retrieve the rows in sorted order. The sort is done by going through all rows according to the join type and storing the sort key and pointer to the row for all rows that match the WHERE clause . The keys then are sorted and the rows are retrieved in sorted order。

MySQL需要额外的一次传递,以找出如何按排序顺序检索行。通过根据联接类型浏览所有行并为所有匹配WHERE子句的行保存排序关键字和行的指针来完成排序。然后关键字被排序,并按排序顺序检索行。标红,重点。即回表操作。

完整SQL隐藏关键信息

代码语言:javascript
复制
explain select  xxfrom   a left join  r on  a.x= r.xwhere  xx   xx  xx  xx   xxorder by  a.submitTime desclimit 0 ,10

查询a表进行一次回表操作

1强制使用索引

FORCE INDEX (ind_zzz)

没有任何效果

order by 字段建立索引

测试

代码语言:javascript
复制
完成响应 249ms

测试数据命中索引

下一篇
举报
领券