Original please indicate the source: https://www.cnblogs.com/agilestyle/p/11429438.html
InnoDB created by default primary key index is clustered index (Clustered Index), the other part of the auxiliary indexes index (Secondary Index), also known as non-clustered index secondary index.
Next, a simple example, a specific realization both stored in the index data.
First create a merchandise table, as follows:
1 CREATE TABLE `merchandise` ( 2 `id` int(11) NOT NULL, 3 `serial_no` varchar(20) DEFAULT NULL, 4 `name` varchar(255) DEFAULT NULL, 5 `unit_price` decimal(10, 2) DEFAULT NULL, 6 PRIMARY KEY (`id`) USING BTREE 7 ) CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
Then add the following lines to the data, as follows:
InnoDB storage engine is used, because InnoDB uses clustered index, the leaf nodes clustered index is recorded in a primary key value, transaction id, MVCC and refluxed for transaction pointer and all the remaining columns, as shown below It shows:
Based on the above illustration, if the product code need to query serial_no commodity, the commodity code need serial_no column as a column index. At this point the index is a secondary index is created, but it is not stored in the leaf nodes of the row pointer, but the primary key, and use it as a pointer to the row. The advantage is that when the row occurred or mobile data division, no longer need to maintain a change index.
If the primary key index query product will find the corresponding leaf nodes in accordance with the B + tree index, the data directly to the line:
1 select * from merchandise where id=7
If serial_no query product, i.e., a secondary index for inquiry, we will first retrieve serial_no secondary index B + tree, to find the corresponding leaf node, obtaining primary key values, and then retrieve the corresponding through clustered index B + tree leaf node, then get the entire row of data. This process is called back to the table.