当前位置:网站首页>Interview shock 56: what is the difference between clustered index and non clustered index?

Interview shock 56: what is the difference between clustered index and non clustered index?

2022-06-13 12:10:00 InfoQ

stay  MySQL  Default engine  InnoDB  in , Indexes can be roughly divided into two categories : Clustered index and non clustered index , Their differences are also common interview questions , So let's dish them today .

Cluster index

Cluster index (Clustered Index) Generally refers to the primary key index ( If there is a primary key index ), A clustered index is also called a clustered index .

Cluster index is in  InnoDB  Is used in  B+  Trees achieve , For example, we create a  student  surface , Its construction  SQL  as follows :

drop table if exists student;
create table student(
 id int primary key, 
 name varchar(16),
 class_id int not null, 
 index (class_id)
)engine=InnoDB;
--  Add test data
insert into student(id,name,class_id) values(1,' Zhang San ',100),
 (2,' Li Si ',200),(3,' Wang Wu ',300);

above  student  There is a clustered index in the table ( That's the primary key index )id, And a non clustered index  class_id.

Cluster index  id  Corresponding  B+  The tree is shown in the figure below :

null
The memory address of the user information is directly stored in the leaf node of the cluster index , We can directly find the corresponding row data by using the memory address .

Nonclustered index

Non clustered index in  InnoDB  In the engine , Also called secondary index , In the above  student  Table as an example , stay  student  Central African cluster index  class_id  Corresponding  B+  The tree is shown in the figure below :

null
As can be seen from the figure above ,
What is stored on the leaf node of the non clustered index is not real row data , It's the primary key  ID, So when we use non clustered indexes for queries , First, you will get a primary key  ID, Then use the primary key  ID  Find the real row data on the clustered index , We call this process back to table query .

summary

stay  MySQL  Of  InnoDB  In the engine , Each index will correspond to one  B+  Trees , The biggest difference between clustered index and non clustered index lies in the difference of data stored in leaf nodes , The clustered index leaf node stores row data , Therefore, real row data can be found directly through cluster index ; The non clustered index leaf node stores the primary key information , Therefore, the use of non clustered indexes also requires back to table queries , Therefore, we can conclude that the main differences between clustered indexes and non clustered indexes are as follows :

  • The clustered index leaf node stores row data ; The non clustered index leaf node stores the clustered index ( It's usually the primary key  ID).
  • Cluster index query is more efficient , Non clustered indexes need to be queried back to the table , Therefore, the performance is not as good as that of clustered indexes .
  • Cluster indexes are generally primary key indexes , A table can only have one primary key , Therefore, a clustered index can only have one table , Non clustered indexes are not limited in number .

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
原网站

版权声明
本文为[InfoQ]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/164/202206131205163241.html