Answer the question
In order to leave comments, you need to log in
How to properly use indexes in MySQL?
The question is the following. If there is a composite table key on columns col1, col2, col3 and I will start searching by prefix, i.e. WHERE col1=something - will I speed up the selection if I make the condition: WHERE col1=something AND col2=something? Plus, let's imagine that in the case of the first condition and in the case of the second condition, the query will return the same number of rows, i.e. a table with the following content:
+---id---+---col1---+---col2---+---col3---+
| 1 | 1 | 1 | ... |
| 2 | 2 | 2 | ... |
| 3 | 3 | 3 | ... |
| 4 | 4 | 4 | ... |
| 5 | 5 | 5 | ... |
+--------+-----------+-----------+-----------+
etc.
Well, respectively, two options for the request - SELECT * FROM table1 WHERE col1=1
and SELECT * FROM table1 WHERE col1=1 AND col2=1
.
(PS I apologize for writing the structure and data, I don’t know how to arrange it beautifully here, I tried my best ((()
Answer the question
In order to leave comments, you need to log in
Ideally, you should have a composite index on all fields that are used in conditions. SELECT * FROM table1 WHERE col1=1
Here the index on the field col1 SELECT * FROM table1 WHERE col1=1 AND col2=1
will be used Here the composite index on (col1, col2) will be used
Of course, if you have an index on col1 and an index on col2, then MySQL uses them, but the composite will be faster.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question