Correct understanding of column indexes and multi-column indexes in Mysql

Source: Internet
Author: User

The Mysql database provides two types of indexes. If they are not correctly set, the index utilization efficiency will be greatly reduced, but the problem is completely unknown.
Copy codeThe Code is as follows:
Create table test (
Id int not null,
Last_name CHAR (30) not null,
First_name CHAR (30) not null,
Primary key (id ),
INDEX name (last_name, first_name)
);

The above is actually a multi-column index. The code for creating a column index is as follows:
Copy codeThe Code is as follows:
Create table test (
Id int not null,
Last_name CHAR (30) not null,
First_name CHAR (30) not null,
Primary key (id ),
INDEX name (last_name ),
INDEX_2 name (first_name)
);

A multi-column index can be considered as a sort array containing the values created by the concatenate index column values. When the query statement conditions include last_name and first_name, for example:
Copy codeThe Code is as follows:
SELECT * FROM test WHERE last_name = 'kun' AND first_name = 'lil ';

SQL first filters out records whose last_name meets the condition, and then filters records whose first_name meets the condition. If we create two column indexes on last_name and first_name respectively, the mysql processing method will be different. It will select the strictest index for retrieval, it can be understood that the index with the strongest retrieval capability is used for retrieval, and the other one cannot be used, so the effect is not as good as that of Multi-column indexes.

However, the use of multi-column indexes is also required. The following query statements can use the above multi-column indexes:
Copy codeThe Code is as follows:
SELECT * FROM test WHERE last_name = 'widenius ';
SELECT * FROM test WHERE last_name = 'widenius 'AND first_name = 'Michael ';
SELECT * FROM test WHERE last_name = 'widenius 'AND (first_name = 'Michael' OR first_name = 'monty ');
SELECT * FROM test WHERE last_name = 'widenius 'AND first_name> = 'M' AND first_name <'n ';

The following query statements do not use multiple column indexes:
Copy codeThe Code is as follows:
SELECT * FROM test WHERE first_name = 'Michael ';
SELECT * FROM test WHERE last_name = 'widenius 'OR first_name = 'Michael ';

Index creation by multiple columns is more advantageous than index creation by each column, because the more index creation, the more disk space occupied, the slower the data update speed.
In addition, when you create multiple index columns, you need to pay attention to the order. You should place the strict index first, so that the filtering intensity will be greater and the efficiency will be higher.

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.