当前位置:网站首页>【MySQL series】- Does LIKE query start with % will make the index invalid?
【MySQL series】- Does LIKE query start with % will make the index invalid?
2022-08-05 01:52:00 【Sommelier】
There are many situations in which MySQL queries lead to index failure. Searching for MySQL index failure through a search engine almost always mentions a situation, that is, the like query starts with "%".But does LIKE query starting with % necessarily invalidate the index?
Preparing for verification
- Create a users table, the table structure is as follows, there are 999999 pieces of data in the table:
- Create index on name field
Verification Phase
The best way to verify whether an SQL statement uses an index is to use the SQL execution plan EXPLAIN.
- First look at the case where the LIKE query does not start with %:
EXPLAIN SELECT * FROM users WHERE `name` LIKE '001%';
type=range,key=idx_name, indicating that this paragraph uses an index.
- Let's take a look at the case where the LIKE query does not start with %
Look at the first SQL statement first:
EXPLAIN SELECT * FROM users WHERE `name` LIKE '%001%'
type=ALL, so this SQL does not use indexes.
Let's look at the second SQL statement:
EXPLAIN SELECT `name` FROM users WHERE `name` LIKE '%001%';
type=index, key=idx_name, this result indicates that this SQL uses an index.
Then look at the third paragraph of SQL statement:
EXPLAIN SELECT id , `name` FROM users WHERE `name` LIKE '%001%';
The third SQL statement is to query an additional column of id based on the second SQL statement.This execution result is the same as the second paragraph, indicating that this paragraph also uses the index.
Lastly look at the fourth paragraph of SQL statement:
EXPLAIN SELECT id , `name` , gender FROM users WHERE `name` LIKE '%001%';
The fourth SQL statement is to query two more columns id and gender based on the third SQL statement.The execution result is the same as the first paragraph, and no index is used.
Verification Results
It can be found through the above verification that LIKE queries starting with % will not necessarily invalidate the index.
Why does the index go?
First explain, type=ALL and index in the execution plan.And the meaning of key.
- type=ALL: Full table scan, traverse the entire table to query the matching results, without going through the index.
- type=index: Use index coverage, just scan the index tree, faster than ALL.
- type=range: The range access method is used when using an index to perform a range query.
- key: The index actually used, if it is NULL, the index is not used.
The reason why LIKE queries use indexes starting with % is that they use index coverage.
Provides an optimization technique for secondary index MySQL.That is, the queried records can be obtained from the auxiliary index, and there is no need to return to the table and then query the complete records according to the clustered index.One advantage of using index coverage is that the auxiliary index does not contain all the information of the entire row, so its size is much smaller than the clustered index, so it can reduce a lot of IO operations, but the premise is that all columns to be queried must be indexed.
LIKE queries starting with % will cause a full index scan or a full table scan. If there is no index coverage, the queried data will be returned to the table, and an additional IO operation will occur. When MySQL estimates the full table scan or full index scanWhen the time is less than the time spent walking the index, the index will not be walked.With index coverage, there is no need to return to the table, IO operations are reduced, and it takes less time, so the index is used.
Summary
LIKE queries starting with % do not necessarily invalidate the index.If the result of the query contains only the primary key and index fields, the index will be used, and vice versa.
边栏推荐
猜你喜欢
随机推荐
【PyQT5 绑定函数的传参】
Binary tree [full solution] (C language)
Are testing jobs so hard to find?I am 32 this year and I have been unemployed for 2 months. What should an older test engineer do next to support his family?
《.NET物联网从零开始》系列
AI+PROTAC|dx/tx完成500万美元种子轮融资
1349. 参加考试的最大学生数 状态压缩
DDOS攻击真的是无解吗?不!
使用OpenVINO实现飞桨版PGNet推理程序
Log an error encountered when compiling google gn "I could not find a ".gn" file ..."
Chapter 09 Use of Performance Analysis Tools [2. Index and Tuning] [MySQL Advanced]
### Error querying database. Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionExcep
释放技术创新引擎,英特尔携手生态合作伙伴推动智慧零售蓬勃发展
XMjs跨域问题解决
CMS建站流程
“配置”是把双刃剑,带你了解各种配置方法
PHP Skills Assessment
Exploding the circle of friends, Alibaba produced billion-level concurrent design quick notes are too fragrant
Use of pytorch: Convolutional Neural Network Module
超越YOLO5-Face | YOLO-FaceV2正式开源Trick+学术点拉满
A new technical director, who calls DDD a senior, is convinced