Mysql 优化包含';或';
查询:Mysql 优化包含';或';,mysql,indexing,Mysql,Indexing,查询: 此查询是否使用索引(a,b)?首先,我将尝试使用中的重写此查询: select * from t where (a = '..' or a = '..') and b = '..' 其次,更好的索引应该是t(b,a)。b没有问题,因为它只在where子句中出现一次,带有= 第三,如果这些不能加快查询速度,则使用union all: select * from t where a in ('..', '..') and b = '..'; 这将绕过where子句优化的限制。解释说什么?
此查询是否使用索引(a,b)?首先,我将尝试使用
中的重写此查询:
select * from t where (a = '..' or a = '..') and b = '..'
其次,更好的索引应该是t(b,a)
。b
没有问题,因为它只在where
子句中出现一次,带有=
第三,如果这些不能加快查询速度,则使用union all
:
select *
from t
where a in ('..', '..') and b = '..';
这将绕过where
子句优化的限制。解释说什么?请参阅此处的文档:并升级到MySQL 5.6,因为它比早期版本更好。@BillKarwin。酷。这意味着MySQL 5.6在这种情况下将使用索引(a,b)
。我还没有对其进行基准测试,但我认为(b,a)上的索引仍然会更好。
select *
from t
where a = '..' and b = '..'
union all
select *
from t
where a = '..' and b = '..';