MySQL index usage instructions (single-column index and multi-column index), mysql multiple columns

Source: Internet
Author: User
Tags mysql index

MySQL index usage instructions (single-column index and multi-column index), mysql multiple columns

1. Single Column Index

In the performance optimization process, selecting the columns to create an index is one of the most important steps. You can consider using two types of indexes: columns that appear in the Where clause and columns that appear in the join clause. See the following query:

Select age # Do not use the index FROM people Where firstname = 'Mike '# consider using the index AND lastname = 'sullivan' # consider using the index

This query is slightly different from the previous query, but it is still a simple query. Because age is referenced in the Select part, MySQL does not use it to restrict Column Selection operations. Therefore, it is unnecessary to create an index for the age column for this query.

The following is a more complex example:

Select people. age, ## do not use the index town. name ## FROM people left join town ON people. townid = town. townid ## use index Where firstname = 'Mike '## use index AND lastname = 'sullivan' ## use index

As in the preceding example, because firstname and lastname appear in the Where clause, the two columns still need to create indexes. In addition, because the townid of the town table is listed in the join clause, we need to consider creating an index for this column.

So can we simply think that every column in The Where clause and join clause should be indexed? This is almost the case, but not completely. We must also consider the operator types for column comparison. MySQL uses indexes only for the following operators: <, <=, =,>,> =, BETWEEN, IN, and sometimes LIKE.

If you can use an index in the LIKE operation, another operand does not start with a wildcard (% or.

For example:

Select peopleid FROM people Where firstname LIKE 'Mich%' 

This query uses an index, but the following query does not use an index.

Select peopleid FROM people Where firstname LIKE '%ike'; 

2. Multi-column Index

An index can be a single-column index or multiple-column index. The following is an example to illustrate the differences between the two indexes. Suppose there is a people table:

Create TABLE people (  peopleid SMALLINT NOT NULL AUTO_INCREMENT,  firstname CHAR(50) NOT NULL,  lastname CHAR(50) NOT NULL,  age SMALLINT NOT NULL,  townid SMALLINT NOT NULL,  PRIMARY KEY (peopleid) ); 

The following figure shows the data we inserted into the people table:

In this data segment, there are four people named "Mikes" (two named Sullivans and two named McConnells), two 17-year-olds, and one named Joe Smith.

This table is mainly used to return the corresponding peopleid Based on the specified user name, name, and age. For example, you may need to find the leid of a 17-year-old user named Mike Sullivan:

Select peopleid FROM people  Where firstname='Mike'     AND lastname='Sullivan' AND age=17; 

Because we don't want MySQL to scan the entire table every time it executes a query, we need to consider using indexes here.

First, we can consider creating an index on a single column, such as the firstname, lastname, or age column. If we create an INDEX for the firstname column (Alter TABLE people add index firstname (firstname);), MySQL will use this INDEX to quickly limit the search range to those records whose firstname = 'Mike, then, search for other conditions on the intermediate result set: it first excluded the records whose lastname is not equal to "Sullivan", and then excluded those records whose age is not equal to 17. After all the search conditions are met, MySQL returns the final search result.

Because the index of the firstname column is created, MySQL is much more efficient than the full scan of the execution table. However, we require that the number of records scanned by MySQL still far exceed the actual needs. Although we can delete the index on the firstname column and then create the index on the lastname or age column, it seems that no matter which column is created, the search efficiency is still similar.

To improve search efficiency, we need to consider using multi-column indexes. If you create a multi-column index for the columns firstname, lastname, and age, MySQL only needs to retrieve the correct results once! The following is an SQL command to create this multi-column index:

Alter TABLE people  ADD INDEX fname_lname_age (firstname,lastname,age); 

Because the index file is saved in B-tree format, MySQL can immediately convert it to the appropriate firstname, then to the appropriate lastname, and finally to the appropriate age. Without scanning any record of the data file, MySQL finds the target record correctly!

So, if you create a single column index on the columns firstname, lastname, and age respectively, will the effect be the same as creating a multi-column index with firstname, lastname, and age?

The answer is no. The two are completely different. When we perform a query, MySQL can only use one index. If you have three single-column indexes, MySQL will try to select the most restrictive index. However, even if it is the most restrictive single-column index, its capacity is certainly far lower than the multiple-column index of the three columns firstname, lastname, and age.

3. Leftmost Prefixing)

Multi-column index has another advantage, which is embodied by the concept of Leftmost Prefixing. Continue to consider the previous example. Now we have a multiple-column index on the firstname, lastname, and age columns. We call this index fname_lname_age. When the search condition is a combination of the following columns, MySQL uses the fname_lname_age index:

Firstname, lastname, age
Firstname, lastname
Firstname

On the other hand, it is equivalent to the index created on the combination of columns (firstname, lastname, age), (firstname, lastname), and (firstname. All of the following queries can use this fname_lname_age index:

Select peopleid FROM people  Where firstname='Mike' AND lastname='Sullivan' AND age='17';  
Select peopleid FROM people  Where firstname='Mike' AND lastname='Sullivan';  
Select peopleid FROM people  Where firstname='Mike';  

The following queries cannot use thisfname_lname_ageIndex:

Select peopleid FROM people  Where lastname='Sullivan'; 
Select peopleid FROM people  Where age='17';  
Select peopleid FROM people  Where lastname='Sullivan' AND age='17'; 

This article is over, and we will introduce you later.

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.