MySQL database - Index (5) - Index usage (Part 1), verify index efficiency, leftmost prefix rule, range query, index failure, SQL prompts

Table of contents

Index usage

Verify index efficiency

leftmost prefix rule

range query

Index failure

Index column operations

String without quotes

fuzzy query

or connection conditions

Data distribution impact

SQL prompt 

use index

ignore index

force index


Index usage (Part 1)

Verify index efficiency

Before explaining the principles of using indexes, let's first verify the index to see if the index can be used to improve data query performance.

During the demonstration, we still used the previous table tb_sku, and prepared 10 million records in this table.

In this table, id is the primary key and has a primary key index, but other fields are not indexed. Let's first query one of the records to see the fields inside, and execute the following SQL:

select * from tb_sku where id = 1\G;
-- \G使行列互换,展示得更加清晰

search result: 

It can be seen that even if there are 10 million data, the performance of data query based on id is still very fast, because the primary key id is indexed .

So next, we will query based on the sn field and execute the following SQL (the sn field is not indexed):

SELECT * FROM tb_sku WHERE sn = '100000003145001';

 search result:

We can see that the query based on the sn field returned a piece of data, and the result took 20.78sec . This is because sn has no index , resulting in low query efficiency .
Then we can create an index for the sn field. After establishing the index, we query based on sn again, and then take a look at the query time consumption.

Create index:

create index idx_sku_sn on tb_sku(sn);

The process of creating this index may take a long time, because there is a lot of data in the table, and it takes a long time to build a B+Tree tree.

After creating this index, we execute the same SQL statement again to see its time consumption.

SELECT * FROM tb_sku WHERE sn = '100000003145001'\G;

We will obviously see that after the sn field is indexed, the query performance is greatly improved. Before and after indexing, the query time is not the same order of magnitude.

leftmost prefix rule

If multiple columns are indexed (joint index), the leftmost prefix rule must be followed.

The leftmost prefix rule means that the query starts from the leftmost column of the index and does not skip columns in the index. If a column is skipped, the index will be partially invalid (the subsequent field indexes will be invalid).

For example, there is currently a joint index in the tb_user table. This joint index involves three fields, in order: profession, age, status.
The leftmost prefix rule means that when querying, the leftmost column, that is, profession, must exist, otherwise all indexes will fail. And a column cannot be skipped in the middle, otherwise the field index behind the column will be invalid.

for example:

When querying, if profession exists, age does not exist, and status exists, then only the profession index will take effect at this time;

If profession does not exist, age exists, and status exists, then the index will not be used at this time;

Note: The leftmost prefix rule has nothing to do with the order in which query conditions are written, but only with the order in which joint indexes are created.

Right now,

explain select * from tb_user where 
profession = '软件工程' 
and age = 31 
and status = '0';

-- 这两条SQL是一致的

explain select * from tb_user where 
age = 31 
and status = '0' 
and profession = '软件工程';

range query

In the joint index, a range query (>,<) occurs, and the column index on the right side of the range query becomes invalid.

For example:

explain select * from tb_user where 
profession = '软件工程' 
and age > 30 
and status = '0';  -- status在>之后,所以该列索引失效

So only the profession and age fields are indexed.

When business permits, use range queries like >= or <= as much as possible, and avoid using > or <.

Index failure

  • Index column operations

Do not perform operations on index columns, the index will become invalid.

For example, if you perform a function operation on the phone field, its index will be invalid:

explain select * from tb_user where substring(phone,10,2) = '15';

  • String without quotes

explain select * from tb_user where 
profession = '软件工程' 
and age = 31 
and status = '0';

--  没有加引号的话,status字段的索引就会失效

explain select * from tb_user where 
profession = '软件工程' 
and age = 31 
and status = 0;

There is no impact on the query results, but there is implicit type conversion in the database and the index will become invalid.

  • fuzzy query

If there is only a tail fuzzy match, the index will not be invalidated. If it is a header fuzzy match, the index will be invalid.

For example:

explain select * from tb_user where profession like '软件%';
explain select * from tb_user where profession like '%工程';  -- 索引失效
explain select * from tb_user where profession like '%工%';   -- 索引失效

In the like fuzzy query, add % after the keyword to make the index effective. And if % is added in front of the keyword, the index will be invalid.

  • or connection conditions

Conditions separated by or, if the column in the condition before or has an index, but there is no index in the following column, then the indexes involved will not be used.

For example, the following two SQL statements:

explain select * from tb_user where id = 10 or age = 23;
explain select * from tb_user where phone = '17799990017' or age = 23;
-- age没有索引,其作为联合索引也不满足最左前缀法则

Since age has no index, even if id and phone have indexes, the indexes will be invalid. Therefore, it is necessary to create an index for age.

The index will only take effect when the conditions of the OR connection include indexes on the left and right fields.

  • Data distribution impact

If MySQL evaluates that using an index is slower than the full table, the index is not used.

When MySQL queries, it will evaluate the efficiency of using the index and the efficiency of a full table scan. If a full table scan is faster, it will abandon the index and use a full table scan. Because the index is used to index a small amount of data, if a large amount of data is returned through an index query, it is not as fast as a full table scan, and the index will become invalid.

SQL prompt 

SQL prompts are an important means of optimizing the database. Simply put, they add some artificial prompts to SQL statements to achieve the purpose of optimizing operations.

When MySQL queries externally, if there are multiple indexes that may be used, MySQL will automatically select an index to use. Can we specify which index to use when querying? The answer is yes, and this can be done with the help of MySQL's SQL prompt.

Next, introduce the SQL prompt.

  • use index

Recommend which index MySQL uses to complete this query (just a suggestion, mysql will evaluate it again internally).

explain select * from tb_user use index(idx_user_pro)
where profession = 'Software Engineering';

  • ignore index

Ignore the specified index.

explain select * from tb_user ignore index(idx_user_pro)

where profession = 'Software Engineering';

  • force index

Force the use of indexes.

explain select * from tb_user force index(idx_user_pro)

where profession = 'Software Engineering';


END 


Learn from: Dark Horse Programmer - MySQL Database Course

Guess you like

Origin blog.csdn.net/li13437542099/article/details/133217910