SQL optimization-optimize order by statement || Filesort optimization

Two sorting methods

1). The first is to sort the returned data, which is generally called filesort sorting . All sorts that do not directly return the sorting results by index are called FileSort sorting.

2). The second way to directly return ordered data through ordered index order scan, this case is using index , no additional sorting is required, and the operation efficiency is high


Sort by multiple fields



Understand the sorting method of MySQL,

The optimization goal is clear: minimize additional sorting, and return ordered data directly through the index.

The where condition and Order by use the same index , and the order of Order By is the same as the index order,

And the fields of Order by are in ascending or descending order .

Otherwise, additional operations must be required, and FileSort will appear.



Filesort optimization

By creating a suitable index, the appearance of Filesort can be reduced, but in some cases, the conditional restrictions cannot make Filesort disappear, so it is necessary to speed up the sorting operation of Filesort. For Filesort, MySQL has two sorting algorithms:

1) Two scan algorithm : Before MySQL 4.1, this method was used for sorting. First extract the sort field and row pointer information according to the conditions, and then sort in the sort buffer in the sort area. If the sort buffer is not enough, store the sort results in the temporary table.

                               After finishing sorting, read the records back to the table according to the row pointer. This operation may cause a lot of random I/O operations.

2) One scan algorithm : Take out all the fields that meet the conditions at one time, and then output the result set directly after sorting in the sort buffer in the sorting area. The memory overhead is large when sorting, but the sorting efficiency is higher than the two-scan algorithm.

MySQL by comparison

The size of the system variable max_length_for_sort_data and the total size of the fields retrieved by the Query statement,

To determine whether that sorting algorithm,

If max_length_for_sort_data is larger, then use the second optimized algorithm;

Otherwise, use the first one.

You can appropriately increase the sort_buffer_size and max_length_for_sort_data system variables to increase the size of the sorting area and improve the efficiency of sorting.

 

Guess you like

Origin blog.csdn.net/qq_39368007/article/details/114694230