当前位置:网站首页>【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.
边栏推荐
猜你喜欢
Binary tree [full solution] (C language)
汇编语言之源程序
pytorch的使用:卷积神经网络模块
1349. Maximum number of students taking the exam Status Compression
Log an error encountered when compiling google gn "I could not find a ".gn" file ..."
安装oracle11的时候为什么会报这个问题
10年测试经验,在35岁的生理年龄面前,一文不值
Jin Jiu Yin Shi Interview and Job-hopping Season; Are You Ready?
Introduction to JVM class loading
详细全面的postman接口测试实战教程
随机推荐
pytorch的使用:卷积神经网络模块
VOC格式数据集转COCO格式数据集
【Redis】Linux下Redis安装
《.NET物联网从零开始》系列
(17) 51 MCU - AD/DA conversion
torch.autograd.grad finds the second derivative
安装oracle11的时候为什么会报这个问题
直播预告|30分钟快速入门!来看可信分布式AI链桨的架构设计
source program in assembly language
Why is this problem reported when installing oracle11
Is DDOS attack really unsolvable?Do not!
XMjs跨域问题解决
day14--postman接口测试
AI+PROTAC|dx/tx完成500万美元种子轮融资
Object.defineProperty实时监听数据变化并更新页面
Methods commonly used interface automation test framework postman tests
GCC: compile-time library path and runtime library path
新唐NUC980使用记录:在用户应用中使用GPIO
Utilities 迅睿cms网站搬迁换了服务器后网站不能正常显示