当前位置:网站首页>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 .

原网站

版权声明
本文为[Bitter candy]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/176/202206250143437707.html