Precautions for using indexes in Mysql single table (avoid invalidation)

Mysql single table use index

1. Try to build a full index

The queried fields can be matched in the index in order!

The order of query fields in SQL is related to the order of fields in the index. But on the premise of not affecting the SQL execution results, it will be automatically optimized for you. no order limit

insert image description here

2. Best Left Prefix Rule

The difference in the order of the query field and the index field will cause the index to not be fully used, or even the index to fail!

Reason: To use a composite index, you need to follow the best left prefix rule, that is, if you index multiple columns, you need to follow the leftmost prefix rule. Refers to the query starting from the leftmost front column of the index and not skipping columns in the index.

Conclusion: To use the index, the filter conditions 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.

3. Do not do any calculations on indexed columns

Doing no operations (calculation, function, (automatic or manual) type conversion) on the index column will cause the index to fail and turn to full table scan.


The function EXPLAIN SELECT SQL_NO_CACHE * FROM emp WHERE age=30 is used on the query column

EXPLAIN SELECT SQL_NO_CACHE * FROM emp WHERE LEFT(age,3)=30;

Conclusion: There is no calculation on the left side of the equal sign!

3.2 Make a conversion on the query column
create index idx_name on emp(name);

explain select sql_no_cache * from emp where name=‘30000’;

explain select sql_no_cache * from emp where name=30000;

If the string does not add single quotes, a conversion will be done on the name column!

Conclusion: No conversion on the right side of the equal sign!

4. There cannot be range queries on indexed columns

explain SELECT SQL_NO_CACHE * FROM emp WHERE emp.age=30 and deptid=5 AND emp.name = ‘abcd’;

explain SELECT SQL_NO_CACHE * FROM emp WHERE emp.age=30 and deptid<=5 AND emp.name = ‘abcd’;

insert image description here

Suggestion: Put the index order of fields that may be used for range queries at the end

5. Try to use covering index

That is, the query column and the index column are the same, do not write select *!

explain SELECT SQL_NO_CACHE * FROM emp WHERE emp.age=30 and deptId=4 and name=‘XamgXt’;

explain SELECT SQL_NO_CACHE age,deptId,name FROM emp WHERE emp.age=30 and deptId=4 and name=‘XamgXt’;

6. When using not equal to (!= or <>)

When mysql uses not equal (!= or <>), sometimes it cannot use the index and cause a full table scan.

insert image description here

7. Field is not null and is null

When the field is allowed to be Null:

is not null does not use the index, is null can use the index.

8. Fuzzy matching before and after like

insert image description here
The prefix cannot be fuzzy matched!

9. Reduce the use of or

Use union all or union instead:

insert image description here

Guess you like

Origin blog.csdn.net/qq_43688472/article/details/106571651