内联查询与 with 子句的性能问题

Fr_*_*ien -1 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>
Run Code Online (Sandbox Code Playgroud)

我想知道是否参加:

(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> )
Run Code Online (Sandbox Code Playgroud)

将帮助我使查询更快更好,否则会产生不利影响。

另请注意,此子查询本身可能会获取大约 10000 条记录。

请帮忙

Dav*_*dge 5

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

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

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

因此,就性能而言,如果您有一个多次使用的大型结果集,那么我认为它是 CTE 的更好候选者。

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