说说explain中的Using filesort
admin
2023-05-01 21:04:22
0

有时查看SQL的执行计划时, 会遇到Using filesort, 如下.

mysql> explain select * from tb1 where col1 = 4 order by col2\G

*************************** 1. row ***************************

           id: 1

  select_type: SIMPLE

        table: tb1

         type: ref

possible_keys: idx_col1

          key: idx_col1

      key_len: 4

          ref: const

         rows: 1

        Extra: Using where; Using filesort

1 row in set (0.00 sec)


这个filesort是说, MySQL要多做一次额外的排序, 确切的说是快速排序(Quicksort).



先初步了解下Quicksort排序的概念(From Wikipedia). 

Quicksort is a divide and conquer algorithm. Quicksort first divides a large array into two smaller sub-arrays: the low elements and the high elements. Quicksort can then recursively sort the sub-arrays.


The steps are:

1. Pick an element, called a pivot, from the array.

2. Partitioning: reorder the array so that all elements with values less than the pivot come before the pivot, while all elements with values greater than the pivot come after it (equal values can go either way). After this partitioning, the pivot is in its final position. This is called the partition operation.

3. Recursively apply the above steps to the sub-array of elements with smaller values and separately to the sub-array of elements with greater values.


再看下Python对于其的一个实现.

#!/usr/bin/env python

# -*- coding: utf-8 -*-


from __future__ import print_function


def quicksort(array):

    if len(array) < 2:

        return array

    else:

        pivot = array[0]

        less = [i for i in array[1:] if i <= pivot]

        greater = [i for i in array[1:] if i > pivot]


        return quicksort(less) + [pivot] + quicksort(greater)


print(quicksort([10, 5, 2, 3]))



再回来说filesort, 在MySQL中有the Original, Modified和In-Memory filesort Algorithm 3种实现. 


The Original filesort Algorithm

1. 扫描或根据WHERE条件, 获取所有记录.


2. 把每条记录的sort key和row ID, 即, 放入sort buffer中. 若sort buffer满了, 就在内存中进行一次quicksort, 然后将写入临时文件, 并记录指向指针. 重复该过程, 直到读取了所有记录.


3. 进行若干次multi-merge操作, 将所有row ID写入结果文件.


4. 根据row ID再次获取记录.


很容易发现, 上面的步骤1和4, 一共读取了2遍记录, 所以也就有了下面的改进实现.


The Modified filesort Algorithm

较Original改变的地方是, 在第2步记录的是sort key和涉及到的其它列, 即, 不是row ID了. 第3步完成后, 就可得到结果了.


这个算法中占用空间比要大, 若排序数据量很大的情况下, 会频繁写临时文件, 为了避免其, 引入了max_length_for_sort_data参数.


The In-Memory filesort Algorithm

那么排序数据量比较小的情况下呢, 小到在sort buffer中就可完成排序, 针对这种情况又有了In-Memory filesort. 这时MySQL把sort buffer当成priority queue使用, 避免使用临时文件.


上面可以看到MySQL已在尽量优化排序了, 也从侧面说明其不希望排序的出现, 如最开始的SQL, 建立一个(col1, col2)的联合索引, 就可以避免排序了, 该原因还要从B+树索引说起...


若感兴趣可关注订阅号”数据库最佳实践”(DBBestPractice).

说说explain中的Using filesort

相关内容

热门资讯

日本零食厂商因石脑油匮乏部分停... 【环球网报道】据日本共同社5月13日报道,日本食品生产公司“野村煎豆加工店”当天接受采访时表示,由于...
郑丽文:特朗普若反对“台独”,... 美国总统特朗普将于5月13日至15日访华,台湾问题是主要议题之一。中国国民党主席郑丽文称,特朗普若表...
特朗普要求中国对美经贸团队访问... 澎湃新闻记者 杨文钦 朱郑勇5月13日,外交部发言人郭嘉昆主持例行记者会。法新社记者提问,美国总统特...
美媒又想起这茬:2年前在地中海... 【文/观察者网 阮佳琪】2024年12月23日,载有16名船员的俄罗斯“大熊星座”号货船在西班牙近海...
App过度索取授权或被境外间谍... 微信公众号“国家安全部”5月13日发文: 手机里各种各样的应用程序(APP)五花八门,在方便我们生...
广合科技获得发明专利授权:“一... 证券之星消息,根据天眼查APP数据显示广合科技(001389)新获得一项发明专利授权,专利名为“一种...
华尔街科技老将:大科技公司分化... 5月11日,互联网泡沫时期的知名芯片分析师、Niles Investment Management创...
香港80后“地产女王”烧炭身亡... 据《香港01》报道,5月12日,香港九龙传统豪宅地段加多利山畔的豪宅项目Kadoorie Hill发...
谷歌发布安卓 AI 系统,这就... 和去年一样,在正式的 Google I/O 开发者大会之前,谷歌为 Android 单独开了一次小型...
300斤医生走红 曾一年猛涨1...   300斤医生走红 曾一年猛涨100斤  【300斤医生走红 曾一年猛涨100斤】5月11日,上海...