Index failure and solutions

Scenarios that may cause index failure

1. Index columns are not independent. Independent means: the column cannot be part of an expression, nor can it be a parameter of a function

For example:
there is calculation on the left side of where condition

explain
select * from employees where emp_no + 1 = 10003;

The explain at this time is all

solution:

Change the condition to the right of the where condition:

explain
select * from exployee where emp_no = 10003 -1;

The explain at this time is const

2. Left blur is used

E.g:

explain * from employees where first_name like '%Geo%'

Solution:
Try to avoid using the left connection, if you can't avoid it, you can consider using a search engine to solve it;

explain * from employees where first_name like 'Geo%'

3. Some of the fields that are queried using or are not indexed

For example: first_name has an index, last_name has no index, then the following statement cannot use the index

explain
select * from employees where first_name = 'Georgi'
							or last_name = 'Georgi';

solution:

Add an additional or conditional index. At this time, the database will merge the two indexes by default, avoiding a full table scan.

4. The string condition is caused by the use of ''

Example: String conditions are not caused by '' (dept_no is a string)

explain 
select * from dept_emp
where dept_no = 3;

The result type is all

Solution: add '', standard writing sql

5. Queries that do not meet the left-most prefix principle

solution:

Adjust the order of the index to become index(first_name, last_name)

6. It is recommended to add NOT NULL constraint for index fields

Single-column indexes cannot store null values, and composite indexes cannot store all null values. When
querying, when the is null condition is used, the index cannot be used, and only the full table scan
mysql. The official recommendation is to define the field as NOT NULL as much as possible

solution:

Set the index field to NOT NULL, you can even set all fields to NOT NULL, and set the default value for the field

7. Implicit conversion causes index failure

When creating a table, try to be as standard as possible: for example, use int or bigint

Index type (6 commonly used):

Index failure is an all (full table scan)
covering index: Get query results directly from the index. To use a covering index, you need to pay attention to the select query column included in the index column; where conditions include the index column or the leading column of the composite index; query The field length of the result is as small as possible.

1. all

"Full table scan" usually means that your sql statement is in the most native state and has a lot of room for optimization. We must avoid this type of lookup unless you have to.

2. index

This connection type is just another form of full table scan, but its scan order is in accordance with the order of the index. This kind of scan is based on the index and then returns to the table to fetch data. Compared with all, they both obtain the data of the entire table, and the index must first read the index and return to the table to fetch data randomly.

3. range

Range refers to a range of index scans. Compared with the full index scan of index, it has a range limit, so it is better than index. The range is easier to understand, what needs to be remembered is 出现了range,则一定是基于索引的. At the same time, in addition to the obvious between, and and'>','<', in and or are also index range scans.

4. ref

The condition for the appearance of this connection type is: the search condition column uses an index and is not a primary key and unique .
In fact, it means that although the index is used, the value of the index column is not unique, and there will be duplication. In this way, even if the index is used to quickly find the first data, it still cannot be stopped, and a small range scan near the target value is required.
But its advantage is that it does not need to scan the entire table, because the index is ordered, even if there are duplicate values, it is scanned in a very small range.

5. ref_eq

The difference between ref_eq and ref is that it knows that there is only one search result set of this type? Under what circumstances there is only one result set! That is the case where the primary key or unique index is used for search. For example, to find a student of a certain school according to the student ID, we know that there must be only one result before searching, so when we find the student ID for the first time, The query was stopped immediately. This connection type performs precise query every time without excessive scanning, so the search efficiency is higher. Of course, the uniqueness of the column needs to be determined according to the actual situation.

6. const

Under normal circumstances, if a primary key is placed after where as a conditional query, the mysql optimizer can convert the query optimization into a constant. As for how to convert and when to convert, this depends on the optimizer.

Guess you like

Origin blog.csdn.net/Beyond_Nothing/article/details/114253664