Interview MySQL-Index

Preface

No foreword, don’t talk nonsense, just start the interview

Interview begins

Interviewer: What is an index

  • Index is a data structure, a data structure that improves retrieval efficiency. Such as B+ tree, hash

Interviewer: Tell me about your understanding of B+ trees

  • The B+ tree is a balanced multi-branch tree. Compared with the B tree, the data of the B+ tree only has leaf nodes. The leaf nodes form a linked list, so range queries can be supported. The query efficiency of B+ tree is: O(logH), H is the height of B+ number.

Interviewer: Why does InnoDB use B+ tree as index structure instead of B tree?

  • First of all, the B+ tree is compared with the B tree. Because the non-leaf nodes also store data, the non-leaf nodes can store fewer memory pages. At the same amount of data, the height of the B tree is higher than that of the B+ tree, so more IO times are required, which makes the overall efficiency of the B tree slower than the B+ tree.
  • The B-tree does not support range queries, because the leaf nodes of the B-tree do not form a linked list.
  • The data of the B+ tree is in the leaf nodes, and the query performance is more stable.

B tree (picture taken from the Internet)
B+ tree (picture taken from the Internet)

Interviewer: What are the categories of the index

  • Primary key index, non-primary key index (normal index, unique index, joint index)

Interviewer: What is the difference between a primary key index and a non-primary key index?

  • In InnoDB, the primary key index is also called a clustered index, and the leaf node stores the entire row of data
  • In InnoDB, non-primary key indexes are also called secondary indexes, and the content of leaf nodes is the value of the primary key

mysql> create table T(
id int primary key, 
k int not null, 
name varchar(16),
index (k))engine=InnoDB;

Primary key index and non-primary key index

Interviewer: Do you know how to return the form?

  • If the statement is select * from T where ID=300, that is, the primary key query method, you only need to search the B+ tree of ID;
  • If the sentence is select * from T where k=3, that is, the normal index query mode, you need to search the k index tree first, get the value of ID 300, and then search the ID index tree again. This process is called returning to the table.
  • In other words, queries based on non-primary key indexes need to scan one more index tree. Therefore, we should try to use primary key queries in our applications.

Interviewer: The principle of hit index?

  • As long as it satisfies either of the "covering index principle" or the "leftmost prefix principle", the index can be used.
  • For details, see MySQL Index Usage Rules

Interviewer: What is a covering index?

  • Covering index means that the execution of a query statement can be obtained only from the index, without reading from the data table. It can also be said that index coverage is achieved.

Interviewer: The last question, how to solve a slow query?

  • Locate the problem: Firstly, by querying the slow log, determine which SQL statement has the slow query
  • Analyze the problem: Next, there are generally two reasons for SQL slowness, one is that the index is not used, and the other is the use of the wrong index
  • Solve the problem: If the index is not used, then we can use explain to analyze the SQL statement to determine whether the index is used. If you use an index, do you use the right index? If you use the wrong index, use force index() to force the use of an index. If the interviewer continues to ask you, why did you use the wrong index? Simply put, which index InnoDB chooses to use is determined based on a value, and this value is calculated based on the sample value. Because it is a sample, the sampling is relatively random, so it is not impossible to make mistakes.

Interviewer: Congratulations on entering the next round of interview at the end of the interview

to sum up

In fact, there are still a lot of knowledge points about the index, which will not be expanded here.

  • What field to choose for indexing
  • Index push down
  • The meaning of the explain field, how to tune according to the field

Talk

Thank you very much for seeing this. If you think the article is well written, please pay attention to it and share it (it is very, very useful for me).
If you think the article needs to be improved, I look forward to your suggestions for me, please leave a message.
If you want to see something, I look forward to your message.
Your support and support is the greatest motivation for my creation!

Reference

  • "High Performance MySQL"
  • "45 Lectures on MYSQL Actual Combat"

Guess you like

Origin blog.csdn.net/Aaron_Tang_/article/details/114676877