【问题标题】:MySQL - Column Indices or Composite Index for a Select Query With an 'Or' ConditionalMySQL - 具有“或”条件的选择查询的列索引或复合索引
【发布时间】:2014-06-06 01:49:33
【问题描述】:

我有以下疑问:

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

我应该使用两个列索引(一个在columnOne 上,一个在columnTwo 上)还是按columnOne, columnTwo 顺序的复合索引?

我正在使用 InnoDB。

【问题讨论】:

    标签: mysql sql indexing innodb


    【解决方案1】:

    复合索引对这个查询没有多大帮助。您需要两个单独的索引。但是,有时or 的优化效率并不高。

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

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

    【讨论】:

    • @user3678068 。 . .谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-12
    • 2020-04-27
    • 2016-02-22
    • 2012-05-14
    相关资源
    最近更新 更多