MySQL index, why does the index fail?

After creating the index, when using the index field to perform filtering queries, the index will become invalid in some cases. Let’s take a look at it below.

Under what circumstances will the index fail?

In order to do the following tests, first rebuild the xuesheng table and add a field email:

drop table if exists xuesheng;
-- 新建学生表
create table xuesheng(
id int primary key auto_increment,
name varchar(20),
age tinyint unsigned,
email varchar(50),
banji_id int,
foreign key(banji_id) references banji(id)
);
insert into xuesheng(name,age,email,banji_id)
values
('张三',22,'[email protected]',1),
('李四',25,'[email protected]',1),
('王五',18,'[email protected]',2),
('赵六',31,'[email protected]',2),
('孙七',18,'[email protected]',3),
('张三',20,'[email protected]',3);my

Then create an index on the name field:

create index idx_name on xuesheng(name);

Queries using the name field can use this index:

explain
select id,name,email
from xuesheng
where name='123';

Reason for failure:

Implicit or explicit type conversion

When filtering data in index fields, if there is implicit data type conversion, the index will become invalid.

In the following example, name is a string type, and 123 is an integer type. At this time, MySQL will try to add the name field to the

The value is converted to a numeric type, which will cause index failure

explain
select id,name,email
from xuesheng
where name=123;

Perform operations on index fields

In the filter conditions, any operation on the index field will cause the index to fail:

explain
select id,name,email
from xuesheng
where left(name, 1)='张';

like left fuzzy query

Using like to perform left fuzzy queries on strings will cause index failure:

explain
select id,name,email
from xuesheng
where name like '%张%';

Right fuzzy query can use index, which is equivalent to a range query

or connect non-index field conditions

or The index will fail when connecting non-index field conditions:

explain
select id,name,email
from xuesheng
where name='张三' or email='[email protected]';

leftmost prefix principle

When using an index with multiple field combinations, the best situation is when all index field conditions occur simultaneously.

The leftmost prefix principle is for multi-field composite indexes. It means that the first field condition in a multi-field composite index must be

The index must be present to take effect, otherwise the index will be invalid. In addition, the index field conditions must be continuous, skip the middle field, and then

The conditions of the fields above will be invalid.

In the following example, we want to create a combined index of the three fields banji_id, age, and name, and an index of the name field.

If you don’t use it temporarily, delete it first:

drop index idx_name
on xuesheng;

Create a combined index of the three fields banji_id, age, and name:

create index idx_banji_age_name
on xuesheng(banji_id,age,name);

For a combined index, the best case scenario is that all index field conditions occur:

explain
select id,name,email
from xuesheng
where banji_id=1 and age=22 and name='张三';

The meaning of the key_len field is the field type of the three fields, and the total number of bytes is 90 bytes.

The order of the three conditions does not matter. The result is the same if the conditions are rearranged.

explain
select id,name,email
from xuesheng
where name='张三' and banji_id=1 and age=22;

If the first field condition is missing, that is, the condition of the banji_id field, the index will fail:

explain
select id,name,email
from xuesheng
where name='张三' and age=22;

The value of the key_len field is 5, which is the byte size of the banji_id field.

Two consecutive field conditions:

explain
select id,name
from xuesheng
where banji_id=1 and age=22;

The value of the key_len field is 7, which is the byte size of the banji_id and age fields.

If you skip the middle age field and use banji_id and name field conditions, the name condition index after the skipped age field will be invalid:

explain
select id,name
from xuesheng
where banji_id=1 and name=22;

The value of key_len is 5, which is only the byte size of the banji_id field, indicating that the name field index is invalid.

The index condition behind the large-small comparison operation is invalid.

When using a multi-field composite index, if the middle field uses a comparison operation, the index of the following fields will be invalid:

explain
select id,name,email
from xuesheng
where banji_id=1 and age>20 and name='张三';

You can try to test it yourself~

The value of key_len is 7, which is the number of bytes of the banji_id and age fields. The index of the name field is invalid.

As a bonus for this article, you can receive free C++ learning materials package, technical videos/codes, and 1,000 interview questions from major manufacturers, including (C++ basics, network programming, database, middleware, back-end development, audio and video development, Qt development)↓↓↓ ↓↓↓See below↓↓Click at the bottom of the article to get it for free↓↓

Guess you like

Origin blog.csdn.net/m0_60259116/article/details/133169374