MySQL order by|group by sort optimization

Source: Internet
Author: User

ORDER BY keyword optimization 1 try to sort by using the index method, avoiding sorting using the Filesort method.  2. MySQL supports two sorting methods, Filesort and Index,index, which refer to the MySQL scan index itself to complete the sorting. Filesort is inefficient in ordering. 
order by satisfies both cases and uses index to sort 1 the. Order BY statement uses the leftmost column of the index 2. Use the WHERE clause and the ORDER BY clause condition to satisfy the leftmost top of the index.
If you are not on the index column, Filesort has two algorithms: MySQL will start by double-sorting and single-sort Mysql4.1 before using two-way sorting, literally two scans of the disk, finally getting the data, reading the row pointers and the order by column, sorting them. Then scan the sorted list and re-read the corresponding data from the list by the values in the list. Single sort: Reads the columns to be queried from the disk, sorts them in buffer according to the order by column, and then scans the sorted list for output, which is more efficient and avoids the second reading of the data. And it turns the random io into sequential io, but he uses more space because he keeps every row in memory. Single query problem: In Sort_buffer, when using a single query, because all the fields are taken out, so it is possible to take out the total size of the data out of sort_buffer capacity, resulting in only sort_buffer capacity to go to the size of the data, to sort, After the discharge of the sort_buffer capacity size, resulting in multiple I/O.
Optimization strategy: Increase the setting of the Sort_buffer_size parameter to increase the setting of the Max_length_forsort_data parameter

Group BY is essentially a sorted first, followed by the best left prefix of the index. When an index column cannot be used to increase the setting of the Sort_buffer_size parameter and the settings of the Max_length_forsort_data parameter where the where is higher than the having, the condition that can be written in the where limit is not to have a qualification.

MySQL order by|group by sort optimization

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.