If you often have this kind of query, it is recommended to build a joint index of a and b, which is the most effective optimization.
The disadvantage is that creating a new index will increase the space occupied and reduce the insertion speed
When a joint index is not built and both columns have indexes, mysql will use its own algorithm to select the fastest index in the selected area,
If you know whether it is faster to search from a or b, you can use forced index FORCE INDEX (FIELD1)
In the case you mentioned, AND the order of the pre and post conditions does not affect the efficiency of the query, there is no difference; since these two fields are query conditions, a joint index can be established to improve query efficiency
I remember that my previous company had a business that also involved this point. The result of the experiment at that time was that mysql queries were from right to left. Now, this point should be optimized. No specific experiments have been done
It doesn’t matter the order of writing.
If the joint index is established, the segment order of the fields in the joint index will affect the query efficiency. The basic principle is to put the most distinctive fields first. (Tested that the order of the indexes is very important)
MySQL’s and is satisfied at the same time, and it does not distinguish who is satisfied more and who is satisfied less.
It may have an impact. You can use
explain
to check the specific situationa
andb
, which is the most effective optimization.The disadvantage is that creating a new index will increase the space occupied and reduce the insertion speed
If you know whether it is faster to search from
a
orb
, you can use forced indexFORCE INDEX (FIELD1)
explain
to observe and optimize your sqlIn the case you mentioned,
AND
the order of the pre and post conditions does not affect the efficiency of the query, there is no difference; since these two fields are query conditions, a joint index can be established to improve query efficiencyI remember that my previous company had a business that also involved this point. The result of the experiment at that time was that mysql queries were from right to left. Now, this point should be optimized. No specific experiments have been done
It doesn’t matter the order of writing.
If the joint index is established, the segment order of the fields in the joint index will affect the query efficiency. The basic principle is to put the most distinctive fields first. (Tested that the order of the indexes is very important)
If mysql’s query planning couldn’t even do this, mysql would have died a long time ago