OctopusLian / leetcode-solutions

LeetCode,LintCode,牛客网,企业题库,《Sword to Offer》,《Cracking the Coding Interview》题解
MIT License
4 stars 4 forks source link

每日一题:mysql中,什么是filesort? #217

Closed OctopusLian closed 2 years ago

OctopusLian commented 3 years ago

https://my.oschina.net/u/4553401/blog/4733355 如果mysql在排序的时候没有使用到索引那么就会输出 using filesort。 filesort有两种实现 1.一遍扫描 一遍扫描数据后将select需要的列数据以及排序的列数据都取出来,这样就不需要进行第二遍扫描了。 2.两遍扫描 第一遍扫描出需要排序的字段,然后进行排序后,根据排序结果,第二遍再扫描一下需要select的列数据。

具体采用哪种扫描是根据max_length_for_sort_data来判断的。 如果需要的列数据一行可以放入max_length_for_sort_data则使用一遍扫描否则使用两遍扫描。