关于oracle with table as 创建临时表的用法示例以及使用with as 的优点

本文详细介绍了 Oracle 中 WITH 语句的用法及其带来的性能优势。WITH 语句允许将复杂的子查询作为临时表使用,减少重复计算,提高查询效率。

关于oracle with table as 创建临时表的用法示例

1、with table as 相当于建个临时表(用于一个语句中某些中间结果放在临时表空间的SQL语句),Oracle 9i 新增WITH语法,可以将查询中的子查询命名,放到SELECT语句的最前面。


语法就是
with tempname as (select ....)
select ...

例子:
with t as (select * from emp where depno=10)
select * from t where empno=xxx

with
wd as (select did,arg(salary) 平均工资 from work group by did),
em as (select emp.*,w.salary from emp left join work w on emp.eid = w.eid)
select * from wd,em where wd.did =em.did and wd.平均工资>em.salary;



2、何时被清除

临时表不都是会话结束就自动被PGA清除嘛! 但with as临时表是查询完成后就被清除了!

注释:

临时表分类

ORACLE临时表有两种类型:会话级的临时表和事务级的临时表。

1)ON COMMIT DELETE ROWS

它是临时表的默认参数,表示临时表中的数据仅在事物过程(Transaction)中有效,当事物提交(COMMIT)后,临时表的暂时段将被自动截断(TRUNCATE),但是临时表的结构 以及元数据还存储在用户的数据字典中。如果临时表完成它的使命后,最好删除(drop命令)临时表,否则数据库会残留很多临时表的表结构和元数据。

2)ON COMMIT PRESERVE ROWS

它表示临时表的内容可以跨事物而存在,不过,当该会话结束时,临时表的暂时段将随着会话的结束而被丢弃,临时表中的数据自然也就随之丢弃。但是临时表的结构以及元数据还存储在用户的数据字典中。如果临时表完成它的使命后,最好删除(drop命令)临时表,否则数据库会残留很多临时表的表结构和元数据。


23:48:58 SCOTT@orcl> with aa as(select * from dept)
23:57:58   2  select * from aa;

    DEPTNO DNAME          LOC
---------- -------------- -------------
        10 ACCOUNTING     NEW YORK
        20 RESEARCH       DALLAS
        30 SALES          CHICAGO
        40 OPERATIONS     BOSTON

已用时间:  00: 00: 00.12
23:58:06 SCOTT@orcl> select * from aa;
select * from aa
              *
第 1 行出现错误:
ORA-00942: 表或视图不存在


已用时间:  00: 00: 00.02
23:58:14 SCOTT@orcl>

3、就这一功能来说,子查询就可以达到啊,为什么要用with呢? 用with有什么好处?
都能写,但执行计划不同的。当有多个相似子查询的时候,用with写公共部分,因为子查询结果在内存临时表中,执行效率当然就高啦~

4、问题:
有张表数据如下:
aaa 高
bbb 低
aaa 低
aaa 高
bbb 低
bbb 高
需要得到下列结果,
  高 低
aaa 2 1
bbb 1 2
问 SQL 语句怎么写??

答案:
with tt as (
  select 'aaa' id, '高' value from dual union all
  select 'bbb' id, '低' value from dual union all
  select 'aaa' id, '低' value from dual union all
  select 'aaa' id, '高' value from dual union all
  select 'bbb' id, '低' value from dual union all
  select 'bbb' id, '高' value from dual)
SELECT id,
       COUNT(decode(VALUE, '高', 1)) 高,
       COUNT(decode(VALUE, '低', 1)) 低
  FROM tt
 GROUP BY id;
===================================================================
扩展:
Oracle9i新增WITH语法,可以将查询中的子查询命名,放到SELECT语句的最前面。

  一个简单的例子:

