当前位置:网站首页>Mysql的order by
Mysql的order by
2022-06-25 03:53:00 【苦 糖 果】
1.全字段排序
为避免全表扫描,我们需要在city字段加上索引。
select city,name,age from t where city=‘杭州’ order by name limit 1000 ;
Extra这个字段中的“Using filesort”表示的就是需要排序,MySQL会给每个线程分配一块内存用于排序,称为sort_buffer。
通常情况下,这个语句执行流程如下所示 :
1.初始化sort_buffer,确定放入name、city、age这三个字段;
2.从索引city找到第一个满足city='杭州’条件的主键id,也就是图中的ID_X;
3.到主键id索引取出整行,取name、city、age三个字段的值,存入sort_buffer中;
4.从索引city取下一个记录的主键id;
5.重复步骤3、4直到city的值不满足查询条件为止,对应的主键id也就是图中的ID_Y;
6.对sort_buffer中的数据按照字段name做快速排序;
7.按照排序结果取前1000行返回给客户端。
按name排序”这个动作,可能在内存中完成,也可能需要使用外部排序,这取决于排序所需的内存和参数sort_buffer_size。
sort_buffer_size,就是MySQL为排序开辟的内存(sort_buffer)的大小。如果要排序的数据量小于sort_buffer_size,排序就在内存中完成。但如果排序数据量太大,内存放不下,则不得不利用磁盘临时文件辅助排序。
内存放不下时,就需要使用外部排序,外部排序一般使用归并排序算法。可以这么简单理解,MySQL将需要排序的数据分成12份,每一份单独排序后存在这些临时文件中。然后把这12个有序文件再合并成一个有序的大文件。
如果sort_buffer_size超过了需要排序的数据量的大小,number_of_tmp_files就是0,表示排序可以直接在内存中完成。
否则就需要放在临时文件中排序。sort_buffer_size越小,需要分成的份数越多,number_of_tmp_files的值就越大。
2.rowid排序
max_length_for_sort_data,是MySQL中专门控制用于排序的行数据的长度的一个参数。它的意思是,如果单行的长度超过这个值,MySQL就认为单行太大,要换一个算法。
新的算法放入sort_buffer的字段,只有要排序的列(即name字段)和主键id。
但这时,排序的结果就因为少了city和age字段的值,不能直接返回了,整个执行流程就变成如下所示的样子:
1.初始化sort_buffer,确定放入两个字段,即name和id;
2.从索引city找到第一个满足city='杭州’条件的主键id,也就是图中的ID_X;
3.到主键id索引取出整行,取name、id这两个字段,存入sort_buffer中;
4.从索引city取下一个记录的主键id;
5.重复步骤3、4直到不满足city='杭州’条件为止,也就是图中的ID_Y;
6.对sort_buffer中的数据按照字段name进行排序;
7.遍历排序结果,取前1000行,并按照id的值回到原表中取出city、name和age三个字段返回给客户端。
3.全字段排序 VS rowid排序
如果MySQL实在是担心排序内存太小,会影响排序效率,才会采用rowid排序算法,这样排序过程中一次可以排序更多行,但是需要再回到原表去取数据。
如果MySQL认为内存足够大,会优先选择全字段排序,把需要的字段都放到sort_buffer中,这样排序后就会直接从内存里面返回查询结果了,不用再回到原表去取数据。
这也就体现了MySQL的一个设计思想:如果内存够,就要多利用内存,尽量减少磁盘访问。
对于InnoDB表来说,rowid排序会要求回表多造成磁盘读,因此不会被优先选择。
边栏推荐
- Understand (DI) dependency injection in PHP
- Crawl Sina Weibo fans
- How to quickly deliver high-value software
- opencv是开源的吗?
- BGP biplane architecture
- Comprehensive assignment of thesis writing instruction of Dongcai
- Development of trading system (XIII) -- Analysis of quickfix source code
- windows 2003 64位系统php运行报错:1% 不是有效的 win32 应用程序
- List rendering in wechat applet
- Cesium loading display thermal diagram
猜你喜欢

Cesium graphic annotation circle, square, polygon, ellipse, etc

2.吹响半音阶口琴
![[harmony OS] [arkui] ETS development graphics and animation drawing](/img/9d/0ac2b3d8bcdcd610767df930e2fa4e.png)
[harmony OS] [arkui] ETS development graphics and animation drawing

Google founder brin's second marriage broke up: it was revealed that he had filed for divorce from his Chinese wife in January, and his current fortune is $631.4 billion

The programmer reality show is coming again! Hulan, as the host, carried the lamp to fill the knowledge. The SSS boss had a bachelor's degree in pharmacy. Zhu Jun and Zhang Min from Tsinghua joined th
![[rust submission] review impl trail and dyn trail in rust](/img/bc/05b3e031659ce19d6f6e3887d70512.jpg)
[rust submission] review impl trail and dyn trail in rust

《Missing Parts》NFT 作品集第 5 系列上线 The Sandbox 市场平台

2. play the chromatic harmonica

Development of trading system (III) - risk control system

client-go gin的简单整合十-Update
随机推荐
严重的PHP缺陷可导致QNAP NAS 设备遭RCE攻击
Cesium graphic annotation circle, square, polygon, ellipse, etc
How to quickly deliver high-value software
The 5th series of NFT works of missing parts was launched on the sandbox market platform
Comprehensive operation of financial risk management X of Dongcai
Windows 2003 64 bit system PHP running error: 1% is not a valid Win32 Application
9 necessary soft skills for program ape career development
【Proteus仿真】Arduino UNO按键控制数码管闪烁增/减显示
cesium 图形标注圆形、正方形、多边形、椭圆等
【Harmony OS】【ARK UI】ETS 上下文基本操作
Jilin University 22 spring March document retrieval assignment assessment-00073
The programmer reality show is coming again! Hulan, as the host, carried the lamp to fill the knowledge. The SSS boss had a bachelor's degree in pharmacy. Zhu Jun and Zhang Min from Tsinghua joined th
About PLSQL error initialization failure
Crawler grabs the data of Douban group
Crawl Sina Weibo fans
uniapp 制作手机app程序, 使用uni.chooseVideo录制视频,视频播放模糊分辨率低的原因
你真的需要自动化测试吗?
Cesium 拖拽3D模型
Comprehensive assignment of thesis writing instruction of Dongcai
【Rust投稿】捋捋 Rust 中的 impl Trait 和 dyn Trait