Mysql database index creation, index deletion, index failure scenario detailed explanation


foreword

In a relational database, an index is a storage structure that sorts the values ​​of one or more columns in a database table separately. A list of logical pointers to the data page of the value.
The function of the index is equivalent to the table of contents of the book, and the required content can be quickly found according to the page number in the table of contents. The MySQL database supports a total of 5 types of indexes and 9 scenarios of index failure. Let me introduce them one by one.

1. Index classification and creation

1. Ordinary index

Ordinary index is a common index in MySQL database. The columns added with ordinary index have no special requirements on the data. The function of ordinary index is to speed up the query speed.
Common examples:
t_user (table name), index_name (index name) id, name, address (all field names) in the example.
Use the create statement to create a normal index

create index index_name on t_user (name);

Use the alter statement to create a normal index

alter table t_user add index index_name(name);

Create a normal index when creating a table

create table t_user(id int,name varchar(64),address text,index index_name (name));

2. Primary key index

The primary key index is the fastest query among all the indexes in the database, and each data table can only have one primary key index, but can have multiple primary key columns (ie composite primary key). The column of the primary key index does not allow duplicate data, nor does it allow null values.
Common examples:
t_user (table name), index_name (index name) id, name, address (all field names) in the example.
Create means to set the primary key. (The primary key will automatically create a primary key index)

create table t_user(id int,name varchar(64),address text,primary key(id));

Use alter statement to add primary key

alter table t_user add primary key(id);

3. Unique index

A unique index requires that all values ​​of the column to which the index is added cannot be duplicated. Unique indexes allow null values ​​(note that they are different from primary key indexes). If it is created with a composite index, the combination of column values ​​must be unique. Adding a unique key will automatically create a unique index.
Common examples:
t_user (table name), index_name (index name) id, name, address (all field names) in the example.
Create a unique index using the create statement

create unique index index_name on t_user(name);

Create a unique index using the alter statement

alter table t_user add unique index_name(name);

Create means to create a unique index

create table t_user(id int,name varchar(64),address text,unique index_name(name));

4. Composite index

Composite index is also called combined index, which means that we use multiple fields when building an index. The composite index needs to meet the leftmost principle when used with the select statement, otherwise the index will be invalid.
Common examples:
t_user (table name), index_name (index name) id, name, address (all field names) in the example.
Create a composite index using the create statement

create index index_name on t_user(name, address);

Create a compound index using the alter statement

alter table t_user add index index_name(name, address);

Creating a table is creating a composite index

create table t_user(id int,name varchar(64), address text,index index_name(name, address));

5. Full text index

Full-text indexing is mainly used to solve the problem of fuzzy matching in the case of large amounts of data. If the amount of data in a certain field in the database is large, if we want to use like+wildcard to search, the speed will become very slow. For this situation, we can use full-text indexing to speed up fuzzy queries. Of course, if your project data scale is large enough, it is recommended to introduce search engines such as ElasticSearch, Solr, MeiliSearch to realize full-text retrieval. The principle of full-text indexing is to analyze keywords and their frequency of occurrence in the text through word segmentation technology, and to build indexes in turn. The use of full-text index is closely related to mysql database version, data table engine and even field type. The main restrictions are as follows:
(1) Full-text index is only supported after MySQL3.2.
(2) After MySQL5.7, MySQL has a built-in ngram plug-in, and the full-text index only starts to support Chinese.
(3) For versions prior to MySQL 5.6, only the MyISAM engine supports full-text indexing.
(4) For versions after MySQL 5.6, both the MyISAM engine and the InnoDB engine support full-text indexing.
(5) Only fields whose data types are char, varchar, and text support adding full-text indexes.
Common examples:
t_user (table name), index_name (index name) id, name, address (all field names) in the example.
Use the create statement to create a full-text index

create fulltext index index_name on t_user(address);

Create a full-text index using the alter statement

alter table t_user add fulltext index_name(address);

Create a full-text index when creating a table

create table t_user(id int,name varchar(64), address text,fulltext index_name(address));

2. Index deletion

There are two different ways to delete indexes in the mysql database: to delete the primary key index and to delete other indexes.
Commonly used deletion index examples:
t_user (table name), index_name (index name) id, name, address (all field names) in the example.
delete primary key index

alter table t_user drop primary key;

Drop other indexes using the drop statement

drop index index_name on t_user;

Use the alter statement to drop other indexes

alter table t_user drop index index_name;

3. Index failure scenario

Just knowing how to create and delete indexes is not enough. To make good use of indexes, you must first figure out which situations will cause the index to fail. After figuring out these things, you can deliberately avoid these scenarios when writing SQL to make your SQL uses indexes. The following describes some scenarios of index failure. The meanings of the fields in the red box are as follows: id: This is the ID value of the execution plan. The larger the value, the higher the execution priority. select_type: the type of the current query statement, with the following values: simple: simple query. primary: The outer query of the complex query. subquery: The subquery included in the query statement. derived: The subquery included in FROM. table: Indicates that the current execution plan is executed based on that table. type: the type of the current execution plan query, there are several situations: all: indicates that the entire table query is executed, the index is not hit or the index is invalid. system: Indicates that there is only one piece of data in the table to be queried. const: Indicates that the query condition of the current SQL statement can hit the index query. range: Indicates that the current query operation is to check a certain range. eq_ref: Indicates that it is currently doing multi-table association queries. ref: Indicates that a normal index query is currently used. index: Indicates that SQL currently uses auxiliary index queries. possible_keys: When executing SQL, the index that the optimizer may choose (not necessarily used in the final execution). key: The index name used when the query statement is executed. key_len: This indicates the number of bytes used by the index field. ref: This shows the type of query that was used. rows: How many rows of data may be scanned by the current query statement to retrieve the results.
在介绍索引失效场景之前,大家还需要了解explain工具,以下所有的示例都是使用explain命令来查看是否使用索引,当在一条SQL前加上explain命令,执行这条SQL后会列出相应的执行计划。如下图。
insert image description here


















