MySQL series-InnoDB index introduction and management

MySQL series-InnoDB index introduction and management

Operation and Maintenance YouthO&M Youth

Series of articles description

MySQL series articles include software installation, specific use, backup and recovery, etc., which are mainly used to record personal study notes. The main MySQL version used is 5.7.28, and the server system version is CentOS 7.5. This chapter is about InnoDB index management.

index

Index definition


Index is a data structure that can help us quickly retrieve data in the database. In layman's terms, an index is like a table of contents of a book, and we can quickly find what we need through the table of contents.

Advantages and disadvantages of indexing


Advantages of indexing:

1. It can speed up data retrieval.
2. Through indexing, you can use optimized hiders in the process of use to improve the performance of the system

Disadvantages of indexing:

1. When the data in the table is added, deleted, and modified, the index needs to be dynamically maintained, which will reduce the execution efficiency of the addition/change/deletion.
2. The index needs to occupy physical space

Index index classification


In MySQL InnoDB, indexes can be divided into clustered indexes and auxiliary indexes (non-clustered indexes).

Clustered index

The leaf node of the index B+Tree stores the entire row of data is the primary key index, also known as the clustered index. A clustered index is an algorithm that reorganizes the actual data on the disk to sort by the value of one or more specified columns. The characteristic is that the order of the stored data is consistent with the index order. In general, the primary key will create a clustered index by default, and only one clustered index is allowed on a table, because once the data is stored, there can only be one order. Once the index is found, the required data is found, then the index is a clustered index, so the primary key is the clustered index. Modifying the clustered index is actually modifying the primary key.

Generally speaking, a table must have a clustered index, even if it is not defined, InnoDB will automatically select the column to generate an index:
1) When there is a primary key, create a clustered index based on the primary key
2) When there is no primary key, it will use a unique and not The empty index column is used as the primary key and becomes the clustered index of this table.
3) If the above two are not satisfied, then innodb creates a virtual clustered index by itself

If you have the following table:


CREATE TABLE world.student(
`id` INT AUTO_INCREMENT NOT NULL COMMENT 'id',
`name` VARCHAR(10) NOT NULL COMMENT '姓名',
`age` INT  NOT NULL COMMENT '年龄',
PRIMARY KEY(id),
INDEX idx_name(NAME)
)ENGINE=INNODB DEFAULT CHARSET='utf8mb4';

INSERT INTO world.`student`(NAME,age) VALUES('张三',24),('李四',20),('王五',21),('运维少年',18);

MySQL series-InnoDB index introduction and management

The clustered index structure is as follows:
MySQL series-InnoDB index introduction and management
clustered index search process:
MySQL series-InnoDB index introduction and management

Auxiliary index

The leaf node of the index B+Tree only stores the value of the primary key and the index column is a non-primary key index, which is also called a non-clustered index. A table can have multiple non-clustered indexes. The storage of non-clustered indexes is separated from the storage of data. That is to say, the index may be found but no data is found. You need to query the table again based on the value (primary key) on the index. Non-clustered indexes are also called secondary indexes.

The data structure of the auxiliary group index in the student table: The
MySQL series-InnoDB index introduction and management
auxiliary group index lookup data process (not back to the table):
MySQL series-InnoDB index introduction and management

Auxiliary group index search data process (back to table):
MySQL series-InnoDB index introduction and management

Auxiliary index single-column index


Single-column index, that is, use one column as the auxiliary index column, but when the query condition uses the auxiliary index column, the index will be used.

Auxiliary index multi-column index (joint index)


Joint index, that is, multi-column index, when creating an index, use multiple columns as index columns, such as:


alter table student add index idx_na(name,age);

Auxiliary index multi-column index (joint index)


The prefix index is aimed at, the length of the index column value we choose is too long, which will cause the height of the index to increase, which will lead to the need to read more index data pages when the index is applied. MySQL recommends that the height of the index tree is 3-4 layers . Therefore, you can select the first part of the large field as the index generation condition.
Similar to the title of a book, the title cannot be too long. If the title is too long, when the table of contents is generated, the table of contents occupies a lot of space and the table of contents page will increase. The prefix index is generally used for fuzzy query.

If you have the following table, you need to use the sno column as the index column. Through comparison, you can see that the first 6 characters can determine a unique value. Therefore, when creating an index, you can set the prefix length to 6 to reduce the height of the index tree.
MySQL series-InnoDB index introduction and management


alter table xxx add index index_name(sno(2));

Index management

View index:


show index from world.student;

MySQL series-InnoDB index introduction and management
MySQL series-InnoDB index introduction and management

Delete index:


drop index idx_name on world.student;

MySQL series-InnoDB index introduction and management

Create index:

Common method 1: Create when creating a table


CREATE TABLE world.student(
`id` INT AUTO_INCREMENT NOT NULL COMMENT 'id',
`name` VARCHAR(10) NOT NULL COMMENT '姓名',
`age` INT  NOT NULL COMMENT '年龄',
PRIMARY KEY(id),  # 聚簇索引
INDEX idx_name(name)  # 辅助索引
)ENGINE=INNODB DEFAULT CHARSET='utf8mb4';

