JAVA interview question 06-Mysql index

1. What scenarios are suitable for index creation, and what scenarios are not suitable for index creation. ?
1.1 Where, order by, and group by appear frequently, and the columns with relatively discrete data distribution are suitable for creating indexes. For example, there is a user table, user name, and mobile phone number are often used as query conditions, and different user names and mobile phone numbers are different (the data is relatively discrete), so it is suitable for index creation. However, although the user's gender is often used as a query condition, because the gender is only male, female, unknown, etc., the data is not discrete enough, so it is not suitable for index creation. Why discrete?
1.2 Frequently modified columns are not suitable for creating indexes. The mysql index is implemented based on the B+ tree. When modifying data, you need to modify the corresponding index.

2. Joint index hit rules?
The hit rule of the joint index is the leftmost matching principle (mysql index is implemented based on B+ tree).
Assuming that the three columns of A, B, and C have created a joint index, which
can hit the index

where A=xx
where A=xx and B=xx
where A=xx and B=xx and C=xx

can't hit the index

where B=xx
where C=xx
where B=xx and c=xx

3. Index hit rules
Expand the second question, which is also the leftmost match (mysql index is implemented based on B+ tree), assuming that the index created in column A
can be hit

<,<=,=,>,>=,BETWEEN,IN, like 'xx%'

Can't hit

<>,not in ,!=,like '%xx'

String to number. For example mobile varchar(11)

 where mobile=131xxxxxxxx

The case of performing function operations on columns

where md5(password) =' xxxx'

NULL will cause the index to be useless, so the existence of NULL should be avoided when designing the table structure (use other ways to express the NULL you want to express, such as -1?)

4. Data structure implementation of mysql index.
To put it simply, the mysql index is implemented based on the b+ tree. If you are interested, you can search it yourself. It is worth spending some time to study it.
Non-leaf nodes only store the range of child node data, and the data only exists in leaf nodes.

Guess you like

Origin blog.csdn.net/m0_67265464/article/details/126741076