MySQL index knowledge remarks

MySQL index classification

The default index of Innodb and MyISAM is the Btree index; the default index of Mermory is the Hash index.

The MyISAM leaf node stores the address of the table, so the data file and the index file are separate, also known as non-aggregated

Unlike Innodb, the leaf nodes of the main index do not store the address of the table, but data. Auxiliary index leaf nodes store primary key information. called a clustered index.

When using the secondary index, the primary key information is retrieved, and then the primary key is used to locate the data in the table in the primary index, so the primary key should not use too long fields. Since all secondary indexes contain the primary index, it is easy to make the secondary index become huge.

Leftmost matching principle in Btree index:

Btree builds the search tree in left-to-right order. For example, if the index is (name, age, sex), the name field will be checked first, and if the name field is the same, the last two fields will be checked.

Therefore, when the data of the last two fields (age, sex) are passed in, because the search tree is built according to the first field, the name field must be used to know where to query the next field.

So when (name, sex) is passed in, the search direction will be specified according to the name first, but the second field is missing, so after all the correct name fields are found, the sex data will be matched.

Rules for indexing:

1. Use the leftmost prefix: Mysql will always search to the right until it encounters a range operation (>, <, like, between) and stops matching. For example, a=1 and b=2 and c>3 and d=6; if the (a,b,c,d) index is established at this time, then the latter d index is completely useless, when it is replaced with (a ,b,d,c) can be used.

2. Do not over-index: When modifying the table content, the index must be updated or reconstructed, so when there are too many indexes, it will consume more time.

3. Try to expand the index instead of creating a new index

4. The most suitable columns for the index are the columns that appear in the where clause or the columns specified in the join clause.

5. Columns with fewer distinct values ​​do not need to be indexed (gender).

Reference: Hash index and BTree index

Detailed explanation of Btree index

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325377723&siteId=291194637