The difference between mysql primary key index and ordinary index

 

The difference between primary key index and ordinary index:
https://www.cnblogs.com/heishuichenzhou/p/10813463.html

 

 

 

For example, for the following table (in fact, a k field is added to the above table), and ID is the primary key.

 

The schematic diagram of the primary key index and the non-primary key index is as follows:

 

Where R represents the value of a whole row.

It is not difficult to see from the figure that the difference between a primary key index and a non-primary key index is: the leaf node of the non-primary key index stores the value of the primary key, while the leaf node of the primary key index stores the entire row of data , of which the non-primary key index is also called It is a secondary index , and the primary key index is also called a clustered index .

According to these two structures, let's make a query to see what difference they have in query.

1. If the query statement is select * from table where ID = 100, that is, the primary key query method, you only need to search the B+ tree of ID.

2. If the query statement is select * from table where k = 1, that is, a non-primary key query method, first search the k index tree, get ID=100, and then search the ID index tree again. This process is also called back to the table .

Now, do you know the difference between them?

 

 


The leaf node of the primary key index stores the entire row of records, and the leaf node of the ordinary index stores the primary key ID. When querying, a query back to the table is required. Is it necessary to query
back to the table?

 

 


Not necessarily, when the query field happens to be an indexed field or part of the index, you don’t need to return to the table. This is also the principle of index coverage.
Read:
mysql coverage index details:
https://blog.csdn.net/jh993627471/article/ details/79421363

Guess you like

Origin blog.csdn.net/liuming690452074/article/details/113807023