比如user
表有a
和b
连个字段,而且都做了索引,那么我构建查询语句
SELECT * FROM user WHERE a = 1 AND b = 2
和
SELECT * FROM user WHERE b = 2 AND a = 1
如果满足a条件行数远大于满足b的,是否调整它们的顺序会产生区别?
伊谢尔伦2017-04-17 11:45:39
MySQL’s and is satisfied at the same time, and it does not distinguish who is satisfied more and who is satisfied less.
PHP中文网2017-04-17 11:45:39
It may have an impact. You can use explain
to check the specific situation
ringa_lee2017-04-17 11:45:39
a
and b
, which is the most effective optimization. a
or b
, you can use forced index FORCE INDEX (FIELD1)
explain
to observe and optimize your sqlPHP中文网2017-04-17 11:45:39
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
大家讲道理2017-04-17 11:45:39
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
高洛峰2017-04-17 11:45:39
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)
ringa_lee2017-04-17 11:45:39
If mysql’s query planning couldn’t even do this, mysql would have died a long time ago