How to understand and use MySql indexes correctly

1 Overview

The index is a data structure used by the storage engine to quickly find records. By using the database index reasonably, the access performance of the system can be greatly improved. The following mainly introduces the index types in the MySql database and how to create a more reasonable and efficient index. Skill.

Note: This is mainly for the B+Tree index data structure of the InnoDB storage engine

                      

2. The advantages of indexing

1. Greatly reduce the amount of data that the server needs to scan, thereby improving the retrieval speed of data

2. Help the server avoid sorting and temporary tables

3. Random I/O can be changed to sequential I/O

3, the creation of the index

3.1, primary key index

ALTER TABLE 'table_name' ADD PRIMARY KEY 'index_name' ('column');

3.2, unique index

ALTER TABLE 'table_name' ADD UNIQUE 'index_name' ('column');

3.3, common index

ALTER TABLE 'table_name' ADD INDEX 'index_name' ('column');

3.4. Full-text indexing

ALTER TABLE 'table_name' ADD FULLTEXT 'index_name' ('column');

3.5. Combined index

ALTER TABLE 'table_name' ADD INDEX 'index_name' ('column1', 'column2', ...);

4. Indexing rules of B+Tree

Create a test user table

DROP TABLE IF EXISTS user_test;
CREATE TABLE user_test(
	id int AUTO_INCREMENT PRIMARY KEY,
	user_name varchar(30) NOT NULL,
	sex bit(1) NOT NULL DEFAULT b'1',
	city varchar(50) NOT NULL,
	age int NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

Create a composite index: ALTER TABLE user_test ADD INDEX idx_user(user_name , city , age);

4.1. Query with efficient index

4.1.1, full value matching

Full-value matching refers to matching with all columns in the index. For example, taking the index created above as an example, after the where condition, the data with the condition (user_name, city, age) can be queried at the same time.

Note: It has nothing to do with the order of query conditions after where. This is a place that many students easily misunderstand.

SELECT * FROM user_test WHERE user_name = 'feinik' AND age = 26 AND city = '广州';

4.1.2, match the leftmost prefix

Matching the leftmost prefix refers to matching the leftmost index column first. For example, the index created above can be used for query conditions: (user_name ), (user_name, city), (user_name , city , age)

Note: The order of satisfying the leftmost prefix query condition has nothing to do with the order of index columns, such as: (city, user_name), (age, city, user_name)

4.1.3, matching column prefix

Refers to the beginning of the matching column value, such as: query all users whose username starts with feinik

SELECT * FROM user_test WHERE user_name LIKE 'feinik%';

4.1.4. Matching range value

For example: to query all users whose usernames start with feinik, the first column of the index is used here

SELECT * FROM user_test WHERE user_name LIKE 'feinik%';

4.2. Index restrictions

1. If the where query condition does not include the leftmost index column in the index column, the index query cannot be used, such as:

SELECT * FROM user_test WHERE city = '广州';

or

SELECT * FROM user_test WHERE age= 26;

or

SELECT * FROM user_test WHERE city = '广州' AND age = '26';

2. Even if the query condition of where is the leftmost index column, you cannot use the index to query the user whose username ends with feinik

SELECT * FROM user_test WHERE user_name like '%feinik';

3. If there is a range query of a certain column in the where query condition, all the columns on the right cannot use the index to optimize the query, such as:

SELECT * FROM user_test WHERE user_name = 'feinik' AND city LIKE '广州%' AND age = 26;

5. Efficient indexing strategy

5.1. The index column cannot be part of the expression, nor can it be used as the parameter of the function, otherwise the index query cannot be used.

SELECT * FROM user_test WHERE user_name = concat(user_name, 'fei');

5.2, prefix index

Sometimes it is necessary to index long character columns, which will increase the storage space of the index and reduce the efficiency of the index. One strategy is to use a hash index, and the other is to use a prefix index. The prefix index is the first choice for character columns. n characters are used as the index, which can greatly save the index space and improve the indexing efficiency.

5.2.1. Selectivity of prefix index

The prefix index should select a prefix that is long enough to ensure high selectivity, but not too long. We can calculate the appropriate length of the prefix index in the following ways:

(1)

SELECT COUNT(DISTINCT index_column)/COUNT(*) FROM table_name; -- index_column代表要添加前缀索引的列

Note: The selectivity ratio of the prefix index is calculated by the above method. The higher the ratio, the more efficient the index is.

(2)

SELECT

COUNT(DISTINCT LEFT(index_column,1))/COUNT(*),

COUNT(DISTINCT LEFT(index_column,2))/COUNT(*),

COUNT(DISTINCT LEFT(index_column,3))/COUNT(*)

...

FROM table_name;

Note: Gradually find the selectivity ratio closest to the prefix index in (1) through the above statement, then you can use the corresponding character interception length to do the prefix index

5.2.2, the creation of prefix index

ALTER TABLE table_name ADD INDEX index_name (index_column(length));

5.2.3. Notes on using prefix index

Prefix index is an effective way to make the index smaller and faster, but MySql cannot use prefix index to do ORDER BY and GROUP BY and use prefix index to do cover scan.

5.3, choose the appropriate index column order

The order of the index columns is very important in the creation of a composite index. The correct index order depends on the query method using the index. For the index order of a composite index, a rule of thumb can help us: put the most selective columns in the index. Foremost, this rule is consistent with the selective method of prefix indexing, but it does not mean that the order of all composite indexes can be determined using this rule, and the specific index order needs to be determined according to the specific query scenario.

5.4 Clustered and non-clustered indexes

1. Clustered index

The clustered index determines the physical ordering of data on the physical disk. A table can only have one clustered index. If the primary key is defined, InnoDB will use the primary key to cluster the data. If the primary key is not defined, InnoDB will choose a unique non-null index instead. , if there is no unique non-null index, InnoDB will implicitly define a primary key as a clustered index.

The clustered index can greatly improve the access speed, because the clustered index saves the index and row data in the same B-Tree, so when the index is found, the corresponding row data is found accordingly, but when using the clustered index At this time, it is necessary to avoid random clustered indexes (generally refers to the discontinuous primary key value and the uneven distribution range). If UUID is used as the clustered index, the performance will be poor, because the discontinuous UUID value will lead to a lot of index fragmentation and Random I/O, which eventually leads to a dramatic drop in query performance.

2. Non-clustered index

Unlike the clustered index, the non-clustered index does not determine the physical ordering of the data on the disk, and the B-Tree contains the index but does not contain row data. The row data is only pointed to by the pointer corresponding to the index stored in the B-Tree. Row data, such as: the index established above on (user_name, city, age) is a non-clustered index.

5.5. Covering Index

If an index (such as a composite index) contains the values ​​of all the fields to be queried, then it is called a covering index, such as:

SELECT user_name, city, age FROM user_test WHERE user_name = 'feinik' AND age > 25;

Because the fields to be queried (user_name, city, age) are all included in the index columns of the composite index, a covering index query is used to check whether the covering index is used. It is proved that the covering index is used, and the covering index can greatly improve the access performance.

5.6, how to use the index to sort

In the sorting operation, if the index can be used for sorting, the speed of sorting can be greatly improved. To use the index for sorting, the following two points need to be satisfied.

  • 1. The column order after the ORDER BY clause should be consistent with the column order of the composite index, and the sorting direction (forward/reverse) of all sorting columns should be consistent
  • 2. The queried field value needs to be included in the index column and satisfy the covering index

Analyze with examples

Create a composite index on the user_test table

ALTER TABLE user_test ADD INDEX index_user(user_name , city , age);

Cases where index sorting can be used

1、SELECT user_name, city, age FROM user_test ORDER BY user_name;

2、SELECT user_name, city, age FROM user_test ORDER BY user_name, city;

3、SELECT user_name, city, age FROM user_test ORDER BY user_name DESC, city DESC;

4、SELECT user_name, city, age FROM user_test WHERE user_name = 'feinik' ORDER BY city;

Note: Point 4 is special, if the where query condition is the first column of the index column and is a constant condition, then the index can also be used

Cases where sorting by index cannot be used

1, sex is not in the index column

SELECT user_name, city, age FROM user_test ORDER BY user_name, sex;

2. The direction of the sorting column is inconsistent

SELECT user_name, city, age FROM user_test ORDER BY user_name ASC, city DESC;

3. The field column sex to be queried is not included in the index column

SELECT user_name, city, age, sex FROM user_test ORDER BY user_name;

4. The user_name after the where query condition is a range query, so other columns of the index cannot be used

SELECT user_name, city, age FROM user_test WHERE user_name LIKE 'feinik%' ORDER BY city;

5. In multi-table join query, index sorting can be used only when the sorting fields after ORDER BY are all index columns in the first table (the above two rules for index sorting need to be met). For example, create another user's extended table user_test_ext, and create an index for uid.

DROP TABLE IF EXISTS user_test_ext;

CREATE TABLE user_test_ext(

    id int AUTO_INCREMENT PRIMARY KEY,

    uid int NOT NULL,

    u_password VARCHAR(64) NOT NULL

) ENGINE=InnoDB DEFAULT CHARSET=utf8;

ALTER TABLE user_test_ext ADD INDEX index_user_ext(uid);

go index sort

SELECT user_name, city, age FROM user_test u LEFT JOIN user_test_ext ue ON u.id = ue.uid ORDER BY u.user_name;

Do not go index sorting

SELECT user_name, city, age FROM user_test u LEFT JOIN user_test_ext ue ON u.id = ue.uid ORDER BY ue.uid;

6. Summary

This article mainly talks about the index rules of the B+Tree tree structure, the creation of different indexes, and how to correctly create efficient index skills to improve the query speed as much as possible. More skills need to be constantly accumulated relevant experience.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324376333&siteId=291194637