MySQL Performance Optimization: Index Optimization

1. Basic knowledge of indexing

(1) Auxiliary Index/Secondary Index

In addition to the key value of the leaf node, the index row in each leaf node also contains a bookmark (bookmark) (each index is a B+ tree, not including all the data recorded in the row)

(2) Return form

Obtain the primary key through the auxiliary index, and then use the primary key index (clustered index) to find a complete row record.

 (3) Joint index/composite index

Combining multiple columns on the table for indexing is called a joint index or a composite index

(4) Covering index

The query records can be obtained from the auxiliary index without querying the records in the clustered index

2. High performance index creation strategy

1. An index is a B+ tree. The index allows our query to quickly locate and scan the data records we need, speeding up the query.

2. A select query statement can generally use at most one secondary index during execution, even if multiple secondary indexes are used in the where condition.

(1) The type of index column should be as small as possible

The smaller the index type, the faster it will be processed.

(2) Selection of index columns

Index selectivity/discreteness: The higher the ratio (range 1/N to 1) of the unique index value to the total number of records (N) in the data table, the higher the query efficiency.

(3) Prefix index

For blob, text, and very long varchar fields, mysql does not support indexing their full length, and a prefix index needs to be established. Syntax: Alter table tableName add key/index (column(X)) Prefix index selection (X): Disadvantages: It cannot be applied to order by and group by, and it cannot be used as a covering index.

(4) Suffix index

mysql does not support suffix indexes. You can create a prefix index by adding a new column to the table to save the inverted value of the field to be suffix indexed. Scenario: Query the mailbox suffix.

(5) Only create indexes for searched, sorted or grouped columns

Building an index consumes a lot of resources when inserting data, so there is no need to build an index for fields that are not frequently queried.

(6) Multi-column index

Put the most selective columns at the front of the index. Adjust the order of index columns based on the most frequently run queries. When optimizing performance, you need to use the same column but different indexes to meet the needs of different types of queries.

Summarize:

Samsung index is probably the best index for a query. The conditions to be met are as follows: The index puts related records together to get one star If the order of the data in the index is the same as the sorting order in the lookup, get two stars (sorting star) If the columns in the index contain all the columns needed in the query then get three stars (broad index stars)

Guess you like

Origin blog.csdn.net/weixin_55229531/article/details/131158275