MySQL tuning index-ORDER BY (GROUP BY)

order bySorting optimization

1. ORDER BYClauses should be sorted by index as much as possible, avoiding sorting by filesort.

2. ORDER BY satisfies two ways to use the index method to sort:

  • order by using index leftmost column
  • Use the where clause and order by clause conditional column combination to satisfy the leftmost front index column

3. If it is not on the index column, filesort has two algorithms, mysql will start two-way and single-way sorting.

  • Two-way sorting
    (1) Before mysql4.1, two-way sorting was used, scanning the disk twice, and finally got the data, reading the row pointer and order by column, sorting them, and then scanning the sorted list, according to the list The value in is read again from the database list and the corresponding data is output.
    (2) Read the fields from the disk, sort them in the buffer, and then take other fields from the disk.
    (3) To fetch a batch of data, scan the disk twice and perform two I / O operations. Because I / O operations are time-consuming, the index uses another algorithm after 4.1, single-way sorting.
  • Single-way sorting
    Read the columns required by the query from the disk, sort in the buffer according to the order by column, and then scan the sorted list for output. It is more efficient and avoids reading the data for the second time. And random I / O becomes sequential I / O, but it will use more memory space because it keeps data in memory.
  • Note that
    in sort buffer, single-way sorting uses more memory space than double-way sorting, because single-way sorting takes out all the fields, all of which may cause the total size of the extracted data to exceed the capacity of sort_buffer, resulting in only taking Sorting the data with the size of sort_buffer (create tmp, multi-way merge), sorting the size of sort_buffer, sorting again ..., resulting in multiple I / O

4. Improve ORDER BY efficiency

ORDER BY only finds the required fields, select * is taboo, this is very important, the impact here is:

  • When the sum of the searched fields is less than max_length_for_sort_data, and the sort field is not TEXT or BLOB, the improved algorithm will be used for single-way sorting, otherwise multi-way sorting is used.
  • Sort_buffer two algorithms are likely to exceed the capacity of the beyond, will create tmp file merge sort, resulting in multiple I / O, but the wind single algorithm is higher, it is appropriate at this time to turn up the sort_buffer_size.
    (2 ) Try to increase the size of sort_buffer_size
    Regardless of which algorithm, appropriate increase in sort_buffer_size will increase efficiency, but it should be improved according to the ability of the system, because this parameter is for each process.
    (3) Try to increase max_length_for_sort_data to
    increase this parameter, will increase the probability of using single-way sorting algorithm, if set too high, the probability that the total data capacity exceeds sort_buffer_size will increase, the obvious symptoms are high I / O activity and low Processor utilization.

GROUP BY Similar

to sum up:

order by.png

Insert picture description here

Published 41 original articles · Liked 14 · Visitors 10,000+

Guess you like

Origin blog.csdn.net/Yunwei_Zheng/article/details/104017189