SQL> WITH
2 SEG AS (SELECT SEGMENT_NAME, SUM(BYTES)/1024 K FROM USER_SEGMENTS GROUP BY SEGMENT_NAME),
3 OBJ AS (SELECT OBJECT_NAME, OBJECT_TYPE FROM USER_OBJECTS)
4 SELECT O.OBJECT_NAME, OBJECT_TYPE, NVL(S.K, 0) SIZE_K
5 FROM OBJ O, SEG S
6 WHERE O.OBJECT_NAME = S.SEGMENT_NAME (+)
7 ;
OBJECT_NAME OBJECT_TYPE SIZE_K
------------------------------ ------------------- ----------
DAIJC_TEST TABLE 128
P_TEST PROCEDURE 0
IND_DAIJC_TEST_C1 INDEX 128

  通过WITH语句定义了两个子查询SEG和OBJ,在随后的SELECT语句中可以直接对预定义的子查询进行查询。从上面的例子也可以看出,使用WITH语句,将一个包含聚集、外连接等操作SQL清晰的展现出来。

  WITH定义的子查询不仅可以使查询语句更加简单、清晰,而且WITH定义的子查询还具有在SELECT语句的任意层均可见的特点。

  即使是在WITH的定义层中,后定义的子查询都可以使用前面已经定义好的子查询:

SQL> WITH
2 Q1 AS (SELECT 3 + 5 S FROM DUAL),
3 Q2 AS (SELECT 3 * 5 M FROM DUAL),
4 Q3 AS (SELECT S, M, S + M, S * M FROM Q1, Q2)
5 SELECT * FROM Q3;
S M S+M S*M
---------- ---------- ---------- ----------
8 15 23 120

  利用WITH定义查询中出现多次的子查询还能带来性能提示。Oracle会对WITH进行性能优化,当需要多次访问WITH定义的子查询时,Oracle会将子查询的结果放到一个临时表中,避免同样的子查询多次执行,从而有效的减少了查询的IO数量。

WITH能用在SELECT语句中,UPDATE和DELETE语句也是支持WITH语法的,只是需要版本支持:
http://www.oracle.com.cn/viewthread.php?tid=83530

=============================================================================
with
sql1 as (select to_char(a) s_name from test_tempa),
sql2 as (select to_char(b) s_name from test_tempb where not exists (select s_name from sql1 where rownum=1))
select * from sql1
union all
select * from sql2
union all
select 'no records' from dual
       where not exists (select s_name from sql1 where rownum=1)
       and not exists (select s_name from sql2 where rownum=1);

再举个简单的例子

with a as (select * from test)

select * from a;

其实就是把一大堆重复用到的SQL语句放在with as 里面,取一个别名,后面的查询就可以用它

这样对于大批量的SQL语句起到一个优化的作用,而且清楚明了


这是搜索到的英文文档资料(说得比较全,但是本人英文特菜,还没具体了解到,希望各高手具体谈谈这个with
as 的好处)

About Oracle WITH clause
Starting in Oracle9i release 2 we see an incorporation of the SQL-99 “WITH clause”, a tool for materializing subqueries to save Oracle from having to re-compute them multiple times.

The SQL “WITH clause” is very similar to the use of Global temporary tables (GTT), a technique that is often used to improve query speed for complex subqueries. Here are some important notes about the Oracle “WITH clause”:

   ? The SQL “WITH clause” only works on Oracle 9i release 2 and beyond.
   ? Formally, the “WITH clause” is called subquery factoring
   ? The SQL “WITH clause” is used when a subquery is executed multiple times
   ? Also useful for recursive queries (SQL-99, but not Oracle SQL)

To keep it simple, the following example only references the aggregations once, where the SQL “WITH clause” is normally used when an aggregation is referenced multiple times in a query.
We can also use the SQL-99 “WITH clause” instead of temporary tables. The Oracle SQL “WITH clause” will compute the aggregation once, give it a name, and allow us to reference it (maybe multiple times), later in the query.

The SQL-99 “WITH clause” is very confusing at first because the SQL statement does not begin with the word SELECT. Instead, we use the “WITH clause” to start our SQL query, defining the aggregations, which can then be named in the main query as if they were “real” tables:

WITH
subquery_name
AS
(the aggregation SQL statement)
SELECT
(query naming subquery_name);

Retuning to our oversimplified example, let’s replace the temporary tables with the SQL “WITH  clause”:

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
FROM
   store,
   sum_sales,
   number_stores,
   sales_by_store
where
   store_sales > (all_sales / nbr_stores)
;

Note the use of the Oracle undocumented “materialize” hint in the “WITH clause”. The Oracle materialize hint is used to ensure that the Oracle cost-based optimizer materializes the temporary tables that are created inside the “WITH” clause. This is not necessary in Oracle10g, but it helps ensure that the tables are only created one time.

