当前位置:网站首页>Possible causes of index failure
Possible causes of index failure
2022-07-27 22:50:00 【Stick stick eat not fat】
Index is mainly to improve the efficiency of data query , But in some cases, indexing will reduce query efficiency , Increase cost .
1) The data magnitude is too small . The index itself also takes up a certain amount of space , Too little data , Using an index doesn't make much sense .
2) Frequently updated fields . The construction of index 、 maintain 、 Deleting also consumes system resources .
3) Fields with low discrimination . Like gender , Generally, there are only two choices , It's difficult for the index to query the desired data only by gender .
Get down to business , The possible causes of index invalidation are as follows :
Suppose the primary key column pri
Secondary index column mul mul1 mul2
No index column noidx no_index_col
1) The query criteria include or And the field column does not contain an index
select * from test where mul=1 or noidx=2;
Secondary index +or+ Indexed columns : I will take the index first , But columns without indexes will be scanned in the whole table , So it's better not to go through the index , Directly scan the whole table .
If or There are indexes before and after , Then maybe go to the index , Or maybe it's not indexed .
2) Implicit conversion leads to index invalidation
Suppose that when a mobile phone number column is created num varchar(15)
In the query ,where The condition column is followed by a string , Use quotation marks
select * from test where num='13911111111';
If not quoted , Is the number , Character types do not match , Don't walk index .
3)like And % Is the prefix's non overlay index
% in front , Don't walk index
select * from test where mul like "%abc";
% Not in front , Go to the index
select * from test where mul like "abc%";
Use overlay index ,% in front , Also go to the index
select pri,mul from test where mul like "%abc";
4) The union index is created and the query does not meet the leftmost matching principle , Index may fail , It may still work . There are many examples of index failure , Here is an example , Express The index is still valid .
Notice the union index I created ,name before ,id After .
CREATE TABLE a(
id INT NOT NULL,
NAME VARCHAR(5)
)ENGINE=INNODB,CHARSET=utf8
ALTER TABLE a ADD INDEX idx_name_id(NAME,id);
Obtaining an execution plan :
DESC SELECT * FROM a WHERE NAME='a' AND id <2;

Obtaining an execution plan :
DESC SELECT * FROM a WHERE id <2 AND NAME='a';

Obtaining an execution plan :
DESC SELECT * FROM a WHERE id =3;

5) Perform numerical operations on indexed columns , Index failure
CREATE TABLE `user`(
`id` INT(11) NOT NULL AUTO_INCREMENT,
`userId` VARCHAR(32) NOT NULL,
`age` INT(11) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `idx_age`(`age`) USING BTREE
)ENGINE=INNODB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;
Get index execution plan :
DESC SELECT * FROM USER WHERE age+1=8;

6) Used in index It's not equal to The symbol of , Index may fail
Include <> != not in
It should be noted that , If it is a clustered index column , Use the above symbols , Still can walk the index .
7) use or The two connected contain null Index field , Don't walk index .
for example :
select * from test where mul1 is null or mul2 is not null;
however , A single index contains null Field , It's indexed .
select * from test where mul1 is null;
8) No query criteria , Or the column of the query criteria has no index , No index
for example :
select * from test;
select * from test where no_index_col=3;
9)Mysql When the optimizer analysis of believes that the full table scan is faster than the index , For example, the result set of the query is not less than the total data 25%-30% when , It believes that there is no need to index .
10) The index itself fails .
Although the index has the ability to maintain itself , But when the content of the data table is modified and updated frequently , It is also possible that the index fails , You need to delete the index , Re index .
Reference article :
Oldguo- Benchmarking class -MySQL-lesson04- Index and execution plan
边栏推荐
- It's time to say goodbye gracefully to nullpointexception
- 初中三年回忆录
- Build your own website (22)
- 三星存储工厂又发生火灾!
- 2022/6/9 考试总结
- US officials suggested trump prevent Infineon from acquiring cypress
- Metersphere financial company landing experience sharing
- The follow-up is coming. Whether it's OK without reference, let's make it clear to everyone at once!
- 联合省选2022复习计划
- Cy3荧光标记抗体/蛋白试剂盒 (10~100mg标记量)
猜你喜欢

Cy3荧光标记抗体/蛋白试剂盒 (10~100mg标记量)

How to quickly pass the probation period for newly trained intermediate test engineers

ConvNeXt:A ConvNet for the 2020s——模型简述

Starfish OS X metabell strategic cooperation, metauniverse business ecosystem further

七大排序之希尔排序

jumpserver学习

多肽KC2S修饰白蛋白纳米粒/靶向肽GX1修饰人血清白蛋白纳米粒探针的研究制备

Credit default prediction based on simplified scorecard, smote sampling and random forest
In depth understanding of redis master-slave principle

2022年五大网络管理趋势
随机推荐
Nodejs NPM common instructions summary
PX4模块设计之十三:WorkQueue设计
2022/3/11 考试总结
Tab bar (addeventlistener and onclick practice, used with bind method, exponential growth to listen for events)
Window localstorage properties and location objects
云计算服务主要安全风险及应对措施
Nodejs npm常用指令总结
浅析云原生应用安全组织架构
It is said that Intel will adopt TSMC 6nm EUV process next year
蓝牙框架总结
cache学习
2022年软件开发的趋势
传华为再度砍单!供应链厂商更难了
The ASML lithography machine purchased by SMIC international entered the factory smoothly, but it is not a non EUV lithography machine!
10 years of technical career, those technical books that make me excited
Hill sort of seven sorts
2022/5/13 考试总结
2022 / July daily report
IELTS Listening - Jianya 5 - text1
Window localStorage 属性和Location 对象