当前位置:网站首页>Interview shock 60: what will cause MySQL index invalidation?
Interview shock 60: what will cause MySQL index invalidation?
2022-06-30 17:16:00 【JAVA Chinese community】
author | Lei brother
source | Java Analysis of the real interview questions (ID:aimianshi666)
Please contact authorization for reprint ( WeChat ID:GG_Stone)
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 :https://gitee.com/mydb/interview
Previous recommendation
Java Weekly summary of interview questions ( common 59 piece )| The latest version
Interview shock 59: Can there be multiple auto increment columns in a table ?
Interview shock 58:truncate、delete and drop Of 6 Big difference !
Interview shock 57: Cluster index = Primary key index ?
边栏推荐
- "Promotion + funny plot", how to collide with the sparks of popular funds?
- 以往我们认识的产业互联网,只是以消费互联网的替代者的身份出现
- STL tutorial 7-set, pair pair pair group and functor
- 列表变成向量 列表变向量 list vector
- [demo] write file circularly
- [C language] explain threads in detail - solve the problem of shared memory caused by concurrent programs by "locking"
- redis数据结构分析
- Differential analysis between different groups nichenet for silicosis runs successfully!
- Eight basic sorting (detailed explanation)
- 【C语言】详解线程 — 开启两个线程
猜你喜欢
svg实现的订票UI效果
Nouvelle version de shangdingyun | la fonction favorite est en ligne pour répondre aux besoins d'utilisation personnelle
3D chart effectively improves the level of large data screen
数据库系统概论习题册
HMS core audio editing service 3D audio technology helps create an immersive auditory feast
[wechat applet] basic use of common components (view/scroll-view/wiper, text/rich-text, button/image)
3D图表有效提升数据大屏档次
知名互联网房屋租赁服务公司物联网关键业务迁移上云实践
[200 opencv routines] 215 Drawing approximate ellipse based on polyline
Data mining knowledge points sorting (final review version)
随机推荐
Sub chain cross technology source level exploration: an overview of xcvm
k线图快速入门必读
Compile u-boot source code for stm32p157 development board
开发那些事儿:如何在视频中添加文字水印?
Required plug-ins for idea
Cesium-1.72 learning (eagle eye map of the earth)
[demo] write file circularly
平面相交与平面方程
The new version of Shangding cloud | favorites function has been launched to meet personal use needs
leetcode:1042. 不邻接植花【随机填入符合要求的 + 后面不会形成矛盾 + set.pop】
[C language] explain threads - thread separation function pthread_ detach
Dart: string replace related methods
IndexSearch
MOOG servo valve d661-4577c
leetcode:787. K 站中转内最便宜的航班【k步最短路 + dfs记忆化 + defaultdict(dict)】
Property or method “approval1“ is not defined on the instance but referenced during render
svg实现的订票UI效果
[C language] explain threads - start two threads
期未课程设计:基于SSM的产品销售管理系统
高等数学(第七版)同济大学 总习题一 个人解答