oracle查询:不同列上的过滤条件相同

时间:2018-03-18 15:30:40

标签: sql oracle oracle11g

我正在处理查询以提高性能。

我看到查询在不同的列上具有相同的过滤条件。只是想想编写查询的更好方法是什么。

select *
from table 1 
where 
col1 in (select filter_value from filtertable where id=1)
or 
col2 in (select filter_value from filtertable where id=1)
or 
col3 in (select filter_value from filtertable where id=1)
or 
col4 in (select filter_value from filtertable where id=1)
or 
col5 in (select filter_value from filtertable where id=1)
or 
col6 in (select filter_value from filtertable where id=1)
or 
col7 in (select filter_value from filtertable where id=1)
or 
col8 in (select filter_value from filtertable where id=1)
....... Same condition till col15 

我尝试使用WITH CLAUSE替换filtertable,但没有多少帮助

with filter_temp
(select /*+ materialize  */ filter_value from filtertable where id=1)
select *from table 1 , filter_temp
where col1 in (filtertable.filter_value)
or 
col2 in (filtertable.filter_value)
or 
col3 in (filtertable.filter_value)
or 
col4 in (filtertable.filter_value)
or 
col5 in (filtertable.filter_value)
or 
col6 in (filtertable.filter_value)
or 
col7 in (filtertable.filter_value)
or 
col8 in (filtertable.filter_value)
....... Same condition till col15 

是否有任何不同的方式来编写此查询。

1 个答案:

答案 0 :(得分:1)

编写查询的较短方法是使用exists

select t1.*
from table1 t1
where exists (select 1
              from filtertable ft
              where ft.id = 1 and
                    ft.filter_value in (t1.col1, t1.col2, . . ., t1.col15)
             );

性能应与您的较长版本非常相似,但这至少更简洁。我认为在某种程度上“更好”。

真正的解决方案是拥有一个联结表,因此您不会在多行的单个列中包含列值,而是将其存储在一行中的不同列中。

相关问题