我有一个大表,其中约有 700 万条记录。 (MySQL)
列就像;
id | atype | textbody | ....
id为主键,atype为索引
当我运行时
select * from tablename where `atype`='doit'
它使用atype索引。 (表中有 170 万个 doit 行)
但是当我运行这个查询时
select * from tablename where `atype`='doit' or `atype`='payment'
它不使用索引。我只说possible_index是atype。 (表中有168个付款行)
对此行为有任何解释吗?
如果我运行此查询;
select * from tablename where `atype`='paymentfailed' or `atype`='payment'
它使用类型索引。
所以每当我使用“doit”时,它都不会使用atype索引
最佳答案
MySQL 对索引相当挑剔。我认为 IN
比 OR
更聪明:
select *
from tablename
where `atype` in ('doit', 'payment');
如果这不起作用,请使用union all
:
select *
from tablename
where `atype` = 'doit'
union all
select *
from tablename
where `atype` = 'payment';
关于MySQL使用索引问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37151357/