当前位置:网站首页>Several types of MySQL index invalidation and their solutions
Several types of MySQL index invalidation and their solutions
2022-06-29 11:18:00 【Migrant worker brother】
Click below “Java Programming duck ” Follow and mark the stars
More exciting First time direct
What are the conditions of index failure ?
Index columns are not independent
Left blur is used
Use or Some fields in the query do not use an index
String conditions are not used ''
Queries that do not conform to the leftmost prefix principle
Index field not added not null constraint
Implicit conversion leads to index invalidation
Index column independence means that the indexed column cannot be part of an expression , Cannot be an argument to a function , For example, the following situation
select id,name,age,salary from table_name where salary + 1000 = 6000;salary The column is evaluated by the user expression , In this case, the index will fail , The solution is to calculate the condition value in advance , Don't let the index column participate in the evaluation .
The index field is used as the parameter of the function
select id,name,age,salary from table_name where substring(name,1,3)= 'luc';What is the solution , You can calculate the conditions in advance , Do not use indexes , Or you can use other sql Replace the above , such as , above sql have access to like Instead of
select id,name,age,salary from table_name where name like 'luc%';Left blur is used
select id,name,age,salary from table_name where name like '%lucs%';Avoid using left blur as much as possible , It can be written like this
select id,name,age,salary from table_name where name like 'lucs%';If you can't avoid left fuzzy query , Consider search engines such as ES
or Some fields in the query do not use an index
select id,name,age,salary from table_name where name ='lucs' and age >25This situation , It can be for name and age All indexed , Otherwise, it will take a full scan .
String conditions are not used ''
select id,name,age,salary from table_name where phone=13088772233The one above sql phone The field type is String type , But it's not used '13088772233 ', SQL Just scan the whole table , So the string index should use ‘’
select id,name,age,salary from table_name where phone='13088772233 'Queries that do not conform to the leftmost prefix principle
For example, there is such a composite index index(a,b,c)
select * from table_name where b='1'and c='2'
select * from table_name where c='2'
// The top two SQL Can't run the index The principle of left , Is to have the leftmost priority , In my absence , You can't play by yourself , Unless you create your own index , Here are a few SQL You can execute the index
select * from table_name where a = 'asaa' and b='1'and c='2'
select * from table_name where a = 'asda' and b='1231'
// The above two items are indexed , But how do you think the index should go , Yes, all go , Or partial indexing ?
select * from table_name where a = 'asda' and c='dsfsdafsfsd'Index field not added not null constraint
select * from table_name where a is null;
// This article sql You can't go index execution ,is null Conditions Index cannot be used , Can only scan the whole table
// mysql The official suggestion is to set the field to not nullSo in this case , stay mysql When creating table fields , You can set the string to be indexed to not null default '' The default is an empty string
Implicit conversion
Implicit conversion will occur if the two field types of the associated table are inconsistent
select * from table_name t1 left join table_name2 t2 on t1.id=t2.tid;
// In the above sentence , If t1 Tabular id The type and t2 Tabular tid When the types are inconsistent , Can not
// Executed by index .
// The solution is to set the field types uniformly .END
After reading this article, there are gains ? Please forward to share with more people
Focus on 「Java Programming duck 」, promote Java Skill
Focus on Java Programming duck WeChat official account , The background to reply : Yard farm gift bag A copy of the latest technical data can be obtained . cover Java Frame learning 、 Architect learning, etc !
If the article helps , Looking at , Forward! .
Thank you for your support (*^__^*)边栏推荐
猜你喜欢

Google Earth Engine(GEE)——GEDI L2A Vector Canopy Top Height (Version 2) 全球生态系统数据集

math_数学表达式&等式方程的变形&组合操作技巧/手段积累

在线文本过滤小于指定长度工具

Data analysis method and Thinking: funnel analysis

西门子S7-200SMART控制步进电机的具体方法及示例程序

【每日3题(1)】判断国际象棋棋盘中一个格子的颜色

Design and implementation of IDS

Specific method and example program of Siemens s7-200smart control stepping motor

math_ Mathematical expression & deformation of equation equation & accumulation of combined operation skills / means

多线程实现客户端与服务端通信(初级版本)
随机推荐
Bs-gx-017 online examination management system based on SSM
ModbusTCP协议网络学习型单路红外模块(双层板)
(JS)手写深拷贝
Memory allocation - static storage stack heap and static variable
ModbusTCP协议WIFI无线学习型单路红外模块(小壳版)
math_数学表达式&等式方程的变形&组合操作技巧/手段积累
[daily 3 questions (3)] reformat the phone number
(JS) handwriting depth comparison
What are the pop, push, unshift, and shift of the (JS) array?
Encore une fois, le chemin de l'amélioration de redis Cloud
Map merges the same keys and values into a list
Google Earth engine (GEE) - Gedi L2a vector canopy top height (version 2) global ecosystem data set
5. migrate uboot set default environment variables, crop, and partition
When the "Ai x scientific computing" is in progress, Huawei's mindspore competition question is hot, waiting for you!
what? It's amazing that you can read the whole comic book for free. You can't learn to be a money saver together
(JS) status mode
(JS) imitate the indexof method to find the position of a character in the string
Multi thread communication between client and server (primary version)
NUC980开源项目16-从SPI FLASH(W25Q128)启动
Xiaomi mobile phone - Unlock BL + open root permission