当前位置:网站首页>Is the index of nine interview sites of ten companies invalid?
Is the index of nine interview sites of ten companies invalid?
2022-06-21 07:38:00 【What a fat thing】
The index of nine questions of ten companies is invalid ?
This issue's main interview test site
The following common scenarios of index failure
1、like wildcard , With the left side open , Full table scan
2、or Condition screening , May cause index to fail
3、where Use... For index columns in mysql Built in functions for , It must fail
4、where Operation on index columns in ( Such as ,+、-、*、/), It must fail
5、 Different types , Implicit type conversion , Index invalidation caused by
6、where The index column in the statement uses a negative query , May cause index to fail . Negative queries include :NOT、!=、<>、!<、!>、NOT IN、NOT LIKE etc. , among :!< !> SQLServer grammar .
7、 The index field can be null, Use is null or is not null when , May cause index to fail
8、 Index invalidation caused by implicit character encoding conversion
9、 In the union index ,where The index column in violates the leftmost matching principle , It must cause the index to fail
10、MySQL The final choice of the optimizer , Don't walk index
I'm brother fat , An unprofessional interviewer !
I'm Jiong , A rookie looking for a job !
I'm sorry to say : Xiaobai's biggest fear in an interview is that the interviewer's knowledge is too general , I can't locate the key problem point quickly !!!
I'm sorry - Nonsense


Verification preparation
Prepare data sheets , At the same time, build a common index idx_user_name
Insert 1 Ten thousand data ( Be careful : More data ,mysql One of the things that doesn't index is The amount of data is very small ,MySQL Query optimizer Think full table scan is faster than using index , Cause index to fail ,explain When checking whether an index is used , I found that I couldn't index )
One 、OR Index invalidation verification
A lot of people say where Use in condition or , Then the index must be invalid , Whether it is right ?

OR It's connected to the same field , The same index

OR Two different fields are connected , Different indexes are invalid

to address Column increase index
OR Two different fields are connected , If both fields have indexes , Go to the index

Validation summary
or May cause index to fail , Not necessarily , This involves MySQL index merge technology .
1、MySQL5.0 Before , When querying, a table can only use one index at a time , You can't use multiple indexes at the same time to do conditional scans separately .
2、 But from 5.1 Start ,MySQL Introduced index merge Optimization techniques , Multiple indexes can be used for separate conditional scans of the same table . Then merge their respective results (intersect/union).
or What are the conditions for the index to take effect ?
The first one is or The two sides are connected by The same index field
The second kind or Two index fields are connected on both sides , namely Both fields are indexed
Two 、LIKE Wildcard index invalidation validation
One of the most common query scenarios , establish idx_user_name Indexes
Is this query indexed ?

Is this query indexed ?

Validation summary
Why does the index fail when the left is open ? Please introduce the principle !
We know that after indexing ,MySQL Will build an orderly B+Tree, The index tree is ordered , Index columns are matched from left to right . Use % and _ matching , This means that the matching value on the left is uncertain . Not sure , It means full of possibilities , How to compare ?
Of course, we can only compare one by one , That's equivalent to , It's all a match , Full matching in the eyes of the optimizer , Instead of searching through the index tree , Then continue to return to the table operation , It's better to scan the whole table directly !
3、 ... and 、where Use... For index columns in mysql Built in functions for
establish idx_age Indexes ,
Don't use built-in functions

Use built-in functions

Validation summary
If you do a function operation on the index field , May break the order of index values , So the optimizer decided to give up the tree search function .
MySQL You can no longer use the index quick location function , You can only use full index scanning .
Four 、where Operation on index columns in ( Such as ,+、-、*、/), It must fail
The operation of index column is not involved

Index column operation

5、 ... and 、 Different types , Implicit type conversion , Index invalidation caused by

Modifying data , Again explain
user_name = 100 , because user_name Fields define varchar, Index in where When matching, it will be called implicitly first case() Function for type conversion Change the matching condition to ,user_name = ‘100’

6、 ... and 、where The index column in the statement uses a negative query , May cause index to fail .
Negative queries include :NOT、!=、<>、!<、!>、NOT IN、NOT LIKE etc. , among :!< !> SQLServer grammar .


7、 ... and 、 The index field can be null, Use is null or is not null when , May cause index to fail
Case one , The table structure allows user_name The field can be null



The second case , The table structure stipulates user_name Field cannot be null



8、 ... and 、 Index invalidation caused by implicit character encoding conversion
When two tables are joined JOIN when , If the character codes of the two tables are different , May cause index to fail .
This index failure scenario has not yet been encountered , Many articles on the Internet say that it will lead to index failure , Look up a lot of blogs that say UTF8mb4 A table of character sets mb4 And UTF8 A table of character sets utf8 Association can cause index invalidation , But I do it according to a lot of blogs , I found that it can't be reproduced for the time being , Readers may consult by themselves .
If the reader repeats this scene , You are welcome to comment, discuss or pay attention to this scene , Welcome to comment or pay attention to the official account
I'm sorryDiscuss
Nine 、 In the union index ,where The index column in violates the leftmost matching principle , It must cause the index to fail
Create a joint citation idx_user_name_deposit, Follow the leftmost matching principle

