【2023】MySQL in detail - SQL optimization

index

Index is a data structure

  • Advantages and disadvantages of indexing:

Index structure:

My sql index mainly refers to B+TREE index

BTREE tree structure:

For example, a 5-cross number : a cross represents a pointer, and each node can have a maximum of 4 elements (the number of node elements = 1 less than the number of M crosses)

B+TREE tree

 

B+Tree in MySQL

The MySQL index data structure optimizes the classic B+Tree. On the basis of the original B+Tree, a linked list pointer pointing to adjacent elements is added, forming a B+Tree with a sequential pointer, providing interval performance

index classification

  • single value index
  • composite index
  • unique index

Index design principles:

  • For fields with higher query frequency and tables with larger amounts of data,
  • When selecting index fields, the best candidate columns should be extracted from the conditions in the where clause. If there are many combinations in the where clause, the combination of the most commonly used columns with the best filtering effect should be selected.
  • Using a unique index, the higher the distinction, the more efficient the use of the index.
  • Indexes will reduce the efficiency of additions, deletions and modifications
  • use short index
  • Using the leftmost prefix, if you do not follow the leftmost prefix rule when using a composite index, the index will be invalid.

Leftmost prefix:

Indicates that if multiple columns are indexed, the query needs to start from the leftmost column and do not skip intermediate indexes;

If starting from the leftmost column is not used as a condition, the index will fail;

If there is a jump in the middle, the subsequent fields will not use the index.

  1. Just include the leftmost column, it doesn't matter the order.

  1. When performing a range query, the column on the right does not use an index (when using >< but not using equals)

  1. No operation can be performed on the index, otherwise the index will be invalid ()

  1. Type conversion, such as: string type, when doing conditional query, if we do not add ' ', it will automatically convert it to int for us.

  2. Do not use *, try to use covering index (the queried field is already included in the index)

  • Because if the queried fields have been queried through the index, when performing the query back to the table, it is found that the fields are already available, and the query will not be returned to the table. If it is *, it is found that there are still some fields that are not indexed and have not been queried out. Then we will go back and query the table.

  1. Conditions separated by or , if the column in the condition before or has an index , but the subsequent column does not have an index , then the indexes involved will not be referenced.

  1. Use like for fuzzy matching. Adding % at the end will lead to indexing, but adding % in front will not lead to indexing.

  1. When mysql finds that not using the index is faster than indexing, mysql will not use the index. For example, when most of the fields queried are the values ​​you include. -When querying Beijing city, the index will not be used

  1. is null, is not null; sometimes it will fail; when most of the queried fields have data, using is null will go to the index, but using is not null will not go to the index, because most of the fields are non-null. mysql will feel that full table scan is faster,

  2. in uses the index, not does not use the index, the principle is the same as above;

  1. When querying the date

The function needs to be changed to a range, otherwise the index cannot be used

Guess you like

Origin blog.csdn.net/weixin_52315708/article/details/131482679