【发布时间】:2014-06-06 01:49:33
【问题描述】:
我有以下疑问:
SELECT *
FROM myTable
WHERE columnOne = 1 OR columnTwo = 1;
我应该使用两个列索引(一个在columnOne 上,一个在columnTwo 上)还是按columnOne, columnTwo 顺序的复合索引?
我正在使用 InnoDB。
【问题讨论】:
我有以下疑问:
SELECT *
FROM myTable
WHERE columnOne = 1 OR columnTwo = 1;
我应该使用两个列索引(一个在columnOne 上,一个在columnTwo 上)还是按columnOne, columnTwo 顺序的复合索引?
我正在使用 InnoDB。
【问题讨论】:
复合索引对这个查询没有多大帮助。您需要两个单独的索引。但是,有时or 的优化效率并不高。
这是另一种最适合这两个索引的方法,mytable(columnOne) 和 mytable(columnOne, columnTwo):
select *
from mytable
where columnOne = 1
union all
select *
from mytable t
where columnTwo = 1 and columnOne <> 1;
【讨论】: