Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/75.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/reporting-services/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
带语句的Oracle SQL是否优于subselect_Sql_Oracle_Subquery_Common Table Expression_Query Performance - Fatal编程技术网

带语句的Oracle SQL是否优于subselect

带语句的Oracle SQL是否优于subselect,sql,oracle,subquery,common-table-expression,query-performance,Sql,Oracle,Subquery,Common Table Expression,Query Performance,我有一个超过1000行的Oracle查询。这里不允许拆分和/或使用存储过程。我要让它更长。以下哪项性能更好?我发现这个版本更容易阅读 /* subselect */ select col01 ,col02 from ( select case col01 when 'X' then 1 else 2 end col01 ,case col02 when 'Y' then 3 else 4 end col02 from (

我有一个超过1000行的Oracle查询。这里不允许拆分和/或使用存储过程。我要让它更长。以下哪项性能更好?我发现这个版本更容易阅读

  /* subselect */
  select col01
        ,col02
  from (
    select case col01 when 'X' then 1 else 2 end col01
          ,case col02 when 'Y' then 3 else 4 end col02
    from (      
      select upper(col01) col01
            ,upper(col02) col02
      from (      
        select 'x' as col01
              ,'y' as col02
        from dual
      )
    )
  )
  ;
  ---------------------------------------
  /* with statement */
  with qry01 as (
  select 'x' as col01
        ,'y' as col02
  from dual
  )
  ,qry02 as (
  select upper(col01) col01
        ,upper(col02) col02
  from qry01      
  )
  ,qry03 as (
  select case col01 when 'X' then 1 else 2 end col01
        ,case col02 when 'Y' then 3 else 4 end col02
  from qry02      
  )
  select col01
        ,col02
  from qry03      
  ;

我还发现带有表达式的CTE
更容易阅读。除此之外,还有理由选择它

  • 您可以在主查询中多次使用CTE子查询
  • Oracle优化器可以将CTE子查询的结果存储在动态创建的临时表中。(注意,您甚至可以通过未记录的提示来强制执行它。
    /*+MATERIALIZE*/
  • 您可以递归地使用CTE,请参见

性能应基本相同。Oracle优化器确定最佳查询计划。使用CTE可能会稍微灵活一些(允许具体化),但如果CTE只被引用一次,这并不重要;唯一确定的方法是测试。我猜这些也不是你真正的疑问。谢谢@mustaccio。这个简化的版本非常简单。在花费数小时将其从一个转换到另一个之前,我只是在寻找建议。我相信第二点对于非CTE子查询也是如此。@我不知道,trincot似乎不太清楚:-不要将“物化”与“合并查询”混为一谈。我不是说强制部分(更不用说“合并查询”),而是关于“Oracle优化器可以…”我相信它可以。