MySQL optimization -- Usingfilesort_MySQL

Source: Internet
Author: User
This problem occurs because MySQL can only use one index for each query. The WHERE condition of your SQL statement is different from that of ORDERBY. if the index is not created, ORDERBY cannot use the index, the reason for this problem occurs when Usingfilesort occurs is that MySQL can only use one index for each query, and the WHERE condition of your SQL statement is different from the ORDER BY condition. if the index is not created properly, order by cannot use the index, and the Using filesort problem occurs.
To solve this problem, create a hybrid index containing the WHERE and order by conditions.

For example, the original SQL statement is:

SELECT * FROM user u where u. id = 100 order by u. update_time

The index is idx_user_id (id)
Now re-create the index as idx_user_id_update_time (id, update_time) and run the EXPLAIN command to view the index. if the key uses the newly created idx_user_id_update_time index, the Using file sort problem disappears, if the key does not use the new idx_user_id_update_time index, you can use the force index () method to forcibly use this index. in this case, the using filesort problem is solved.
SELECT * FROM user u force index (idx_user_id_update_time) where u. id = 100 order by u. update_time

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.