MySQL index failure or partial failure

MySQL version: 8.0.21

1. The best left prefix rule

  When using a composite index, the order of use of the index should be strictly in accordance with the order when the index was created. The composite index is regarded as a train. If there is an index field in the middle of the composite index that is not used, it is equivalent to a train out of joint in the middle of the train. Disconnected cars, including all cars behind will be out of joint, that is, all the fields behind will not use the index. The most serious is that if the order of the first field index is wrong, the index is completely invalid. Therefore, if this problem occurs, it is best to rebuild the index and change the order of the fields in the composite index.

Full match situation: the
whole train is complete, it runs well
type = ref, key_len = 170
Insert picture description here
The leftmost prefix match: the
front of the train is still there, the body is gone, it's okay to run
type = ref, key_len = 82
Insert picture description here
The leftmost prefix matches, the first One car is still there: the
front is still there, and the first car is still
there, so type = ref, key_len = 87
Insert picture description here

The leftmost prefix matches, and the age composite index field is
missing at the back : the front of the car is still there, the first car is lost, and all the cars behind are lost.
So type = ref, key_len = 82
Insert picture description here
The leftmost prefix does not match. The
front of the car is gone, and the index is running. It doesn’t work,
so the type is all, no index is used
Insert picture description here

2. Make small actions on the index column

  The calculations (including !=), functions, and type conversion (including automatic) operations that occur on the index column will cause the index to fail. The
locomotive has an operation, and the entire train cannot run.
type = all The
Insert picture description here
first car has an operation, and the locomotive works alone, including All the cars behind, including the first car, are scrapped
type = ref, key_len = 82 The
Insert picture description here
second car has an operation, the locomotive took the first car away, and all the cars behind are scrapped
type = ref, key_len = 87
Insert picture description here

3. Scope of the index

  The composite index field after the index field of the range condition is invalid, the previous one has no effect on the
use range of the first car, the locomotive and the first car are still alive, and the second includes the subsequent cars go die
Insert picture description here

4. Covering Index

  Try to use the covering index as much as possible, do not use select* to
use the covering index if it is not necessary , there will be a Using index in
Insert picture description here
Extra to use *, then Extra is empty
Insert picture description here

5. NULL related

  IS NULL IS NOT NULL will invalidate the index, and the effect is the same as the range condition of the index.
The first carriage uses null, the locomotive and the first carriage are still alive, and the second carriage including the later carriages
Insert picture description here
uses null for the head of the go die , and the family is neat and tidy.
Insert picture description here

6.like wildcard related

  1. Using the two methods of %xxx or %xxx% will invalidate the index. If you can, choose to use xxx% with a wildcard on the right.
    The index failure is equivalent to a small action on the index. The
    locomotive wildcard chooses to start
    Insert picture description here
    with% and scraps the entire locomotive. The wildcard does not choose to start with %, type = range, and the name_age_jobs index can be used for those that meet the beginning of e. The index key_len = 170 uses the Using index It's just that the ref constant is not used, and the effect is not bad.
    Insert picture description here
    The second car wildcard chooses to start with% to scrap all the second and later cars. Fortunately, the locomotive and the first car are not affected, so type = ref, ref also uses two constants, compared to the locomotive crashed Very good. The
    Insert picture description here
    wildcard in the second carriage does not start with %. The composite index is all used, type = range, key_len = 170
    Insert picture description here
  2. When you must start with %, you should build a covering index, and you should try to avoid querying fields other than the fields involved in the covering index.
    Use covering index, type = index
    Insert picture description here
    query does not use covering index, type = all
    Insert picture description here

7. String without single quotes

The field is of varchar type but there is no quotation mark in the SQL, which may not affect the query result, but it will definitely affect the query efficiency.
Normal condition
Insert picture description here
Index failure condition
Insert picture description here

Guess you like

Origin blog.csdn.net/TreeCode/article/details/108507635