oracle 9i r2中引入了SQL-99中的with子句,它是一个实现子查询的工具,
我们可以用它来临时存储oracle经过计算得到的结果集。
with子句的作用有点类似global temporary tables,设计with子句目的,
是为提高复杂子查询的速度。下面是with子句的一些要点:
1.with子句应用于oracle 9i以及更高版本
2.正式的,with子句称作分解子查询
3.with子句应用于当一个查询被多次执行的情况
4.with子句对于递归查询也很有用(SQL-99,oracle不支持)
为了简单起见,下面的例子仅仅引用集合一次;
而with子句通常被用在一个查询中多次引用一个集合情况下。
--
with子句能够简化复杂的sql查询
在SQL-99中,我们可以用with子句来代替临时表。
oracle中的with子句一次性获得一个集合,并为其取一个名,
以便于我们在后面的查询中引用到它。
--
首先,SQL-99中的with子句让人很困惑,因为这个sql语句不是以select开始的;
然而,我们的查询语句可以以with子句开始,定义一个集合,在主查询中引用到集合,
它就好像一个"真正的表":
with subquery_name
as (the aggregation SQL statement)
select (query naming subquery_name);
--
回到我们过于简单化的例子,我们用with子句代替临时表(注意:通过使用global temporary table,
你会发现你的执行计划更优了,不过这取决于你的oracle版本):
WITH sum_sales AS(select /*+ materialize */ sum(quantity) all_sales from stores ),
number_stores AS(select /*+ materialize */ count(*) nbr_stores from stores ),
sales_by_store AS(select /*+ materialize */ store_name, sum(quantity) store_sales from store natural join sales )
SELECT store_name
FROMstore,sum_sales,number_stores,sales_by_store
wherestore_sales > (all_sales / nbr_stores);
--
注释掉了oracle中没有公开的实现提示;oracle中的实现提示是用来确定
在with子句内部创建的临时表是基于开销优化的。对于oracle 10g版本来说,
这是不必要的,但是它确保了这个表只被创建一次。
应该指出的是,with子句在oracle中并不是完全的起作用,
oracle的sql中就不支持用with来替代connect by的递归查询。
--
Here is an actual performance comparison of equivalent queries:
SQL> --*********************************************
SQL> -- Using subqueries
SQL> --*********************************************
SQL>
SQL> select2       store_name,3       sum(quantity)                                                  store_sales,4       (select sum(quantity) from sales)/(select count(*) from store) avg_sales5  from6       store  s,7       sales  sl8  where9       s.store_key = sl.store_key10  having11       sum(quantity) > (select sum(quantity) from sales)/(select count(*) from store)12  group by13       store_name14  ;
----------------------------------------------------------------------------------------------
| Id  | Operation                      | Name        | Rows  | Bytes | Cost (%CPU)| Time     |
----------------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT               |             |     1 |    31 |     4  (25)| 00:00:01 |
|   1 |  SORT AGGREGATE                |             |     1 |     4 |            |          |
|   2 |   TABLE ACCESS FULL            | SALES       |   100 |   400 |     2   (0)| 00:00:01 |
|   3 |    SORT AGGREGATE              |             |     1 |       |            |          |
|   4 |     INDEX FULL SCAN            | SYS_C003999 |    10 |       |     1   (0)| 00:00:01 |
|*  5 |  FILTER                        |             |       |       |            |          |
|   6 |   HASH GROUP BY                |             |     1 |    31 |     4  (25)| 00:00:01 |
|   7 |    NESTED LOOPS                |             |   100 |  3100 |     3   (0)| 00:00:01 |
|   8 |     TABLE ACCESS FULL          | SALES       |   100 |   900 |     2   (0)| 00:00:01 |
|   9 |     TABLE ACCESS BY INDEX ROWID| STORE       |     1 |    22 |     1   (0)| 00:00:01 |
|* 10 |      INDEX UNIQUE SCAN         | SYS_C003999 |     1 |       |     0   (0)| 00:00:01 |
|  11 |   SORT AGGREGATE               |             |     1 |     4 |            |          |
|  12 |    TABLE ACCESS FULL           | SALES       |   100 |   400 |     2   (0)| 00:00:01 |
|  13 |     SORT AGGREGATE             |             |     1 |       |            |          |
|  14 |      INDEX FULL SCAN           | SYS_C003999 |    10 |       |     1   (0)| 00:00:01 |
----------------------------------------------------------------------------------------------                                                                                                                                                                                                                                      113  consistent gets //IO数量
--
SQL> --*********************************************
SQL> -- Using CTAS(create table as select)
SQL> --*********************************************
SQL> create table t1 as select sum(quantity) all_sales from sales;
Table created.
SQL> create table t2 as select count(*) nbr_stores from store;
Table created.
SQL> create table t3 as select store_name, sum(quantity) store_sales from store natural join sales group by store_name;
Table created.
SQL>
SQL> select2    store_name3    from4    t1,5    t2,6    t37    where8    store_sales > (all_sales / nbr_stores);
------------------------------------------------------------------------------
| Id  | Operation             | Name | Rows  | Bytes | Cost (%CPU)| Time     |
------------------------------------------------------------------------------
|   0 | SELECT STATEMENT      |      |     1 |    61 |     6   (0)| 00:00:01 |
|   1 |  NESTED LOOPS         |      |     1 |    61 |     6   (0)| 00:00:01 |
|   2 |   MERGE JOIN CARTESIAN|      |     1 |    26 |     4   (0)| 00:00:01 |
|   3 |    TABLE ACCESS FULL  | T1   |     1 |    13 |     2   (0)| 00:00:01 |
|   4 |    BUFFER SORT        |      |     1 |    13 |     2   (0)| 00:00:01 |
|   5 |     TABLE ACCESS FULL | T2   |     1 |    13 |     2   (0)| 00:00:01 |
|*  6 |   TABLE ACCESS FULL   | T3   |     1 |    35 |     2   (0)| 00:00:01 |
------------------------------------------------------------------------------                                                                                                                                                                                                                                               30  consistent gets
--
SQL> --*********************************************
SQL> -- Using the WITH clause
SQL> --*********************************************
SQL>
SQL> with2  number_stores as3       (select count(*) nbr_stores from store),4  total_sales as5       (select sum(quantity) all_sales from sales),6  store_sales as7       (select store_name, sum(quantity) sales from store natural join sales group by store_name)8  select9       store_name10  from11       number_stores,12       total_sales,13       store_sales14  where15       sales > (all_sales / nbr_stores);
-----------------------------------------------------------------------------------------------
| Id  | Operation                       | Name        | Rows  | Bytes | Cost (%CPU)| Time     |
-----------------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT                |             |     1 |    61 |     7  (15)| 00:00:01 |
|   1 |  NESTED LOOPS                   |             |     1 |    61 |     7  (15)| 00:00:01 |
|   2 |   NESTED LOOPS                  |             |     1 |    26 |     3   (0)| 00:00:01 |
|   3 |    VIEW                         |             |     1 |    13 |     1   (0)| 00:00:01 |
|   4 |     SORT AGGREGATE              |             |     1 |       |            |          |
|   5 |      INDEX FULL SCAN            | SYS_C003999 |    10 |       |     1   (0)| 00:00:01 |
|   6 |    VIEW                         |             |     1 |    13 |     2   (0)| 00:00:01 |
|   7 |     SORT AGGREGATE              |             |     1 |     4 |            |          |
|   8 |      TABLE ACCESS FULL          | SALES       |   100 |   400 |     2   (0)| 00:00:01 |
|*  9 |   VIEW                          |             |     1 |    35 |     4  (25)| 00:00:01 |
|  10 |    SORT GROUP BY                |             |    10 |   310 |     4  (25)| 00:00:01 |
|  11 |     NESTED LOOPS                |             |   100 |  3100 |     3   (0)| 00:00:01 |
|  12 |      TABLE ACCESS FULL          | SALES       |   100 |   900 |     2   (0)| 00:00:01 |
|  13 |      TABLE ACCESS BY INDEX ROWID| STORE       |     1 |    22 |     1   (0)| 00:00:01 |
|* 14 |       INDEX UNIQUE SCAN         | SYS_C003999 |     1 |       |     0   (0)| 00:00:01 |
-----------------------------------------------------------------------------------------------                                                                                                                                                                                                                                       109  consistent gets

