当前位置:网站首页>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

 When will the index fail ?

 The interviewer's test site is simple. Talk about the index failure scenario you encounter in your work ?

     
  • 1.
  • 2.
  • 3.

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

 The index of nine interview sites of ten companies is invalid ?_ Index failure

 The index of nine interview sites of ten companies is invalid ?_ Interview questions _02

Verification preparation

Prepare data sheets , At the same time, build a common index idx_user_name

CREATE TABLE `t_user` (
  `id` int(11) NOT NULL,
  `user_name` varchar(32) CHARACTER DEFAULT NULL COMMENT ' user name ',
  `address` varchar(255) CHARACTER DEFAULT NULL COMMENT ' Address ',
  `create_time` datetime DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP COMMENT ' Creation time ',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

     
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.

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 )

--  Create stored procedure , Insert 10000 User information 
CREATE PROCEDURE user_insert()
--  Define stored procedure start 
BEGIN
	--  Defining variables  i ,int  type , The default value is  1
	DECLARE i INT DEFAULT 1;
	
	WHILE i <= 10000
		--  Define the execution of commands within the loop 
		DO INSERT INTO t_user(id, user_name, address, create_time) VALUES(i, CONCAT('mayun', i), CONCAT(' Hangzhou, Zhejiang ', i), now());
		SET i=i+1;
	END WHILE;
	
	COMMIT;
END;
--  Define the end of the stored procedure 


--  Call the storage project 
CALL user_insert();

     
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.

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 ?

 The index of nine interview sites of ten companies is invalid ?_ Interview questions _03

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

explain select * from t_user where user_name = 'mayun10' or user_name = 'mayun1000'

     
  • 1.

 The index of nine interview sites of ten companies is invalid ?_MySQL_04

OR Two different fields are connected , Different indexes are invalid

explain select * from t_user where user_name = 'mayun10' or address = ' Hangzhou, Zhejiang 12'

     
  • 1.

 The index of nine interview sites of ten companies is invalid ?_ database _05

to address Column increase index

alter table t_user add index idx_address (address)

     
  • 1.

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

 The index of nine interview sites of ten companies is invalid ?_ Interview questions _06

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

select * from t_user where user_name like '%mayun100%';

     
  • 1.

Is this query indexed ?

 The index of nine interview sites of ten companies is invalid ?_ Interview questions _07

select * from t_user where user_name like 'mayun100%';

     
  • 1.

Is this query indexed ?

 The index of nine interview sites of ten companies is invalid ?_ Index failure _08

Validation summary

like  The wildcard feature allows you to open and close matching queries on the left and right 

 When the left side is open  %  perhaps  _  No index will be used when matching , A full scan will be performed 

     
  • 1.
  • 2.
  • 3.

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 ,

alter table t_user add index idx_age(age);

     
  • 1.

Don't use built-in functions

explain select * from t_user where age = 80

     
  • 1.

 The index of nine interview sites of ten companies is invalid ?_ Index failure _09

Use built-in functions

explain select * from t_user where abs(age) = 80

     
  • 1.

 The index of nine interview sites of ten companies is invalid ?_ Interview questions _10

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

alter table t_user add index idx_age(age);
explain select * from t_user where age = 80;

     
  • 1.
  • 2.

 The index of nine interview sites of ten companies is invalid ?_ Index failure _09

Index column operation

explain select * from t_user where age + 5 = 80

     
  • 1.

 The index of nine interview sites of ten companies is invalid ?_ Interview questions _12

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

alter table t_user add index idx_user_name(user_name);

explain select * from t_user where user_name = 'mayun1';

     
  • 1.
  • 2.
  • 3.

 The index of nine interview sites of ten companies is invalid ?_ Interview questions _13

Modifying data , Again explain

update t_user set user_name = '100' where user_name = 'mayun1';
explain select * from t_user where user_name = 100;

     
  • 1.
  • 2.

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’

 The index of nine interview sites of ten companies is invalid ?_MySQL_14

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 .

alter table t_user add index idx_age(age);
explain select * from t_user where age in (100, 50);

     
  • 1.
  • 2.

 The index of nine interview sites of ten companies is invalid ?_ I'm sorry _15

explain select * from t_user where age not in (100, 50);

     
  • 1.

 The index of nine interview sites of ten companies is invalid ?_MySQL_16

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 index of nine interview sites of ten companies is invalid ?_ I'm sorry _17

explain select * from t_user where user_name is null;

     
  • 1.

 The index of nine interview sites of ten companies is invalid ?_ Index failure _18

explain select * from t_user where user_name is not null;

     
  • 1.

 The index of nine interview sites of ten companies is invalid ?_ Index failure _19

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

 The index of nine interview sites of ten companies is invalid ?_ Interview questions _20

explain select * from t_user where user_name is null;

     
  • 1.

 The index of nine interview sites of ten companies is invalid ?_ Interview questions _21

explain select * from t_user where user_name is not null;

     
  • 1.

 The index of nine interview sites of ten companies is invalid ?_ database _22

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 sorry Discuss

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

alter table t_user add index idx_user_name_deposit(user_name, deposit);

explain select * from t_user where user_name like 'mayun86%'

     
  • 1.
  • 2.
  • 3.

 The index of nine interview sites of ten companies is invalid ?_ Index failure _23

Follow the leftmost matching a b type

explain select * from t_user where user_name like 'mayun86%' and deposit = 5620.26;

     
  • 1.

 The index of nine interview sites of ten companies is invalid ?_ Interview questions _24

Change index position , Test joint index writing rules

explain select * from t_user where deposit = 5620.26 and user_name like 'mayun86%';

     
  • 1.

 The index of nine interview sites of ten companies is invalid ?_ database _25

Violation of the leftmost matching principle

explain select * from t_user where deposit = 5620.26;

     
  • 1.

 The index of nine interview sites of ten companies is invalid ?_ I'm sorry _26

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 .

 Suppose we build an index  idx_a_b_c, It's kind of set up  (a), (a,b), (a,b,c) Three indexes 

 When querying for matching, the matching order is  a b c 

 If there is no  a  Field filtering , Then the index will be invalid 

     
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.

Lifting chestnuts , Take the index

select * from test where a=1 
select * from test where a=1 and b=2 
select * from test where a=1 and b=2 and c=3

     
  • 1.
  • 2.
  • 3.

Index failure ?

select * from test where b=2 and c=3

     
  • 1.

Joint index if you want to index , The query condition must be Contains the first index , Otherwise, the index will fail

select * from test where b=1 and a=1

select * from test where m='222' and a=1

     
  • 1.
  • 2.
  • 3.

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

explain select * from t_user where age > 59;

     
  • 1.

 The index of nine interview sites of ten companies is invalid ?_ Interview questions _27

explain select * from t_user where age > 99;

     
  • 1.

 The index of nine interview sites of ten companies is invalid ?_ database _28

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)

 The index of nine interview sites of ten companies is invalid ?_ Interview questions _29

原网站

版权声明
本文为[What a fat thing]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202221500013478.html