内联查询与性能问题的条款

时间:2013-02-15 06:55:37

标签: sql performance oracle11g database-performance

我的查询结构如下:

select (select first_name 
        from sub_table_1,
             sub_table_2,
             sub_table_3 
        where <where clause for sub_table 1,2,4> 
          and sub_table_1.col_1=table_1.col_1) first_name ,
       (select last_name 
        from sub_table_1,
             sub_table_2,
             sub_table_3 
        where <where clause for sub_table 1,2,4> 
              and sub_table_1.col_1=table_1.col_1) last_name ,
       <other select clause for table 1,2,3>
from table_1,
     table_2,
     table_3
where <Where clauses>
union
select (select first_name 
        from sub_table_1,
             sub_table_2,
             sub_table_3 
        where <where clause for sub_table 1,2,3> 
          and sub_table_1.col_1=table_4.col_1) first_name ,
       (select last_name 
        from sub_table_1,
             sub_table_2,
             sub_table_3 
        where <where clause for sub_table 1,2,3> 
          and sub_table_1.col_1=table_4.col_1) last_name ,
       <other select clause for table 4,5,6>
from table_4,
     table_5,
     table_6
where <Where clauses>

我想作为我是否参与其中:

(select first_name , last_name
    from sub_table_1,
    sub_table_2,
    sub_table_3 
    where <where clause for sub_table 1,2,3> 
    and sub_table_1.col_1=table_4.col_1) first_name ,
        (select last_name 
    from sub_table_1,
    sub_table_2,
    sub_table_3 
    where <where clause for sub_table 1,2,3> )

将帮助我更快更好地进行查询,或者会对查询产生负面影响。

另请注意,此子查询可以在其中获取大约10000条记录。

请帮助

3 个答案:

答案 0 :(得分:4)

据我所知,公用表表达式(WITH子句)对查询有两个潜在的影响。

  • 它们允许优化器实现结果集,有效地创建一个临时表来保存结果。据我所知,它没有记录它执行此操作的条件,但我已经读过,如果集合的大小超过会话的排序区域大小,并且结果将多次使用,则会发生这种情况。查询。

  • 有时会出现与CTE查询优化相关的错误。

所以在性能方面,如果你有一个很长时间使用的大结果集,那么我认为它是一个更好的CTE候选者。

我倾向于在Oracle和PostgreSQL中大量使用它们,因为我发现它们使代码比嵌套内联视图更清晰。

答案 1 :(得分:1)

如果继续以子查询的形式进行,那么使用CTE / WITH子句不太可能影响性能,因为SQL解释器会将其有效地扩展为长格式 - 出于这种目的(即不使用的地方)递归CTE),CTE的使用实际上是一种编写更干的代码的方法。

确定,比较两个版本生成的查询计划。

在任何一种情况下,现有的查询结构都是非常低效的,因为它实际上必须为主查询中返回的每一行单独执行每个子查询。

答案 2 :(得分:1)

根据我在11gR2中的经验,使用WITH子句可能会更慢。提示materialize可以非常高效并大大缩短执行时间。 但正如Mark Ba​​nnister所说,要做一些真正的调整,你需要制定执行计划。