加快慢速oracle查询

时间:2015-09-24 18:43:51

标签: sql oracle performance query-optimization

我有以下格式的Oracle查询:

select col1, min(col2), max(col2) 
from table1
where col2 between add_months(to_date('12/06/2014', 'mm/dd/yyyy'), -6) and to_date('12/05/2014', 'mm/dd/yyyy') 
and col3 = 'CLICKS' 
and col4 = '-8'  
and col1 in (select col1 from table2  where id = '05742') 
group by col1

在我的架构中,table1非常大(数百万,可能是数十亿条记录),但表2相当小。有关如何优化它的任何想法吗?

1 个答案:

答案 0 :(得分:0)

试试这个:

select t2.col1, min(col2), max(col2)
from table2 t2
join table1 t1 on t1.col1 = t2.col1
  on t1.col2 between add_months(to_date('12/06/2014', 'mm/dd/yyyy'), -6) and to_date('12/05/2014', 'mm/dd/yyyy')
  and t1.col3 = 'CLICKS'
  and t1.col4 = '-8'
where t1.id = '05742' 
group by t2.col1

当table2很小且table1很大时,这将特别有效。

注意:如果table.id的数据类型为 numeric (而不是文本),则应将where子句条件编码为:

where t1.id = 5742

所以值类型与列类型匹配,以防db是愚蠢的并且转换列以匹配值而不是相反的方式因此不使用此索引,我已经看过很多次。