Don't treat interviewers as gods, because they don't know the answers to many questions. Even what he knows is the wrong answer, and the question he is asking you now may be a technical problem he is facing now.
I met an interviewer today, and he thought he was right. In fact, he is only to deal with you when there are a lot of people in the interview.
Question: Joint index, a joint index of the three fields a, b, and c. index(a,b,c), now I use b,c to search where, will the index be used?
I said maybe not, the interviewer said, you are wrong. The index will be used, so go down and review it again. I still want to explain, but the interviewer was a bit disgusted. Go down and take a look at the joint index.
I mainly have a good temper, otherwise, I just leave.
Now, let's just look at the demo, the evidence speaks.
Create a table statement as shown above, and then add a few pieces of test data.
Then, EXPLAIN.
A screenshot of the execution plan is as follows:
What if I use b,c?
The execution plan is as follows:
It is equivalent to a full table scan.
By analogy, you can use (b,c), (a,c), (c,a), (c,b) and other combinations to try again. You will find that there is a rule, the leftmost matching principle. index(a,b,c) is equivalent to index(a), index(a,b), index(a,b,c) three indexes.
According to our table above, we execute:
You will find that only the first one can use the index, and the other two cannot be searched according to the index.
However, at this time, you must never come to a conclusion, saying: The second and third queries will not be indexed. Because there is a situation, they will still take the index, that is, when there are only three fields a, b, and c in the table.
So, when answering questions, don't be like me, answering so affirmatively. Let me talk about a vague answer. Do not go to the index, optimize SQL and look at EXPLAIN, don't memorize it by rote. Then, besides, if I remember correctly, what should I do?
Sometimes, when there are many people interviewing together, it is very likely that you will find someone to deal with you. Because the real interviewer is too busy. What would you do in this situation? Welcome to leave a comment!