MySQL使用索引问题

标签 mysql sql sql-optimization

我有一个大表,其中约有 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 对索引相当挑剔。我认为 INOR 更聪明:

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/

相关文章:

php - MySQL 变音符号不敏感搜索(西类牙口音)

php - 从 Joomla 中的回调 URL 更新 MySQL 表

php - 如何知道 "one to many"数据库中自增列的ID?

sql - 如何优化同一张表中的几个 "WHERE (Select .... ) = value"

sql - SQL查询的临时表与短路操作

MySQL:UNION 是我要找的吗? (将行分解为多行返回)

mysql - 左连接的慢速选择查询为空并限制结果

sql - 在 Sql 中冒泡列

mysql - 如何将列从一个表复制到另一个表

mysql - 使用 from 中的子查询优化 SQL