Here comes your interviewer , Wearing a plaid shirt , With a beer belly , A middle-aged man whose hairline has moved back seriously . Holding a thermos cup soaked with Chinese wolfberry , With arms MacBook,MacBook There are company slogans on the :“ Working overtime makes me happy ”.

edit
Add picture comments , No more than 140 word ( Optional )
interviewer :
Look at your resume MySQL, Let me ask you a few simple questions . What are clustered indexes and nonclustered indexes ?
This problem can't help me .
I :
for instance : There is such a user table
CREATE TABLE `user` (
`id` int COMMENT ' Primary key ID',
`name` varchar(10) COMMENT ' full name ',
`age` int COMMENT ' Age ',
PRIMARY KEY (`id`)
) ENGINE=InnoDB CHARSET=utf8 COMMENT=' User table ';
These data are stored in the user table :
So in the index , How is this data stored ?
MySQL Of InnoDB The index used in the engine B+ Tree structure .

Edit switch to center
Add picture comments , No more than 140 word ( Optional )
Don't ask why the root node stores (1,4) Two elements , The left child node stores (1,2,3) Three elements , There are three leaf nodes below , Leaf nodes are connected by an ordered linked list ?
Asking is B+ Characteristics of trees , If you don't know, you can turn to the article in the previous issue .
As shown in the figure above , The leaf node stores the indexes of all elements , Namely
Cluster index
. Generally, the primary key index is
Cluster index
, If there is no primary key in the table ,MySQL A hidden primary key will also be created by default as the primary key index .
What is a nonclustered index ?
Suppose we were age( Age ) Create a common index on the field ,age The index storage structure above the field is as follows :

Edit switch to center
Add picture comments , No more than 140 word ( Optional )
Only the current index field and primary key are stored in the leaf node ID, Such a storage structure is a non clustered index .
interviewer :
So what is a joint index ?
I :
An index consisting of multiple fields is a union index .
interviewer :
【 dizzy 】 What are the benefits of building a joint index ? What is the difference between it and indexing on a single field ?
I :
Suppose there is such a query statement .
select * from user where age = 18 and name = ' Zhang San ';
If we were age and name Two indexes are built on the field , This query statement only uses one of the indexes .
But we are age and name Field to create a union index (age,name), Its storage structure becomes like this .

Edit switch to center
Add picture comments , No more than 140 word ( Optional )
If only age Build an index on it , We'll check age The above non clustered index , There are three age=18 The record of , Primary key ID Namely 1、4、5, Then use these three ID Go to query the primary key ID Clustering index of .
If in age and name Build a joint index on it , We'll check age and name The non clustered index above , Match to a record , Primary key ID yes 1, Then use this ID Go to query the primary key ID Clustering index of .
From this we can get , Advantages of Federated indexing :
Greatly reduce the number of scanning lines .
interviewer :
What is the leftmost matching principle ?
I :
The leftmost matching principle refers to when establishing a union index , Follow top left priority , Any consecutive index starting from the leftmost can match up .
When we're in (age,name) When building a union index on ,where Only age You can use indexes , At the same time there is age and name You can also use indexes . But only name You can't use the index when .
Why does this happen ?
Look at the picture above , I understand. ,(age,name) Joint index of , It's according to age Sort ,age Equal lines are then followed by name Sort . If where Only one condition name, Of course you can't use an index .
interviewer :
What are overlay indexes and backtable queries ?
I :
This is even easier , This knowledge point has been mentioned above .

Edit switch to center
Add picture comments , No more than 140 word ( Optional )
When we're in age When building an index , Inquire about SQL This is the time :
select id from user where age = 18;
The overlay index will be used , because ID We use age It has been found when indexing , There is no need to query the table twice .
But when querying SQL This is the time :
select * from user where age = 18;
Want to query all fields , You need to query the table twice . Because for the first time we used age When indexing, only the primary key is found ID, You also need to use the primary key ID Return to the table to query all fields .
interviewer :
Ask one more , Do you know what index pushdown is ?
Such an unpopular question , You can ask , Really want to interview to build a rocket !
I :
Index push down (Index Condition Pushdown) yes MySQL5.6 The introduction of a feature that optimizes indexes .
give an example :
stay (age,name) Build a joint index on it , And check SQL This is the time :
select * from user where age = 18 and name = ' Zhang San ';

Edit switch to center
Add picture comments , No more than 140 word ( Optional )
If there is no index push down , Will match first age = 18 Three records of , Reuse ID Return to the table for query , select name = ' Zhang San ' The record of .
If you use index push down , Will match first age = 18 Three records of , And screen out name = ' Zhang San ' A record of , Last but not least ID Return to the table for query .
The resulting , Advantages of index push down : Reduces the number of rows scanned back to the table .
** interviewer : ** Young man , The eight part essay recites very smoothly . Let me give you a practical problem , See if you are ready . Here's the query SQL How to build a union index ?
select a from table where b = 1 and c = 2;
Deliberately make things difficult for me ? Do you think you can't recite the eight part essay for the practical problem ?
I :
I have already mentioned this knowledge point when talking about joint index ,where There are conditions b and c The equivalent query of , The joint index is built (b,c), because select In the back a, We will establish (b,c,a) Joint index of , And you can use the overlay index , Faster queries .
interviewer :
Young man , There's something . I'll send it to you in a minute offer, Come to work tomorrow , Salary double.
原网站版权声明
本文为[InfoQ]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/176/202206251532219538.html