当前位置:网站首页>【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.
边栏推荐
- <开发>实用工具
- 执掌图表
- 行业现状?互联网公司为什么宁愿花20k招人,也不愿涨薪留住老员工~
- Transfer Learning - Joint Geometrical and Statistical Alignment for Visual Domain Adaptation
- 第十一章 开关级建模
- source program in assembly language
- CMS建站流程
- 迁移学习——Joint Geometrical and Statistical Alignment for Visual Domain Adaptation
- Exploding the circle of friends, Alibaba produced billion-level concurrent design quick notes are too fragrant
- Transfer Learning - Distant Domain Transfer Learning
猜你喜欢
### Error querying database. Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionExcep
张驰咨询:揭晓六西格玛管理(6 Sigma)长盛不衰的秘密
A new technical director, who calls DDD a senior, is convinced
(17) 51 MCU - AD/DA conversion
程序员失眠时的数羊列表 | 每日趣闻
Method Overriding and Object Class
树形查找(二叉查找树)
Activity Recommendation | Kuaishou StreamLake Brand Launch Conference, witness together on August 10!
[Unity Entry Plan] Handling of Occlusion Problems in 2D Games & Pseudo Perspective
hypervisor相关的知识点
随机推荐
VOC格式数据集转COCO格式数据集
原生js实现多选框全部选中和取消效果
(十七)51单片机——AD/DA转换
ORA-00604 ORA-02429
安装oracle11的时候为什么会报这个问题
Methods commonly used interface automation test framework postman tests
MySQL3
英特尔 XDC 2022 精彩回顾:共建开放生态,释放“基建”潜能
dotnet 6 为什么网络请求不跟随系统网络代理变化而动态切换代理
Live playback including PPT download | Build Online Deep Learning based on Flink & DeepRec
hypervisor相关的知识点
Gartner Hype Cycle:超融合技术将在2年内到达“生产力成熟期”
一文看懂推荐系统:召回06:双塔模型——模型结构、训练方法,召回模型是后期融合特征,排序模型是前期融合特征
快速批量修改VOC格式数据集标签的文件名,即快速批量修改.xml文件名
[Redis] Redis installation under Linux
Difference between MBps and Mbps
刷爆朋友圈,Alibaba出品亿级并发设计速成笔记太香了
记录谷歌gn编译时碰到的一个错误“I could not find a “.gn“ file ...”
张驰咨询:揭晓六西格玛管理(6 Sigma)长盛不衰的秘密
How to create an rpm package