MySQL index failure and solution, covering index, full value matching

What is index invalidation and how to avoid it?

insert image description here

Index failure refers to the situation that in the MySQL database, the index does not play the expected role, resulting in a decline in query performance. ¹²³⁴⁵

Indexes can fail for a number of reasons, such as:

  • Does not meet the full value match or best left prefix rule , that is, the query condition does not include the first column of the index or skips some columns in the index. ¹²³
  • Operations, functions, type conversion and other operations are used on the index column , which makes MySQL unable to use the index. ¹²³
  • Conditions such as not equal to, or, like% are used, resulting in MySQL being unable to effectively use indexes. ¹²³⁴
  • The query result set is too large, causing MySQL to think that a full table scan is faster than using an index. ³⁴

There are many ways to avoid index invalidation, for example:

  • Design a reasonable index according to the query conditions, follow the full value matching and the best left prefix rule. ¹²³
  • Avoid using operations, functions, type conversion and other operations on index columns, and try to keep the original value of index columns. ¹²³
  • Optimize query conditions, try to use equal, in, between and other conditions, and avoid using not equal to, or, like % and other conditions. ¹²³⁴
  • Use a covering index, that is, a query that only accesses the index, avoiding select *. ¹²³
  • Analyze the SQL execution plan and use the explain command or third-party tools to check whether there is room for optimization. ²³

What is a covering index

Covering index means that the query data column can be obtained only from the index without reading the data row, in other words, the query column must be covered by the built index. ²³⁴⁵⁶

The advantage of the covering index is that it can avoid the operation of returning to the table, that is, there is no need to search for data rows based on the primary key, thereby reducing the time for system calls and data copying, and improving query performance. ¹³⁵

The method of using the covering index is to design an appropriate index according to the query requirements, try to make the index contain all the required fields, and avoid select *. ¹²³⁴⁵

For example, suppose there is a table user that contains fields such as id, name, age, gender, etc., where id is the primary key, and name and age are non-primary key indexes. If the query statement is:

select name from user where age = 20;

Then this statement can use the covering index, because the name and age are in the non-primary key index, and there is no need to go back to the table to find the data row.

MySQL full value matching

MySQL full-value matching means that when using a composite index, the query condition must include all the columns of the index to maximize the use of the index. ¹

For example, suppose there is a table emp that contains fields such as id, name, age, and deptid, where id is the primary key, and age, deptid, and name are composite indexes. If the query statement is:

select * from emp where age = 20 and deptid = 10 and name = 'Tom';

Then this statement can use full-value matching, because the query condition includes all columns of the composite index. ¹

If the query statement is:

select * from emp where age = 20 and deptid = 10;

Then this statement cannot use full-value matching, because the query condition lacks the last column name of the composite index. ¹

Guess you like

Origin blog.csdn.net/a772304419/article/details/130651087