Introduction to MySQL Indexes

1. Index type

1.1 B-Tree Index

The data is stored using a B-Tree data structure, so the index values ​​are stored sequentially, and each leaf node is the same distance from the root node. The height of the B-Tree is log(n) level, so the search complexity is log(n) level.

Scenarios that can be used:

(1) Full value matching

(2) Match the leftmost prefix: the leftmost index columns of the joint index

(3) Matching column prefix: the beginning of the value of an index column

(4) Matching range value

(5) Exactly match a column and range match another column

Restricted usage scenarios:

(1) Do not start the search according to the leftmost column of the joint index

(2) The columns in the index cannot be skipped: For example, the joint index has 3 columns. If you search for columns 1 and 3, you can only use the index of column 1.

(3) If there is a range query of a certain column in the query, all columns on the right side cannot use the index

2. High performance index

1.1 Index columns should be independent

An index column cannot be part of an expression, or an argument to a function

1.2 Multi-column index

When multiple columns are often used for intersecting operations, a joint index can be built for these columns instead of indexing each column individually

1.3 Order of Index Columns

Choose an appropriate index column order, placing the most selective column at the top of the index

1.4 Clustered Index

Data rows are stored on the leaf nodes. Generally, a clustered index is built on the primary key column, and only one clustered index can be built for a table. Other indexes that are not clustered indexes store index columns and primary key values ​​on leaf nodes. Therefore, when using an index query that is not a clustered index, the primary key value will be found first, and then the primary key value can be used to obtain the entire row of data using the clustered index.

The leaf nodes of a nonclustered index store the values ​​of the index columns and rows that point to the values ​​of the associated index columns.

1.5 Covering indexes

The leaf nodes of the index already contain the fields to be queried. When the index is overwritten, the extra column in explain shows using index

1.6 Sorting using index scans

Because the data obtained through the index is originally sorted according to the index column. Therefore, when order by is followed by an index column, the sorting step will be omitted and the speed will be accelerated. Otherwise, Using filesort appears in the extra column of explain

limitation factor:

(1) The order of the index columns is exactly the same as that of the order by, and the sorting direction of the columns after the order by is the same

(2) When multiple tables are associated, the order by reference fields are all the first table

(3) Satisfy the leftmost prefix of the index

 

Guess you like

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