Performance Comparison of MySQL composite indexes

Source: Internet
Author: User

Composite index is a common data Optimization Method in mysql. Next I will introduce the performance comparison of composite indexes in mysql.

Let's look at some test instances.

The Code is as follows: Copy code

Select * from dlog_user order by online_status, username

First look at the preceding inline SQL statement. username is the primary key of the dlog_user table. dlog_friend has a composite primary key consisting of username and friend_username.

Test Condition 1:

Dlog_user and dlog_friend tables do not have any indexes except the primary key. The result of this SQL statement EXPLAIN is that dlog_user performs a full table query (type = ALL), and the Extra information is use filesort.

Test condition 2:

Add a composite index for dlog_user

The Code is as follows: Copy code

Create index idx_online_status on dlog_user (username, online_status );

EXPLAIN the SQL statement again, or full table query and use filesort

Test Condition 3:

Modify the composite index and change the order of username and online_status. The result is: type = index, Extra = NULL.

The index takes effect!

Test Condition 4:

Modify the SQL statement as follows:

The Code is as follows: Copy code

Select a. * from dlog_user a inner join dlog_friend B on a. username = B. friend_username where B. username = 'ld 'order by a. online_status desc, a. username

That is, when order by is executed, the two fields are sorted in the opposite way. No matter how the index is adjusted, full table query and user filesort are required to execute this SQL statement.

Conclusion:

1. The fields in order by must be indexed in the SQL statement ORDER;
2. The ORDER of the fields in order by must be the same; otherwise, the index is invalid.
3. index creation is not necessarily effective. Use the actual SQL statement to check it.

The following example compares the impact of different query conditions on performance.

Create table test (a int, B int, c int, KEY a (a, B, c ));

The Code is as follows: Copy code

Excellent: select * from test where a = 10 and B> 50

Difference: select * from test where a50

Excellent: select * from test where order by

Difference: select * from test where order by B

Difference: select * from test where order by c

Excellent: select * from test where a = 10 order by

Excellent: select * from test where a = 10 order by B

Difference: select * from test where a = 10 order by c

Excellent: select * from test where a> 10 order by

Difference: select * from test where a> 10 order by B

Difference: select * from test where a> 10 order by c

Excellent: select * from test where a = 10 and B = 10 order by

Excellent: select * from test where a = 10 and B = 10 order by B

Excellent: select * from test where a = 10 and B = 10 order by c

Excellent: select * from test where a = 10 and B = 10 order by

Excellent: select * from test where a = 10 and B> 10 order by B

Difference: select * from test where a = 10 and B> 10 order by c

Indexing principles

1. The fewer indexes, the better.
Cause: when modifying data, the first index must be updated to reduce the write speed.
2. Place the narrower field on the left of the key.
3. Avoid file sort sorting, temporary tables, and table scanning.

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.