当前位置:网站首页>MySQL order by
MySQL order by
2022-06-25 04:10:00 【Bitter candy】
1. Full field sorting
To avoid full table scanning , We need to be in city Field plus index .
select city,name,age from t where city=‘ Hangzhou ’ order by name limit 1000 ;
Extra In this field “Using filesort” It means you need to sort ,MySQL Each thread is allocated a block of memory for sorting , be called sort_buffer.
Usually , The execution flow of this statement is as follows :
1. initialization sort_buffer, Make sure to put in name、city、age These three fields ;
2. From the index city Find the first satisfaction city=' Hangzhou ’ The primary key of the condition id, That's what this is ID_X;
3. To primary key id Index takes out the whole line , take name、city、age The values of the three fields , Deposit in sort_buffer in ;
4. From the index city Take the primary key of the next record id;
5. Repeat step 3、4 until city The value of does not meet the query conditions , The corresponding primary key id That's what this is ID_Y;
6. Yes sort_buffer The data in is sorted by field name Do quick sort ;
7. According to the sorting results, take the first 1000 Line back to the client .
Press name Sort ” This action , It could be done in memory , You may also need to use external sorting , It depends on the memory and parameters required for sorting sort_buffer_size.
sort_buffer_size, Namely MySQL Memory opened up for sorting (sort_buffer) Size . If the amount of data to be sorted is less than sort_buffer_size, Sorting is done in memory . But if the sorting data is too large , There's no memory , You have to use temporary disk files to help sort .
When the memory can't fit , You need to use external sorting , External sorting generally uses merge sorting algorithm . It's easy to understand ,MySQL Divide the data to be sorted into 12 Share , Each one is sorted separately and stored in these temporary files . Then put this 12 An ordered file is merged into an ordered large file .
If sort_buffer_size The size exceeds the amount of data to be sorted ,number_of_tmp_files Namely 0, Indicates that sorting can be done directly in memory .
Otherwise, it needs to be sorted in a temporary file .sort_buffer_size The smaller it is , The more shares you need to divide ,number_of_tmp_files The greater the value of .
2.rowid Sort
max_length_for_sort_data, yes MySQL A parameter that controls the length of row data used for sorting . It means , If the length of a single line exceeds this value ,MySQL I think the single line is too big , To change the algorithm .
The new algorithm puts sort_buffer Field of , Only columns to sort ( namely name Field ) And the primary key id.
But at this moment , The result of sorting is less city and age Value of field , Can't go back directly , The whole execution process becomes as follows :
1. initialization sort_buffer, Make sure to put in two fields , namely name and id;
2. From the index city Find the first satisfaction city=' Hangzhou ’ The primary key of the condition id, That's what this is ID_X;
3. To primary key id Index takes out the whole line , take name、id These two fields , Deposit in sort_buffer in ;
4. From the index city Take the primary key of the next record id;
5. Repeat step 3、4 Until not satisfied city=' Hangzhou ’ Until the conditions are met , That's what this is ID_Y;
6. Yes sort_buffer The data in is sorted by field name Sort ;
7. Traversal sort results , Take before 1000 That's ok , And in accordance with the id Return the value of to the original table city、name and age Three fields are returned to the client .
3. Full field sorting VS rowid Sort
If MySQL I'm really worried that the sorting memory is too small , Will affect the sorting efficiency , To adopt rowid Sorting algorithm , In this way, you can sort more rows at a time in the sorting process , But you need to go back to the original table to get the data .
If MySQL Think the memory is big enough , Priority will be given to full field sorting , Put all the fields you need in sort_buffer in , After sorting, the query results will be returned directly from memory , You don't have to go back to the original table to get the data .
This also reflects MySQL A design idea of : If there's enough memory , Make more use of memory , Minimize disk access .
about InnoDB Table for example ,rowid Sorting will require more disk reads to go back to the table , So it won't be a priority .
边栏推荐
- Hello CTP (I) - basic knowledge of futures
- [rust submission] review impl trail and dyn trail in rust
- Jilin University 22 spring March "technical economics" assignment assessment-00073
- Development of trading system (II) -- market data
- 【LeetCode】22. 括号生成
- JS tool function, self encapsulating a throttling function
- 练习:仿真模拟福彩双色球——中500w巨奖到底有多难?跑跑代码就晓得了。
- PHP code audit 2 - these functions must be known and understood
- Jilin University 22 spring March "official document writing" assignment assessment-00084
- Work assessment of Biopharmaceutics of Jilin University in March of the 22nd spring -00031
猜你喜欢

opencv最大能打开多少图像?

Changsha's "talent seeking": "making efforts" and "making practical moves" go hand in hand, "rapid development" and "slow life" go hand in hand

MySQL modifies and deletes tables in batches according to the table prefix

How to install opencv? Opencv download installation tutorial

The 5th series of NFT works of missing parts was launched on the sandbox market platform

Hot and cold, sweet and sour, want to achieve success? Dengkang oral, the parent company of lengsuanling, intends to be listed on the main board of Shenzhen Stock Exchange

client-go gin的简单整合十一-Delete

冷热酸甜、想成就成?冷酸灵母公司登康口腔欲在深交所主板上市

Crawler grabs the idea of reading on wechat

地方/园区产业规划之 “ 如何进行产业定位 ”
随机推荐
JS tool function, self encapsulating a throttling function
【LeetCode】148. 排序链表
Watch out for the stolen face! So many risks of face recognition used every day?
数字时代的“文艺复兴”?起底数字藏品,让人欢喜让人愁
Mstp+vrrp+ospf implements a three-tier architecture
《悉达多》:一生之书,可以时常反刍
The problem that only the home page can be accessed under openSUSE Apache laravel
"Comment positionner l'industrie" dans la planification industrielle locale / parc
Development of trading system (XII) - Official quickfix document
MySQL modifies and deletes tables in batches according to the table prefix
[rust contribution] implement Message Oriented Middleware (6) -client from zero
How to quickly deliver high-value software
PHP代码审计1—PHP.ini的那些事
General steps for QT compiling database plug-ins
Is opencv open source?
How much do you know about the use value of WMS warehouse management system
【Proteus仿真】Arduino UNO按键控制数码管闪烁增/减显示
2022-06-21-Flink-49(一. SQL手册)
Simple integration of client go gin -update
佐喃社区