当前位置:网站首页>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排序会要求回表多造成磁盘读,因此不会被优先选择。
边栏推荐
- [rust submission] review impl trail and dyn trail in rust
- Cesium graphic annotation circle, square, polygon, ellipse, etc
- 【LeetCode】143. 重排链表
- DevEco Studio 3.0编辑器配置技巧篇
- Cesium 拖拽3D模型
- 2022-06-21-Flink-49(一. SQL手册)
- Jilin University 22 spring March document retrieval assignment assessment-00073
- Trading system development (IV) - trading counter system
- opencv是开源的吗?
- [harmony OS] [ark UI] basic ETS context operations
猜你喜欢
佐喃社区

NFT Insider #63:The Sandbox与时代杂志达成合作,YGG成立西班牙subDAO

Does it count as staying up late to sleep at 2:00 and get up at 10:00? Unless you can do it every day

Cesium loading display thermal diagram

DevEco Studio 3.0编辑器配置技巧篇

About PLSQL error initialization failure

【组队学习】SQL编程语言笔记——Task04

How to use ide to automatically sign and debug Hongmeng application

Serious PHP defects can lead to rce attacks on QNAP NAS devices

Flutter FittedBox组件
随机推荐
【Proteus仿真】Arduino UNO按键控制数码管闪烁增/减显示
Comprehensive operation of financial risk management X of Dongcai
Color NFT series representing diversity launched on the sandbox market platform
MySQL modifies and deletes tables in batches according to the table prefix
DAP data scheduling function improvement description
Do you really need automated testing?
How to quickly deliver high-value software
JS tool function, self encapsulating a throttling function
【组队学习】SQL编程语言笔记——Task04
Crawler grabs the data of Douban group
Hello CTP (IV) - CTP transaction API
Comprehensive assignment of thesis writing instruction of Dongcai
uniapp 制作手机app程序, 使用uni.chooseVideo录制视频,视频播放模糊分辨率低的原因
Jilin University 22 spring March "automatic control principle" work assessment-00050
Work assessment of Biopharmaceutics of Jilin University in March of the 22nd spring -00005
Teach you how to install win11 system in winpe
Create SQLite table with shell script and add SQL statement -- General
AI quantitative transaction (I) -- Introduction to quantitative transaction
JS tool function, self encapsulating a throttling function
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