MySQL Index and performance (4) sorting

Source: Internet
Author: User

We know that the B-tree index is ordered, so can you complete the order by operation by simply scanning the index? The answer is yes, but the conditions are also harsh: only if the column order of the index is exactly the same as the order in which the sequence is ordered by, and all the columns in the sentence are sorted by either ASC or all desc,mysql to use the index to sort the results ; If the query needs to correlate more than one table, the condition is more stringent and the index can be sorted only when the columns in the ORDER BY clause are all driver tables (in the execution plan).

Let's take a look at some examples: Suppose the Users table has an index (login_id,status)

Root@test 05:03:12>explain Select ID from the users order by Login_id,status\g *************************** 1. Row *************************** id:1 select_type:simple table:users type:index p Ossible_keys:null key:login_id key_len:387 ref:null rows:5894108 Ext Ra:using index 1 row in Set (0.00 sec) root@test 05:03:17>explain Select ID from the users ORDER by Status\g * 1. Row *************************** id:1 select_type:simple table:users type:index p Ossible_keys:null key:login_id key_len:387 ref:null rows:5894108 Ext Ra:using index; Using filesort 1 row in Set (0.00 sec) Root@test 05:03:26>explain the Select ID from the users order by login_id des C,status\g *************************** 1. Row *************************** id:1 Select_type: Simple table:users type:index possible_keys:null key:login_id key_len:387 Ref:null rows:5894108 extra:using Index; Using filesort 1 row in Set (0.00 sec) Root@test 05:04:03>explain the Select ID from the users order by login_id des C,status desc\g *************************** 1. Row *************************** id:1 select_type:simple table:users type:index p Ossible_keys:null key:login_id key_len:387 ref:null rows:5894108 Ext Ra:using index 1 row in Set (0.00 sec)

Note: If a using filesort appears in the extra column, it means that it cannot be scanned in indexed order.

See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/database/MySQL/

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.