Common method 2: Use the alter statement


alter table world.student add index idx_name(name);

MySQL series-InnoDB index introduction and management

When is the index created?

1) Create a suitable index according to the needs of the business statement, not all columns are indexed
2) Not all columns are indexed, not that the more indexes the better
3) The index is built in the process to do where group by order by join on conditional column

The consequences of disorderly indexing?

1) If there are too many redundant indexes, when the table data changes, it is likely that the indexes will be updated frequently. Will block many normal business requests
2) Too many indexes will cause the optimizer to choose deviations

Index application specification:

1. When building a table, there must be a primary key. The primary key is preferably a numeric column. If not, you can customize an irrelevant column and define it as self-growth.
2. When selecting the unique
index, the value of the unique index is unique, and you can change it. Quickly use the index to determine the
unique field of a record such as the student table middle school number. Establishing a unique index for this field can quickly determine the information of a certain student. If the name is changed, there may be the phenomenon of the same name. Thereby reducing the query speed.
3. For fields that often require operations such as where, order by, group by, join on, etc., sorting will waste a lot of time. Indexes can be established to optimize queries. If the columns are often used as conditions and there are too many duplicate values, a joint index can be established.
4. Use prefix index as much as possible. If the value of the index field is very long, it is best to use the prefix of the value to index.
5. Limit the number of
indexes. The number of indexes is not as much as possible. Problems that may arise
1) Each index is required Occupy disk space, the more indexes, the more disk space is needed.
2) When modifying the table, it is troublesome to reconstruct and update the index. The more indexes, the table update will become a waste of time.
3) The optimizer The burden will be heavy, which may affect the choice of the optimizer.
There is a tool in percona-toolkit that specifically analyzes whether the index is useful
6, deletes no longer used or rarely used indexes
7, and adds indexes to large tables. Operation during busy periods
8. Try to build as little as possible on the updated value column

Index supplement

Supplementary Preparation Form



CREATE TABLE world.student(
`id` INT AUTO_INCREMENT NOT NULL COMMENT 'id',
`name` VARCHAR(10) NOT NULL COMMENT '姓名',
`age` CHAR(3)  NOT NULL COMMENT '年龄',
`address` VARCHAR(20) NOT NULL COMMENT '地址',
`phone` VARCHAR(11) NOT NULL COMMENT '手机号码',
PRIMARY KEY(id),
INDEX idx_info(age,NAME,address)
)ENGINE=INNODB DEFAULT CHARSET='utf8mb4';

INSERT INTO world.`student`(NAME,age,address,phone) VALUES('张三',24,'北京市','10086'),('李四',20,'上海市','10000'),('王五',21,'重庆市','10010'),('运维少年',18,'天津市','13800138000');

How to check whether SQL is indexed or not?


Method 1: explain


explain 执行的语句
explain select * from student where id=1;

Method 2: desc


desc 执行的语句
desc select * from student where name='张三'

MySQL series-InnoDB index introduction and management
MySQL series-InnoDB index introduction and management

Supplementary index scan type


1) index full index scan-need to scan the entire index tree

Index full index scan generally occurs when the query column is an index column


select id from student;

MySQL series-InnoDB index introduction and management

2) range - range scan

When the query conditions are >, <, in, like, the type will be range
MySQL series-InnoDB index introduction and management
MySQL series-InnoDB index introduction and management
MySQL series-InnoDB index introduction and management

3) ref-auxiliary index equivalent query

Auxiliary index equivalent query generally occurs when using auxiliary index columns for equivalent query.
MySQL series-InnoDB index introduction and management

4) Const primary key equivalent query

Clustered index equivalent query generally occurs when using clustered index columns to do equivalent query.
MySQL series-InnoDB index introduction and management

Under what circumstances will the index not be added?


1) Without query conditions
MySQL series-InnoDB index introduction and management

2) When querying the result set, most of the data in the original table should be 15%-30%. If it exceeds, the optimizer feels that there is no need to index. You can use limi paging.
MySQL series-InnoDB index introduction and management
3) The query condition belongs to the function on the index column. Or perform operations on the index column, the operation includes (+- /! etc.)
wrong example: select
from student id-1=2;
correct example: select * from student id=3;
MySQL series-InnoDB index introduction and management
4) The implicit conversion causes the index to fail, This point should be taken seriously, and this mistake is often made. If char is defined, the numeric type is used when querying.
MySQL series-InnoDB index introduction and management
5) not in does not take the auxiliary index. or or in can be changed to union
MySQL series-InnoDB index introduction and management
6) like "%_" does not go in front of the percent sign
MySQL series-InnoDB index introduction and management

Supplement multi-column index (joint index)


Use multiple columns to combine an index idx(age,name,address)

Valid index: age age,name age,name,address (only starting with age and continuous)
Invalid index: name address name,address
MySQL series-InnoDB index introduction and management

The index can only go equal and cannot go range values:

1、age= and name= and address=  #索引能到address
2、age= and name> and address= # 索引能到name
3、age< and name = and address= # 索引能到a

MySQL series-InnoDB index introduction and management
MySQL series-InnoDB index introduction and management

Guess you like

Origin blog.51cto.com/15082392/2656023