Chapter 20: MySQL Index Failure Cases

1. Full Value Match My Favorites

When SQL query

EXPLAIN SELECT SQL_NO_CACHE * FROM student WHERE age=30 AND classId=4 AND NAME='abcd';

Create 3 indexes

idx_age,idx_age_classid,idx_age_classid_name

The current optimizer will select the idx_age_classid_name index that matches the where condition the highest, directly query the corresponding primary key value and then query back to the table, which is the most efficient at this time. Therefore, some indexes are invalid, because some indexes used will query multiple primary key values ​​and need to go back to the table to continue to judge, which is inefficient.

2. Best left prefix rule

 When SQL query

EXPLAIN SELECT SQL_NO_CACHE * FROM student WHERE age=30 AND NAME='abcd';

create index

idx_age_classid_name

At this time, only the index age is used, because the index field is skipped, and the classid_name in the latter part is not used.

Conclusion: MySQL can create a joint index for multiple fields. The where condition to use the index must be satisfied in the order in which the index was created. Once a field is skipped, the fields behind the index cannot be used. If the where condition does not use the first field, the index will not be available.

3. Primary key insertion order

If the current page is full of data

 At this time, insert a record with a primary key value of 9

It will cause page splitting, splitting the current page into two pages. Moving some records of this page to a new page will cause performance loss. So the primary key value should be incremented sequentially. The primary key has auto_increment, which is written sequentially to reduce page splits.

4. Calculations, functions, and type conversions cause the index to fail.

① Function index invalid

Create index: idx_name

Usage index: SELECT * FROM student where name like 'abc%'

Without using an index: SELECT * FROM student where LEFT(name,3) = 'abc'

②Calculation index failure

Create index: idx_sno

Use index: SELECT * FROM student where stuno = 123;

Without indexing: SELECT * FROM student where stuno+1 = 123;

③The type conversion index is invalid

Create index: idx_name

Using an index: SELECT * FROM student where name = 'abc'

Without index: SELECT * FROM student where name = 123

5. The column index on the right side of the range condition (greater than less than) is invalid

Create index: idx_age_classId_name

SQL语句: select * from student where age = 30 and classId>20 and name='abc'

Only use the age_classId part of the index, and the name index is invalid.

Optimization: Create a joint index, put the field of the range condition at the end of the index, idx_age_name_classId

6. Where conditions are not equal to, greater than, less than, index failure

7. is null can use index (equal), is not null cannot use index (not equal)

Optimization: set not null constraints on the fields in the design data table.

8. The index at the beginning of the fuzzy query % is invalid

Optimization: page search is strictly prohibited left blurred or full blurred, if necessary, use a search engine

9. There are non-indexed columns before and after or, and the index fails

Or before and after the field must exist index, can be used

10. The character set of the database and the table uses utf8mb4 uniformly, and the index will become invalid if the comparison of different character sets needs to be converted

【Exercise】index(a,b,c)

 

【suggestion】

①Single-column index, the index with better filterability for current search

②Combined index: the index with good filterability is at the front, contains more fields with where conditions, and the range query appears at the end

Guess you like

Origin blog.csdn.net/jbkjhji/article/details/131374508