Reasons and solutions for MySQL index failure

Reasons and solutions for MySQL index failure

insert image description here

Indexes are an important performance optimization tool when working with MySQL databases. However, sometimes we may encounter index invalidation. This article will introduce several common causes of MySQL index failure and corresponding solutions, and provide error and correct examples of SQL statements.

1. The string field does not use a prefix index

  • Reason: The string field does not use the prefix of the index.
  • Solution: Use a prefix for string fields when creating indexes.
  • Error example:SELECT * FROM table WHERE name LIKE '%keyword%';
  • Correct example:SELECT * FROM table WHERE name LIKE 'keyword%';

2. The OR operator uses a multi-column index

  • Reason: The OR operator is used to connect multiple column query conditions, resulting in index failure.
  • Solution: Split the multi-column query conditions into single-column queries and use indexes separately.
  • Error example:SELECT * FROM table WHERE col1 = 'value1' OR col2 = 'value2';
  • Correct example: SELECT * FROM table WHERE col1 = 'value1';orSELECT * FROM table WHERE col2 = 'value2';

3. A function is used on the index column

  • Reason: Using a function on an indexed column prevents the index from being used.
  • Solution: Avoid using functions on indexed columns, or use function expression indexes.
  • Error example:SELECT * FROM table WHERE YEAR(date_column) = 2021;
  • Correct example:SELECT * FROM table WHERE date_column >= '2021-01-01' AND date_column < '2022-01-01';

4. Implicit data type conversion

  • Reason: The data type in the query condition is inconsistent with the data type of the index field, resulting in an implicit data type conversion, thus invalidating the index.
  • Solution: Make sure that the data type of the query condition is consistent with the data type of the index field.
  • Error example: SELECT * FROM table WHERE id = '1';(the id field is an integer, and the query condition is a string)
  • Correct example:SELECT * FROM table WHERE id = 1;

5. Range query using not equal operator

  • Cause: A range query uses an inequality operator (!= or <>), which prevents the use of an index.
  • Solution: Try to avoid using the not equal operator for range queries.
  • Error example:SELECT * FROM table WHERE age != 30;
  • Correct example:SELECT * FROM table WHERE age > 30 OR age < 30;

6. The composite index field order is incorrect

  • Reason: The order of the fields in the query condition is inconsistent with the order of the index fields, resulting in the inability to use the index.
  • Solution: Adjust the order of the fields in the query condition to be consistent with the order of the index fields.
  • Error example: SELECT * FROM table WHERE name = 'John' AND age = 30;(index: (age, name))
  • Correct example:SELECT * FROM table WHERE age = 30 AND name = 'John';

7. The NOT operator is used on an indexed column

  • Reason: The NOT operator is used on the index column, resulting in the inability to use the index.
  • Solution: Avoid using the NOT operator on indexed columns, you can use other ways to achieve the same logic.
  • Error example:SELECT * FROM table WHERE NOT id = 1;
  • Correct example:SELECT * FROM table WHERE id <> 1;

8. IS NULL or IS NOT NULL is used on the index column

  • Cause: The IS NULL or IS NOT NULL operator is used on the index column, which makes it impossible to use the index.
  • Solution: Avoid using IS NULL or IS NOT NULL operators on indexed columns, you can use other ways to achieve the same logic.
  • Error example:SELECT * FROM table WHERE id IS NULL;
  • Correct example: SELECT * FROM table WHERE id = NULL;orSELECT * FROM table WHERE id <=> NULL;

9. Operator used on index column

  • Cause: An operator was used on an indexed column, which prevented the index from being used.
  • Solution: Avoid using operators on indexed columns, you can use other ways to achieve the same logic.
  • Error example:SELECT * FROM table WHERE id + 1 = 2;
  • Correct example:SELECT * FROM table WHERE id = 1;

10. Lots of duplicate values ​​on indexed columns

  • Reason: There are a large number of duplicate values ​​on the index column, causing the index to fail.
  • Solution: Consider using a more granular indexing strategy to reduce duplicate values.
  • Error example: None
  • Correct example: none

11. A large number of NULL values ​​on the index column

  • Reason: There are a large number of NULL values ​​on the index column, causing the index to fail.
  • Solution: Consider using other ways to handle NULL values, or use conditional indexes.
  • Error example: None
  • Correct example: none

Guess you like

Origin blog.csdn.net/weixin_45626288/article/details/131953319