Extra:
Here are some additional index usage information recorded, there are several states: using index: Indicates that the covering index query is currently used (to be discussed later). using where: indicates that the where clause query is used, and usually indicates that no index is used. using index condition: Indicates that the query condition uses the first few fields of the joint index. using temporary: Indicates that a temporary table is used to process query results. using filesort: Indicates to sort by means other than the index field, which is less efficient. select tables optimized away: indicates that the aggregate function is used on the index field.
对于上述这么多的字段,其实目前不需要完全弄懂,本文只需要记住里面的type字段即可,all表示走全表扫描,const、ref、range等表示通过索引查询。

1. The or keyword in the query causes the index to fail

Example:

explain select * from t_user where id='1' and name='lilei'; 

As shown in the figure below, when using and to connect two query conditions, type=const means that the index is used.
insert image description here

As shown in the figure below, when using or to connect two query conditions, although indexes are created for both id and name, but type=all means that a full table scan is performed and the index becomes invalid.
insert image description here

2. In the fuzzy query, like starts with %, causing the index to fail

When performing fuzzy queries, using like '%XX' or like '%XX%' to query will cause the index to become invalid.
Example:

explain select * from t_user where name like '%lilei';

As shown in the figure below, starting with %, although the name field has created an index, but type=all, it means that a full table scan has been performed, and the index is invalid.
insert image description here

As shown in the figure below, if type=range ends with %, it means that the range scan is performed and the index is not invalidated.
insert image description here

3. Without quotation marks when querying character types, the index will fail

Example:

explain select * from t_user where name = 111;

As shown in the figure below, the name field is a character type, but when querying, the value is not quoted with type=all, causing the index to fail.
insert image description here

As shown in the figure below, after adding quotation marks to the name value, type=ref, and the index takes effect.
insert image description here

4. The index field participates in the calculation or uses the function to cause the index to fail

Example:

--索引字段参与计算
explain select * from t_user where name + 1 = 'lilei';
--索引字段使用函数
explain select * from t_user where substring(name,0,2) = 'li';

As shown in the figure below, the +1 operation is performed on the name field, the full table scan is performed on type=all, and the index becomes invalid.
insert image description here

As shown in the figure below, the name field uses the substring function, type=all scans the entire table, and the index fails.

insert image description here

5. Violation of the leftmost prefix principle leads to index failure

When using a composite index, the leftmost prefix principle must be met, otherwise the index will fail.
Example: (email, is_active two fields create a composite index)

explain select * from t_user where email = 'li' and is_active='1';

As shown in the figure below, when the query condition does not include the email field, the leftmost prefix principle is violated, type=all scans the entire table, and the index becomes invalid.
insert image description here

As shown in the figure below, when the query condition includes the email field, type=ref, the index takes effect.
insert image description here

6. The comparison of different field values ​​​​causes the index to fail

Query some values ​​from a table, and then compare them with other tables. This business scenario is also relatively common. For simple implementation, use the name and address fields to simulate a scene example of field comparison
:

explain select * from t_user where name = address;

As shown in the figure below, due to the comparison between the two fields of name and address, although the two fields have been indexed, but type=all in the execution plan, the full table scan is performed, and the index becomes invalid.
insert image description here

7. Reverse range operation causes index invalidation

Generally speaking, if the SQL is a forward range query, such as >, <, between, like, in... and other operations, the index can take effect normally, but if the SQL performs a reverse range operation, such as NOT IN, NOT LIKE, IS NOT NULL, !=, <>... and other operations will cause the index to become invalid. Example:
但你一定要记住这并非绝对了,随着数据库优化器的逐步完善,这种失效的情况也在逐步改善,所以本条只做参考,一切以执行计划结果为准。

explain select * from t_user where name <> 'lilei';

As shown in the figure below, after the <> operation is used, the type of the execution plan is all, and the full table scan is performed, and the index becomes invalid.
insert image description here

As shown in the figure below, after using the = sign to operate, the type of the execution plan is rel, and the index takes effect.
insert image description here

8. The optimizer evaluates that using a full table scan is faster than using an index, and the index becomes invalid

There is also a special situation in MySQL that will cause the index to fail, that is, when the number of rows scanned by the index exceeds 30% of the number of rows in the table, the MySQL optimizer will think that the full table scan is faster, and will give up the index query by default. However, the full table scan method is used to retrieve data, so in this case, the sequential disk IO of the index may not necessarily be as fast as the random disk IO of the full table.

9. Inconsistent character sets lead to index failure

The Mysql database will compare the character set when using it. If the character set of the data table is inconsistent, the index may become invalid. The mysql database recommends using the utf8mb4 character set.

Summarize

Word document download address: Mysql database index creation, index deletion, and index failure scenario detailed explanation

Guess you like

Origin blog.csdn.net/ma286388309/article/details/129578979
Recommended