MySQL - 选择查询的列索引或复合索引使用'或'条件

时间:2014-06-06 01:49:33

标签: mysql sql indexing innodb

我有以下查询:

SELECT *
FROM myTable 
WHERE columnOne = 1 OR columnTwo = 1;

我应该使用两个列索引(一个在columnOne上,一个在columnTwo上)还是一个复合索引,大约是columnOne, columnTwo

我正在使用InnoDB。

1 个答案:

答案 0 :(得分:3)

复合索引对此查询没有多大帮助。你会想要两个单独的索引。但是,有时or无法有效优化。

这是另一种最适合这两个索引的方法,mytable(columnOne)mytable(columnOne, columnTwo)

select *
from mytable
where columnOne = 1
union all
select *
from mytable t
where columnTwo = 1 and columnOne <> 1;