我们在一个表中有一百万行。
我们的选择:
select * from tableA where column1 in ("a", "b", "c", "d") and where column2 = "abc";
并且我们在column1和column2上具有唯一索引。
有人告诉我要:
select * from tableA where column1 = "a" and column2 = "abc"
union
select * from tableA where column1 = "b" and column2 = "abc"
union
select * from tableA where column1 = "c" and column2 = "abc"
union
select * from tableA where column1 = "d" and column2 = "abc";
IN子句中可以有1到100个不同的值。因此最好是使用IN子句运行一个语句或运行100语句并执行并集。
如果您column1, column2
按顺序有唯一的索引,则带有的版本union
肯定会利用该索引。如评论中所述,您应该使用union all
而不是union
。这消除了删除重复项的步骤(即使没有重复项)。这将是少数索引查找操作,并且应该很快进行。
Oracle是否使用第一个版本所需的索引在某种程度上是未知的:
where column1 in ('a', 'b', 'c', 'd') and column2 = 'abc'
大多数数据库将不会在这种情况下,最佳使用索引。如果数据库使用该索引,它将使用该索引进行column1
查找,然后扫描将值与进行比较的索引column2
。Oracle可能还有一些其他的灵巧之处,它们将在此处有效地使用索引。
但是,很容易解决问题。如果您在上有一个索引column2, column1
,则该索引将用于该where
子句。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句