ORACLE数据库除了可以保存永久表外,还可以建立临时表temporary tables。这些临时表用来保存一个会话SESSION的数据,或者保存在一个事务中需要的数据。当会话退出或者用户提交commit和回滚rollback事务的时候,临时表的数据自动清空,但是临时表的结构以及元数据还存储在用户的数据字典中。

1. 会话级临时表

会话级临时表是指临时表中的数据只在会话生命周期之中存在,当用户退出会话结束的时候,Oracle自动清除临时表中数据。

Create Global Temporary Table Table_Name
(
Col1 Type1,
Col2 Type2
...
)
On Commit Preserve Rows;

2.事务级临时表

事务级临时表是指临时表中的数据只在事务生命周期中存在。

Create Global Temporary Table Table_Name
(
Col1 Type1,
Col2 Type2
...
)
On Commit Delete Rows;

3. With Clause

with as在查询的时候建立临时表,数据是写入了内存中,在处理逻辑复杂且数据量不是很大的业务时,我们可以采取的这样的方法来提高sql的性能,并且也降低sql的复杂性,让逻辑更加的清晰,方便维护。
增加了SQL的易读性,如果构造了多个子查询,结构会更清晰;更重要的是:“一次分析,多次使用”,这也是为什么会提供性能的地方,达到了“少读”的目标。

如果WITH AS短语所定义的表名被调用两次以上,则优化器会自动将WITH AS短语所获取的数据放入一个TEMP表里,如果只是被调用一次,则不会。而提示materialize则是强制将WITH AS短语里的数据放入一个
全局临时表里。很多查询通过这种方法都可以提高速度。

with as语法
–针对一个别名
with tmp as (select * from tb_name)–针对多个别名
withtmp as (select * from tb_name),tmp2 as (select * from tb_name2),tmp3 as (select * from tb_name3),.....
hint关键字 描述
materialize 强制将 with as 中的子查询结果集转换为临时表
inline 强制不将 with as 中的子查询结果集转换为临时表

应用场景
用 with as 将一个子查询独立出来:一般来说,如果这个子查询定义的表名被调用2次及2次以上,那么CBO就会把这个子查询返回的数据放入临时表(对应执行计划中的 SYS_TEMP_XXX);如果只被调用1次,则不会。

一般情况下,如果在 with as 中的子查询返回结果集很大,且调用该子查询不走索引时,加 materialize 能极大提升整体性能;如果在 with as 中的子查询返回结果集很小,且调用该子查询能走索引时,加 materialize 反而会降低整体性能,这种情况下,为稳定性能,可使用 inline 抑制其转换为临时表。

格式

with tmp as (select /*+ materialize */ * from tb_name)或者with tmp as (select /*+ inline*/ * from tb_name)

参考资料

下面是搜索到的英文文档资料

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”:

oracle的临时表和With As总结相关推荐

  1. php oracle创建临时表,Oracle常用命令笔记

    Oracle常用命令笔记 客户端用的pl/sql工具 ORACLE的重启命令 (1) 以系统管理员登录,命令:connect / as sysdba (2) 启动数据库,命令:startup (3) ...

  2. oracle缩减临时表空间,oracle的临时表空间写满磁盘空间解决改问题的步骤

    oracle的临时表空间写满磁盘空间,解决改问题的具体步骤,以下的操作是用数据库的sys超级用户操作 刚开始打算把临时表空间的数据文件重新缩小就好了 执行: SQL> alter databas ...

  3. oracle临时表空间大小查询,查看oracle 系统临时表空间、undo表空间、SGA和PGA大小...

    1)检查oracle系统临时表空间大小: select sum(bytes)/1024/1024 "temp size(M)" from dba_temp_files where ...

  4. java oracle临时表,JdbcTemplate操作oracle的临时表

    使用spring 的 JdbcTemplate 进行sql 的操作, 对于每一次的如:query, update, batchUpdate,execute 这些方法, 执行一次都是调用不同的 Conn ...

  5. oracle 临时表空间语句,oracle的临时表空间

    经常看到有人说看到temporary tablespace空间不释放等等的问题,整理一篇metalink上的相关文章,入门级的,还算浅显吧.基本上就是翻译了. sort之后临时段不回收 描述 当你监控 ...

  6. oracle的临时表空间问题

    当你监控temporary tablespace 的空闲空间,发现空闲空间并没有随着sort的结束而增加,甚至当前数据库没有任何客户端连接也是如此:仍然有大量的temporary类型的extent存在 ...

  7. Oracle Temp临时表空间及其故障处理

    Oracle Temp临时表空间及其故障处理 Oracle 11g中Temp临时表空间.文件的新特性 临时表空间是Oracle体系结构中比较特殊的结构.通常情境下,数据库使用者只需要设置对应的临时表空 ...

  8. Oracle全局临时表

    Oracle全局临时表 目前所有使用Oracle作为数据库支撑平台的应用,大部分是数据量比较庞大的系统,即表的数据量级一般情况下都是在百万级以上.当然,在Oracle中创建分区是一种不错的选择,但是当 ...

  9. oracle如何查询临时表空间,Oracle查询临时表空间的占用

    可以使用以下语句查询是哪个session number的哪个sql占用了较大的临时表空间 select inst_id,username,session_num,sql_id,tablespace,s ...

  10. oracle查看临时表空间文件,Oracle-临时表空间

    一:临时表空间查询 --查看数据库表空间数据文件 select * from dba_data_files; --查看数据库临时表空间文件 select * from dba_temp_files; ...

最新文章

  1. Android中的PopupWindow详解
  2. Spring-Retry重试实现原理
  3. shell的输入和输出
  4. 第二章 OpenResty(Nginx+Lua)开发入门
  5. PHP访问连接MYSQL数据库
  6. 火狐怎么放大页面?火狐浏览器页面放大技巧
  7. 【数据结构和算法笔记】c语言实现顺序表和链表
  8. python下读sougou中文语料文件
  9. vue上传图片文件到服务器,vue如何将quill图片上传到服务器
  10. mysql导入存储过程报错_mysql导入存储过程时declare报错的有关问题解决
  11. 怎么把cad的图导入ps_CAD图纸快速高效导入PS的方法
  12. python爬虫遇到动态加密怎么办?爬取某点评网站内容
  13. Mysql同环比计算详解
  14. Kernel:CC_HAVE_ASM_GOTO 、 Compiler lacks asm-goto support
  15. VS C++控制台程序 错误 fatal error C1083: 无法打开包括文件 解决办法
  16. 萝卜小铺和店主们的故事(五)
  17. oracle异地双活距离,再谈异地双活容灾部署(6.24)
  18. el轮播图片image单张图比例缩放及多张图宽度固定高度等比例自适应展示;
  19. 使用Endnotes生成知网参考文献的Latex引文格式(BibTex)
  20. OCJP 考试题之七

热门文章

  1. 9th, Jan 2012 养成好的生活习惯真的很不容易
  2. asp.net弹出alert提示框
  3. 转:金牌网管师初级网络实验手册
  4. 【WPF】如何保存RichTextBox的文本到数据库?以及如何对RichTextBox的Document做绑定?...
  5. 无线网络MIMO技术浅谈
  6. TypeScript入门教程 之 const
  7. 什么是DataV数据可视化?
  8. Vue服务端配置示例
  9. DreamFactory 第7章 限制和记录API请求
  10. java调用jndi出错,Webshpere数据源错误:无法查找JNDI名称