It should be noted that the “WITH clause” does not yet fully-functional within Oracle SQL and it does not yet support the use of “WITH clause” replacement for “CONNECT BY” when performing recursive queries.

To see how the “WITH clause” is used in ANSI SQL-99 syntax, here is an excerpt from Jonathan Gennick’s great work “Understanding the WITH Clause” showing the use of the SQL-99 “WITH clause” to traverse a recursive bill-of-materials hierarchy

The SQL-99 “WITH clause” is very confusing at first because the SQL statement does not begin with the word SELECT. Instead, we use the “WITH clause” to start our SQL query, defining the aggregations, which can then be named in the main query as if they were “real” tables:

WITH
subquery_name
AS
(the aggregation SQL statement)
SELECT
(query naming subquery_name);

Retuning to our oversimplified example, let’s replace the temporary tables with the SQL “WITH” clause”:


参考:

http://blog.youkuaiyun.com/a9529lty/article/details/4923957/

http://blog.youkuaiyun.com/a9529lty/article/details/4923988

http://blog.youkuaiyun.com/a9529lty/article/details/4923948

oracle with    百度

Oracle with语句的用法


Oracle数据库中,WITH AS语法也被称为公共表表达式(CTE),用于创建临时命名的结果集,这些结果集可以在后续的查询中引用。以下是其常见的使用方法: ### 基本用法 创建一个临时表 `t_a`,并对其进行查询,由于with as是内存中的table,所以查询速度相对较快,但数据量较大时不建议使用示例代码如下: ```sql with t_a as( select 18 as age from dual union select 19 as age from dual union select 20 as age from dual ) select avg(age) from t_a; ``` 上述代码中,`t_a` 是临时表名,它包含了 `age` 列,最后对 `age` 列求平均值 [^2]。 ### 插入数据时使用 在向一张表插入数据时也可以使用 `WITH AS` 语法。示例代码如下: ```sql insert into table2 with s1 as ( select rownum c1 from dual connect by rownum <= 10), s2 as ( select rownum c2 from dual connect by rownum <= 10) select a.c1, b.c2 from s1 a, s2 b where ...; ``` 这里创建了两个临时表 `s1` 和 `s2`,并将它们连接后的结果插入到 `table2` 中 [^3]。 ### 多CTE联合使用 可以定义多个CTE,并在主查询中引用它们。示例代码如下: ```sql with dept_costs as ( select d.dname, sum(e.sal) dept_total from emp e, dept d where e.deptno = d.deptno group by d.dname ), avg_cost as ( select sum(dept_total) / count(1) avg from dept_costs ) select * from dept_costs where dept_total < (select avg from avg_cost); ``` 代码中定义了 `dept_costs` 和 `avg_cost` 两个CTE,主查询从 `dept_costs` 中筛选出部门总薪资小于平均薪资的记录 [^4]。 ### 用于复杂查询提高可读性 使用 `WITH AS` 可以将复杂的查询分解为多个简单的部分,提高查询的可读性和可维护性。示例代码如下: ```sql WITH department_avg_salary AS ( SELECT department_id, AVG(salary) AS avg_salary FROM employees GROUP BY department_id ) SELECT e.first_name, e.last_name, e.department_id, e.salary FROM employees e JOIN department_avg_salary d_avg ON e.department_id = d_avg.department_id WHERE e.salary > d_avg.avg_salary; ``` 这里先定义了 `department_avg_salary` CTE 计算每个部门的平均薪资,然后在主查询中筛选出薪资高于部门平均薪资的员工信息 [^5]。 ### 递归查询 CTE支持递归查询,适用于处理层次结构数据,如组织结构图、文件系统目录等。不过引用中未给出递归CTE的具体示例,以下是一个简单的递归CTE示例: ```sql WITH RECURSIVE cte_name AS ( -- 初始查询 SELECT 1 as num UNION ALL -- 递归部分 SELECT num + 1 FROM cte_name WHERE num < 10 ) SELECT * FROM cte_name; ``` 上述代码从 `1` 开始,每次递归将 `num` 加 `1`,直到 `num` 达到 `10` 为止。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值