Index failure caused by Mysql data type

Record the relatively hidden index failures that you find during your work.

When we design the use of the table structure, we need to first preset the index of the data that needs to be frequently queried later. There is also an important index optimization. On the surface, it seems to have no impact on the index. That is, when designing the types of fields in the table, when querying When the field types do not match, the index is invalid.

example:

1. Joint table index failure

If the field data types of the two connected tables are different, for example, one is int and the other is varchar, even if the index is created at this time, it will be invalid. Why would it fail if both are varchar? At this time, you must ensure that their characters are of the same type, as shown in the figure

 2. Conditional query index invalid

If you are querying a date string, for example:

 

 At this time you are using select * from table where ds = 20210909

You will find that there is no indexing

 

 If you change to select * from table at this time, you will find that the index is used 

 Summarize:

Sometimes you will find that the design index of the table is clearly established at work, but the reason for the index failure cannot be found in SQL. At this time, you can start with the table structure. After all, sometimes the table was not designed by you. The table structure of the joint table may also be designed by multiple people, resulting in inconsistent data types. This can also be said to be a common problem.

 

Guess you like

Origin blog.csdn.net/weixin_42736075/article/details/120196522