Index related (2)

Index correlation


1. Comparison of InnoDB and MyISAM

storage engine InnoDB MyISAM
storage structure InnoDB tablespace data file and its log file; the table data file itself is an index structure organized by B+Tree, and the data field of the leaf node of this tree saves complete data records. The key of this index is the primary key of the data table, so the InnoDB table data file itself is the primary index MyISAM tables create three different files; table definition, data file, index file
index data structure B+ tree B+ tree
The key value KEY of the leaf node The primary key of the table (generally the primary key INT is unsigned and incremented) the value of the corresponding field of the index
DATA field of leaf node Save complete data records The address of the data record, that is, the address of each line of data in the data file
Secondary index The secondary index data field stores the value of the primary key of the corresponding record instead of the address There is no difference in structure from the main index, the main index cannot be repeated, and the auxiliary index can
type of index clustered index nonclustered index
Full-text index, compressed index not support support
Primary and secondary indexes The primary key node of IAnnoDB stores data rows at the same time, and other auxiliary indexes store the value of the primary key index MyISAM stores data sequentially, the index leaf node stores the corresponding data row address, and the auxiliary index is almost the same as the primary key index.
data storage InnoDB key values ​​are stored together, and indexes are loaded into the InnoDB buffer pool together with data MyISAM key value separation, the index is loaded into memory (key_buffer_size), the data cache depends on the operating system
index order Ascending order; Advantages: Shorten the size of index key values; Disadvantages: Does not support reverse index extraction uncertain


Blog reference:
From shallow to deep to explore the principle of mysql index structure, performance analysis and optimization


2. Precautions

1. InnoDB itself forms a B+ tree structure according to the primary key as the KEY, so the table must have a primary key; if there is no primary key, MySQL automatically generates the InnoDB table An implicit field is used as the primary key. The length of this field is 6 bytes and the type is long.

2. InnoDB does not recommend using a field that is too long as the primary key, because the secondary index is indirectly implemented by looking up the primary index during retrieval; then The value of the primary index is stored in the data field of the node of the secondary index. Because the primary index is very long, the index storage space will increase

. 3. The primary key of InnoDB is monotonous, because the overall data file of InnoDB is a B+ tree structure. If there is duplicate data , will continuously adjust the structure of the B+ tree, increasing maintenance costs and time

4. InnoDB secondary index search needs to retrieve the index twice: first, retrieve the secondary index to obtain the primary key, and then use the primary key to retrieve the records from the primary index.

Blog reference:
MySQL index The data structure and algorithm principle behind it

3. Explanation of terms

storage engine clustered index nonclustered index
definition The logical order of the key values ​​in the index determines the physical order of the corresponding rows in the table; the retrieval range data is fast, and the data has been sorted in order without the need for data sorting; The logical order of the indexes in the index is different from the physical storage order of the disk row
leaf node The leaf node of the index is the data node; for example, the complete record of the data is stored in the DATA in the index structure of the B+ tree of InnoDB The leaf node stores the index node, and there is a pointer to the corresponding data block; for example, in the index structure of the B+ tree of MyISAM, the DATA stores the physical address pointing to the record in the data table
quantity There can only be one in each table Depends on the actual use of the index


Blog Reference:
Clustered and Nonclustered Indexes

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326617180&siteId=291194637