当前位置:网站首页>【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.
边栏推荐
猜你喜欢

1349. 参加考试的最大学生数 状态压缩

Bit rate vs. resolution, which one is more important?
![Chapter 09 Use of Performance Analysis Tools [2. Index and Tuning] [MySQL Advanced]](/img/e7/61863c356e4c7bb03ba27865ee7be0.png)
Chapter 09 Use of Performance Analysis Tools [2. Index and Tuning] [MySQL Advanced]

【七夕如何根据情侣倾听的音乐进行薅羊毛】背景音乐是否会影响情侣对酒的选择

tcp中的三次握手与四次挥手

自定义线程池

Leetcode刷题——22. 括号生成

详细全面的postman接口测试实战教程

Interview summary: Why do interviewers in large factories always ask about the underlying principles of Framework?

MySQL learning
随机推荐
亚马逊云科技 + 英特尔 + 中科创达为行业客户构建 AIoT 平台
GCC: compile-time library path and runtime library path
EBS uses virtual columns and hint hints to optimize sql case
Lattice PCIe Learning 1
day14--postman interface test
linux(centOs7)部署mysql(8.0.20)数据库
刷爆朋友圈,Alibaba出品亿级并发设计速成笔记太香了
第09章 性能分析工具的使用【2.索引及调优篇】【MySQL高级】
1349. Maximum number of students taking the exam Status Compression
JZ搜索引擎solr研究-从数据库创建索引
AI+PROTAC|dx/tx完成500万美元种子轮融资
<开发>实用工具
【七夕如何根据情侣倾听的音乐进行薅羊毛】背景音乐是否会影响情侣对酒的选择
工具类总结
Creative code confession
记录谷歌gn编译时碰到的一个错误“I could not find a “.gn“ file ...”
PHP技能评测
LiveVideoStackCon 2022 Shanghai Station opens tomorrow!
dotnet 6 为什么网络请求不跟随系统网络代理变化而动态切换代理
.Net C# 控制台 使用 Win32 API 创建一个窗口