原文:http://www.dba-oracle.com/t_with_clause.htm

oracle CTE 简介相关推荐

  1. oracle的cte,oracle CTE 简介 | 学步园

    oracle 9i r2中引入了SQL-99中的with子句,它是一个实现子查询的工具, 我们可以用它来临时存储oracle经过计算得到的结果集. with子句的作用有点类似global tempor ...

  2. Oracle数据类型简介【转贴】

    为什么80%的码农都做不了架构师?>>>    Oracle数据类型简介 一.概述 在ORACLE8中定义了:标量(SCALAR).复合(COMPOSITE).引用(REFERENC ...

  3. 1、oracle数据库简介

    1.ORACLE数据库简介 数据库指的是存储和管理数据的仓库,是一种文件集合(包括数据文件.临时文件.日志文件和控制文件),我们一般所说的数据库指的是数据库管理系统,一种用于操作数据库的软件,简称DB ...

  4. oracle的partition,ORACLE PARTITION简介

    一.Oracle分区简介 ORACLE的分区是一种处理超大型表.索引等的技术.分区是一种"分而治之"的技术,通过将大表和索引分成可以管理的小块,从而避免了对每个表作为一个大的.单独 ...

  5. Oracle索引简介

    Oracle索引简介 索引(INDEX)是为了加快数据的查找而创建的数据库对象,特别是对大表,索引可以有效地提高查找速度,也可以保证数据的惟一性.索引是对数据库表中一列或多列的值进行排序的一种结构使用 ...

  6. Oracle分区简介

    Oracle分区简介 partition 一.Oracle分区简介 ORACLE的分区是一种处理超大型表.索引等的技术.分区是一种"分而治之"的技术,通过将大表和索引分成可以管理的 ...

  7. oracle中的crs,Oracle CRS简介

    Oracle CRS简介 从Oracle 10gR1 RAC 开始,Oracle推出了自身的集群软件,这个软件的名称叫做Oracle Cluster Ready Service(Oracle集群就绪服 ...

  8. oracle的crs是什么,Oracle CRS简介

    Oracle CRS简介 从Oracle 10gR1 RAC 开始,Oracle推出了自身的集群软件,这个软件的名称叫做Oracle Cluster Ready Service(Oracle集群就绪服 ...

  9. Oracle MTS简介

    Oracle MTS简介 简介: 一.什么是MTS MTS = Multi-Threaded Server MTS是ORACLE SERVER的一个可选的配置选择,是相对DEDICATE方式而言,它最 ...

最新文章

  1. php jquery选择器,常用jQuery选择器总结_jquery
  2. 图解设计模式:抽象工厂
  3. Xcode做简易计算器
  4. (计算机组成原理题目题型总结)第五章:中央处理器
  5. Spring 处理请求和响应相关的注解
  6. 专访黄翀:东方航空到底用MongoDB做了什么,技术选型为何花落MongoDB?
  7. 文件上传中的临时上传路径问题
  8. javax maven项目缺少_maven冲突解决流程
  9. python遗传算法求解TSP问题
  10. 苹果手机如何投屏到电脑【无线有线】
  11. 苹果6s强制删除id锁_付费苹果解锁软件 屏幕解锁及AppleID解锁
  12. 如何使用码云Gitee上传本地项目
  13. android保存裁剪图片,Android选择图片并裁剪,无法保存经过裁剪的图片
  14. 计算机毕业设计php的人事档案管理系统
  15. php读取加密表格,Excel表格如何加密
  16. matlab图上输入希腊字母,Matlab中给图形添加【希腊字母】
  17. neo4j--Cypher语法练习(WITH、 FOREACH、Aggregation、UNWIND、UNION、CALL)
  18. Invalid value for option“watch“:expected anObject,but got Function.
  19. ART-PI 嵌入式人形检测 附源码(RT-AK Demo)
  20. 游戏建模基本流程(讲解建模经验)

热门文章

  1. python求乘积_Python实现求笛卡尔乘积的方法
  2. python两个字符串查找公共字符串
  3. 如何区分企业微信的内部群和外部群
  4. R语言画图中输出上或者下标的方法
  5. VS2015中使用libcurl环境搭建
  6. 时序预测大厂算法岗位总结(含校招入口)
  7. Appium安装卸载和判断应用是否已经安装
  8. 华为p8刷机失败后无限重启 强制关机也不行
  9. 【好用的工具】视频剪辑工具
  10. 使用seqtk截取fastq数据