Index usage-a little trick

1.select * from user_info a where user_no = 59027 A large number of slow queries appear continuously

The field user_no in the user_info table has an index, and the index is very differentiated. It always reports that the query is slow. After checking the execution plan, it is found that the index is never used.

Reason: user_no in the user_info table is of varchar type, but the query condition is of numeric type. In this case, the index will be invalid. Modify the statement to:

select * from user_info a where a.user_no = '59027', the index is used normally.

A little trick: No matter whether the field is int or varchar, the value in the query condition must be added with single quotes and treated as a string. That is to say, if the value is a string, it can be indexed. If the value is a numeric type, the index will not be used if the type is incorrect.

There are always unexpected places, so learn more and ask more questions.

2. Common methods for calculating index distinction

select count(distinct user_no)/count(1) from user_info;

user_no is an index field. Generally, the distinction is acceptable if it reaches 0.1. If it is a joint index, focus on the distinction of the first field.


Guess you like

Origin blog.csdn.net/weichao9999/article/details/80334231