Different (storage structure) MySQL MyISAM and InnoDB storage engine to achieve the index

Reference website: http://www.2cto.com/database/201211/172380.html

Myisam and different innodb index achieved

 

MyISAM engine used as a B + Tree index structure, leaf node is stored in the address data field of the data record. The figure is a schematic diagram MyISAM indexes:

 

Here provided a total of three tables, we assume Col1 primary key, the primary index is a figure MyISAM tables (Primary key) schematically. It can be seen MyISAM index file only save the address data records. In MyISAM, the main index and a secondary index (Secondary key) without any difference in structure, but the main index key requirement is unique, and the auxiliary key index may be repeated. If we build a secondary index on Col2, the structure of this index is shown below:

 

 

Is also an address B + Tree, data field holds the data records. Thus, MyISAM index search algorithm first search algorithm in accordance with the B + Tree search index, if the specified Key is present, the value of its data field taken out, then the value of the address data field, reads the corresponding data record.

MyISAM indexes mode is also called "non-clustered", the reason so called to distinguish the InnoDB clustered index.

 
InnoDB index achieved

Although InnoDB also be used as a B + Tree index structure, but the specific implementation manner, but with different MyISAM.

The first major difference is that InnoDB data file itself is an index file. Known from the above, MyISAM index file and data file are separate, only the address of the index file stored in the data record. In the InnoDB table data file itself is a pressing B + Tree index structure of the organization, this tree's leaf node data field holds a complete data record. This index is the primary key of key data tables, so InnoDB table data file itself is the main index.

 

The figure is a schematic diagram InnoDB primary index (also data files), you can see the leaf node contains complete data record. This index is called the clustered index. Because InnoDB data file itself Yaoan primary key aggregation, so InnoDB requires table must have a primary key (MyISAM can not), if not explicitly specified, MySQL system will automatically select a column as the primary key uniquely identifies a data record, if there is no this column, MySQL automatically generates a hidden field as the primary key InnoDB tables, this field is 6 bytes in length, as long integer type.
 

The second difference is that the MyISAM index InnoDB secondary index field stores data records corresponding to the primary key value instead of an address. In other words, all the secondary index InnoDB are cited as the primary key data field. For example, defined below shows a secondary index on the Col3:

 

Here in ASCII English characters as the comparison criteria. This implementation clustered index makes the search highly efficient primary key press, but the secondary index search index needs to be retrieved twice: first retrieve the auxiliary primary key index is obtained, and then use the primary index to the primary key to retrieve records obtained.
 

Learn index different storage engine implementations for proper use and optimize indexes are very helpful, for example, know the InnoDB after the index to achieve, it is easy to see why not recommend the use of long field as the primary key, because all secondary indexes are the main reference index, long the main index will make secondary indexes become too large. As another example, a non-monotonic field as the primary key in InnoDB not a good idea because InnoDB data file itself is a B + Tree, non-monotonic primary key will result in the insertion of a new recording data file in order to maintain + Tree characteristic B and frequent split adjusted, very inefficient, and use auto-increment field as the primary key is a good choice.

Guess you like

Origin blog.csdn.net/u012501054/article/details/90414467