Follow the leftmost matching a b type

Change index position , Test joint index writing rules

Violation of the leftmost matching principle

Validation summary
The union index builds the index tree according to the leftmost matching principle , During the query, index values are matched according to the order of joint index , If the leftmost matching principle is violated when querying , Will result in index invalidation .
Lifting chestnuts , Take the index
Index failure ?
Joint index if you want to index , The query condition must be Contains the first index , Otherwise, the index will fail
Why are these two queries indexed ?
The leftmost prefix refers to the order in which the matching index columns are created when querying , But you don't need to write strictly in the order in which the union index is created ,MySQL The optimizer will automatically adjust , So the above two query indexes are valid !
Ten 、MySQL The final choice of the optimizer , Don't walk index


Validation summary
MySQL There are many cases of query index failure , Even in other cases , But after the optimizer determines the query scheme , Index failure is still possible .
The optimizer will consider Query cost , To confirm what it thinks is the best way to execute the query
When the amount of data is small , Or when you need to visit a lot of lines
The optimizer will think Use the index tree to go back to the table , It's better to scan the whole table directly , The optimizer will abandon Walking Tree .
recommend MySQL Related leisure reading :
The first paragraph , Index interview questions recommend reading one : 【 From the interviewer side MySQL Indexed continuous soul torture 】
The second paragraph , Index interview questions recommended reading 2 : 【 From both sides of the interviewer MySQL Indexed continuous soul torture 】
The third paragraph , Index failure scenario interview questions are recommended to read : 【 interviewer : Tell me about the MySQL Index failure scenario , How did you solve ?】
The fourth paragraph , Query cache interview questions, recommended reading : 【 interviewer : What scenario will lead to MySQL Cache invalidation ? Whether the production environment should be started or not MySQL cache ?】
The fifth paragraph , To be updated ? Casual reading is recommended : 【 I'm sorry 】
More highlights , Welcome to WeChat official account. : I'm sorry ( Or search for :jiongmefeishi)

边栏推荐
- RPA(影刀)无需写代码抓取某东的商品信息
- Realize the industry vision of decentralization, safety, accuracy and real-time price feeding
- A table to easily understand the prefix and suffix of increment and decrement operators
- stm32中定义和声明问题
- Best practice | how to use Tencent cloud micro build to develop enterprise portal applications from 0 to 1
- RPA (shadow knife) does not need to write code to capture the product information of an East
- SaaS multi lease mall system in logistics industry improves logistics management efficiency and realizes efficient collaboration
- Hisilicon series mass production hardware commissioning record
- 【osg】osg开发(02)—基于MinGW编译构建osgQt库
- 企业级开发使用POI踩坑盘点
猜你喜欢

Rdkit | compound library based on murcko skeleton clustering

Sword finger offer (2nd Edition) brush questions | 04 Find in 2D array

RPA(影刀)无需写代码抓取某东的商品信息

How to see who developed the applet (see the method of the applet development company)

【osg】osg开发(02)—基于MinGW编译构建osgQt库

Rdkit | molecular similarity based on molecular fingerprint

mysql分页查询如何优化

如何让mysql不区分大小写

mysql存储过程中的循环语句怎么写

mysql如何关闭事务
随机推荐
[Blue Bridge Cup monolithic unit] serial port communication
Type de contrôle qml: Drawer
ANSA二次开发 - 外部程序采用socket与ANSA实现通信
Fault analysis | case analysis of master-slave synchronization error reporting after MySQL slave restart
Integrating eslint in old projects [02]
Record context information through ThreadLocal (record user information to realize global operation)
app安全渗透测试详细方法流程
根因解析 | Kubernetes Pod状态异常九大场景盘点
QML控件类型:Drawer
【osg】osg开发(02)—基于MinGW编译构建osgQt库
18 statistics and its sampling distribution chi square distribution-t distribution-f distribution
Hisilicon series mass production hardware commissioning record
模拟手机设备长按事件
Open up the connection between production and marketing and build a new digital supply chain of agricultural and sideline products
CUDA or FPGA for special purpose 3D graphics computations? [closed]
Horizontal slot, one line of code can directly convert the web page to PDF and save it (pdfkit)
Detailed explanation of deep learning technology for building an image search engine that can find similar images
如何安装Visual Studio Code编辑器?
What are the differences between SQL and MySQL
Stripping the cocoon -- making the digital control of diversified groups simple