MySQL creates a composite index

Everyone knows that index is the main factor for optimization, and it has little impact on a small amount of data index. For hundreds of thousands or millions of data, the performance of single-column index is not very ideal, and combined index can be greatly reduced. overhead.

First, the method of creating a composite index

1. SQL statements:

ALTER TABLE `table_name` ADD INDEX index_name (`column1`,`column2`,`column3`);

table_name : the name of the table that needs to create a composite index;

index_name : the name of the composite index;

column1... : create a column for the composite index;

2、Navicat:

Select the table --- right click on the design table --- index --- add index --- three points on the right side of the field --- select multiple columns --- OK --- select the index type and method --- set the index name --- save;


2. Rules of use

The composite index follows the best left matching rule ; that is, when the leftmost column condition with the composite index in the query condition takes effect.

For example, when creating a combined index in the order of name, num, and iphone;

The query methods for hitting the index are as follows (the order after where does not affect):

1. Query name;

2. Query name, num;

3. Query name, num, iphone;

The query methods of non-hit indexes are as follows (the order after where does not affect):

1. Query num;

2. Query iphone;

3. Query num, iphone;

Guess you like

Origin blog.csdn.net/weixin_45151960/article/details/129875178