MySQL optimized order by using Filesort

Source: Internet
Author: User
Tags mysql query

The using filesort generally appears in the use of the order by statement.

The using filesort does not necessarily cause MySQL performance problems. However, if the number of queries is very many, then each time in MySQL to sort, or will have an impact.

The optimization method here is to index the fields in the order by, such as statements:

SELECT * from YW_SYJGB ORDER by result_date desc LIMIT 0,1000;

To view the execution plan:

+----+-------------+----------+------+---------------+------+---------+------+---------+----------------+
| ID | Select_type | Table | Type | Possible_keys | Key | Key_len | Ref | Rows | Extra |
+----+-------------+----------+------+---------------+------+---------+------+---------+----------------+
| 1 | Simple | YW_SYJGB | All | NULL | NULL | NULL | NULL | 1312418 | Using Filesort |
+----+-------------+----------+------+---------------+------+---------+------+---------+----------------+


You need to build an index on the result_date:

View the execution plan at this time:

+----+-------------+----------+-------+---------------+-------------+---------+------+------+-------+
| ID | Select_type | Table | Type | Possible_keys | Key | Key_len | Ref | Rows | Extra |
+----+-------------+----------+-------+---------------+-------------+---------+------+------+-------+
| 1 | Simple | YW_SYJGB | Index | NULL | Result_date | 6 | NULL | 1000 | NULL |
+----+-------------+----------+-------+---------------+-------------+---------+------+------+-------+

You can see that there is no using Filesort after the index is used in the execution plan

It is important to note that because the using filesort is used to sort in memory, MySQL has a limit on the size of the sorted record: Max_length_for_sort_data, default is 1024
Show variables like '%max_length_for_sort_data% ';

+--------------------------+-------+
| variable_name | Value |
+--------------------------+-------+
| Max_length_for_sort_data | 1024 |
+--------------------------+-------+
After the author tests, if the data of the sort query is more than this default value, you will still use the using Filesort.

In summary, when the amount of data for a sorted query is within the range of the default value, adding an index to the sorted field increases the speed of the MySQL query.

This article permanently updates the link address : http://www.linuxidc.com/Linux/2015-11/124881.htm

MySQL optimized order by using Filesort

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.