gpt4 book ai didi

MySQL - 具有 'Or' 条件的选择查询的列索引或复合索引

转载 作者:太空宇宙 更新时间:2023-11-03 12:11:22 24 4
gpt4 key购买 nike

我有以下查询:

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

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

我正在使用 InnoDB。

最佳答案

复合索引对此查询帮助不大。您需要两个单独的索引。但是,有时 无法有效优化。

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

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

关于MySQL - 具有 'Or' 条件的选择查询的列索引或复合索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24072941/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com