当前位置:网站首页>Interview shock 60: what will cause MySQL index invalidation?
Interview shock 60: what will cause MySQL index invalidation?
2022-06-27 12:16:00 【JAVA Chinese community】
Keep creating , Accelerate growth ! This is my participation 「 Nuggets day new plan · 6 Yuegengwen challenge 」 Of the 12 God , Click to see the event details
In order to verify MySQL Under what circumstances will the index fail , We can use explain Execute the plan to analyze the specific scenario of index failure .
explain Use as follows , Just look up the SQL Add... Before explain Key words can be used , As shown in the figure below : In the columns of the above query results , Our main observation key This column ,key This column represents the index actually used , If NULL The index is not used , Otherwise, the index is used .
All the results listed above are explained as follows :
- id — Select the identifier ,id The higher the priority, the higher , The first to be executed ;
- select_type — Represents the type of query ;
- table — Output result set table ;
- partitions — Matching partition ;
- type — Indicates the connection type of the table ;
- possible_keys — When representing a query , Possible indexes ;
- key — Indicates the index actually used ;
- key_len — Length of index field ;
- ref— Comparison of columns and indexes ;
- rows — Approximate estimated number of lines ;
- filtered — Percentage of rows filtered by table criteria ;
- Extra — Description and description of implementation .
One of the most important is type Field ,type The value types are as follows :
all — Scan the full table data ;
index — Traverse index ;
range — Index range lookup ;
index_subquery — Use... In subqueries ref;
unique_subquery — Use... In subqueries eq_ref;
ref_or_null — Yes null To optimize the index ref;
fulltext — Use full text indexing ;
ref — Find data using a non unique index ;
eq_ref — stay join Use primary key or unique index association in query ;
const — Put a primary key in where The following is used as a conditional query , MySQL The optimizer can turn this query optimization into a constant , How and when to transform , It depends on the optimizer , This ratio eq_ref Be more efficient .
Create test tables and data
In order to demonstrate and test that situation will lead to index invalidation , Let's create a test table and corresponding data :
-- Create table drop table if exists student; create table student( id int primary key auto_increment comment ' Primary key ', sn varchar(32) comment ' Student number ', name varchar(250) comment ' full name ', age int comment ' Age ', sex bit comment ' Gender ', address varchar(250) comment ' Home address ', key idx_address (address), key idx_sn_name_age (sn,name,age) )ENGINE=InnoDB DEFAULT CHARSET=utf8; -- Add test data insert into student(id,sn,name,age,sex,address) values(1,'cn001',' Zhang San ',18,1,' Gao Laozhuang '), (2,'cn002',' Li Si ',20,0,' Huaguoshan '), (3,'cn003',' Wang Wu ',50,1,' Waterfall cave ');There are... In total in the current table 3 An index , As shown in the figure below :
PS: The following content of this article is based on MySQL 5.7 InnoDB Data engine .
Index failure 1: Non leftmost match
The leftmost matching principle refers to , Queries that start with the leftmost field can use a union index , Otherwise, you will not be able to use the union index . The field order of our union index is sn + name + age, We assume that their order is A + B + C, The following union indexes are used as follows : As can be seen from the above results , If the matching fields start from the leftmost, the upper union index can be used , such as :
A+B+C
A+B
A+C
among :A Equals field sn,B Equals field name,C Equals field age.
and B+C But you can't use the union index , This is the left most matching principle .
Index failure 2: Error fuzzy query
Fuzzy query like The common usage of 3 Kind of :
- Fuzzy match followed by any character :like ' Zhang %'
- Fuzzy match any preceding character :like '% Zhang '
- Fuzzy matching before and after any character :like '% Zhang %'
And this 3 There are only two kinds of fuzzy queries 1 Three query methods can be used to index , The specific implementation results are as follows :
Index failure 3: Column operation
If the index column uses an operation , Then the index will fail , As shown in the figure below :
Index failure 4: Using functions
If any... Is used in the query column MySQL The provided function will invalidate the index , For example, the following columns use ifnull The execution plan after the function is as follows :
Index failure 5: Type conversion
If the index column has a type conversion , Then I won't go to the index , such as address Is a string type , When querying, you set int A value of type will invalidate the index , As shown in the figure below :
Index failure 6: Use is not null
When used in a query is not null It can also cause indexes to fail , and is null The index will be triggered normally , As shown in the figure below :
summary
Lead to MySQL Common scenarios for index invalidation are as follows 6 Kind of :
- The union index does not satisfy the leftmost matching principle .
- The first fuzzy query is an indefinite matching character .
- The index column is involved in the operation .
- The index column uses the function .
- Index column has type conversion .
- Index columns use is not null Inquire about .
It's up to you to judge right and wrong , Disdain is to listen to people , Gain or loss is more important than number .
official account :Java Analysis of the real interview questions
Interview collection :gitee.com/mydb/interv…
边栏推荐
- 剑指 Offer 04. 二维数组中的查找
- Wechat applet payment password input
- The DBSCAN function of FPC package in R language performs density clustering analysis on data, and the plot function visualizes the clustering graph
- Dynamic programming [III] (interval DP) stone merging
- TiDB 6.0:让 TSO 更高效丨TiDB Book Rush
- Private dry goods sharing: how to implement platform in Enterprise Architecture
- 巅峰小店APP仿站开发玩法模式讲解源码分享
- Deep understanding of happens before principle
- Topic38——56. 合并区间
- How to adjust an integer that is entered in Excel but always displays decimals?
猜你喜欢

Interviewer: with the for loop, why do you need foreach?

JSP custom tag

"24 of the 29 students in the class successfully went to graduate school" rushed to the hot search! Where are the remaining five?

dried food! What problems will the intelligent management of retail industry encounter? It is enough to understand this article

c/s 架构

怎么找相同台词的影视片段?这8个电影搜索神器,一句台词找到对应片段

Drive to APasS!使用明道云管理F1赛事

uniapp下拉弹层选择框效果demo(整理)

Research Report on the overall scale, major producers, major regions, products and application segments of swine vaccine in the global market in 2022

Heap heap sort TOPK
随机推荐
Unity Shader学习(二)第一个Shader
Unity shader learning (I) understanding the basic structure of unity shader
Interview shock 60: what will cause MySQL index invalidation?
微服务拆分
动态规划【三】(区间dp)石子合并
器审科普:创新医疗器械系列科普——胸骨板产品
亚马逊测评掉评、留不上评是怎么回事呢?要如何应对?
秒云荣获《2022爱分析 · IT运维厂商全景报告》智能运维AIOps市场代表厂商
R语言使用epiDisplay包的followup.plot函数可视化多个ID(病例)监测指标的纵向随访图、使用stress.labels参数在可视化图像中为强调线添加标签信息
In 2021, the global enhanced oil production surfactant revenue was about USD 202.3 million, and it is expected to reach USD 297.1 million in 2028
Research Report on the overall scale, major manufacturers, major regions, products and application segments of hydraulic torque in the global market in 2022
Jianmu continuous integration platform v2.5.0 release
R语言使用glm函数构建泊松对数线性回归模型处理三维列联表数据构建饱和模型、使用step函数基于AIC指标实现逐步回归筛选最佳模型、解读分析模型
Research Report on the overall scale, major producers, major regions, products and application segments of swine vaccine in the global market in 2022
怎么找相同台词的影视片段?这8个电影搜索神器,一句台词找到对应片段
在 Golang 中构建 CRUD 应用程序
pull request
$15.8 billion! 2021 the world's top15 most profitable hedge fund giant
树莓派 3b+ 学习
Don't miss it. New media operates 15 treasure official account to share