MySQL multi-column index classic case

Source: Internet
Author: User

A classic multi-column index case, such as the title:

假设某个表有一个联合索引(c1,c2,c3,c4)一下——只能使用该联合索引的 c1,c2,c3 部分Awhere c1=x and c2=x and c4>x and c3=xB where c1=x and c2=x and c4=x order by c3C where c1=x and c4= x group by c3,c2D where c1=x and c5=x order by c2,c3E where c1=x and c2=x and c5=? order by c2,c3

Create a test table

create table test (    c1 tinyint(1) not null default 0,    c2 tinyint(1) not null default 0,    c3 tinyint(1) not null default 0,    c4 tinyint(1) not null default 0,    c5 tinyint(1) not null default 0,    index c1234(c1,c2,c3,c4));//插入两条数据insert into test values (1,3,5,6,7),(2,3,9,8,3),(4,3,2,7,5);insert into test values (2,4,5,8,7),(1,3,5,8,4),(3,4,2,9,6);

Analysis A = C1=x and C2=x and c4>x and c3=x <== equivalent ==> c1=x and C2=x and C3=x and c4>x
C1,C2,C3,C4 can use it all.

Analysis b = = select * FROM test where c1=1 and c2=2 and c4=3 order by C3
C1, c2 index is used, on the basis of C2 used to the index, C3 is ordered, so no additional sorting, and C4 does not play a role

Analysis c = select * from test where c1=1 and c4=2 GROUP by C3,C2
The C1 index is used only because the order of group by C3,C2 cannot take advantage of the C2,C3 index

Analysis D = select * FROM test where c1=1 and c5=2 order by C2,C3
C1 is determined on the basis that C2 is orderly, C2 under C3 is orderly, so c2,c3 plays a role in sequencing. Therefore, it is useless to filesort

Analysis E = select * from test where c1=1 and c2=3 and c5=2 order by C3;
Because the value of C2 is both fixed and not considered when participating in a sort

MySQL multi-column index classic case

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.