Oracle 临时事务表 全局临时表_global temporary table
所有的操作都在一个事务里,事务提交后,此表清空,特别适合做插入删除频率特别高的临时表操作,比如插入完数据就开始查询,查询完就删掉等,用完就扔!
临时表分事务级临时表和会话级临时表。
事务级临时表只对当前事务有效,通过语句:ON COMMIT DELETE ROWS 指定。
会话级临时表对当前会话有效,通过语句:ON COMMIT PRESERVE ROWS语句指定。
-- Create table
create global temporary table WFM_TMP_WORKLIST
(
proc_inst_id NUMBER(10),
workitem_id NUMBER(10),
buzicondi NVARCHAR2(500)
)
on commit delete rows;
---全局临时表创建语法
SQL> create global temporary table t_global_temp(a int)
2 on commit delete rows;
Table created.
---查询表名
SQL> select table_name from user_tables where table_name='T_GLOBAL_TEMP';
TABLE_NAME
------------------------------------------------------------
T_GLOBAL_TEMP
--查询表对应的segment
SQL> select segment_name,segment_type from user_segments where segment_name='T_G
LOBAL_TEMP';
no rows selected
---插入数据
SQL> insert into t_global_temp values(1);
1 row created.
SQL> commit;
Commit complete.
--提交查询无记录
SQL> select * from t_global_temp;
no rows selected
--再次查询segment无记录,原因:创建全局临时表指定on commit delete rows一提交即清表
SQL> select segment_name,segment_type from user_segments where segment_name='T_G
LOBAL_TEMP';
no rows selected
SQL> insert into t_global_temp values(1);
1 row created.
--插入不提交即可查询到记录
SQL> select segment_name,segment_type from user_segments where segment_name='T_G
LOBAL_TEMP';
no rows selected
--提交与否皆不占用存储空间,引申问题:哪全局临时表的数据存储在哪儿呢?
SQL> select segment_name,segment_type from user_segments where segment_name='T_G
LOBAL_TEMP';
no rows selected
---以基于会话方式创建全局临时表
SQL> create global temporary table t_global_temp(a int) on commit preserve rows;
Table created.
SQL> insert into t_global_temp values(1);
1 row created.
---提交前查询
SQL> select * from t_global_temp;
A
----------
1
SQL> select segment_name,segment_type from user_segments where segment_name='T_G
LOBAL_TEMP';
no rows selected
SQL> commit;
Commit complete.
--提交后查询
SQL> select * from t_global_temp;
A
----------
1
---附上提交前后在另一会话查全局临时表测试,全局临时表的数据仅在当前会话可见
SQL> select * from t_global_temp;
no rows selected
SQL> /
no rows selected
SQL> desc t_global_temp;
Name Null? Type
----------------------------------------- -------- -----------------------
A NUMBER(38)
---测试全局临时表的alter table及create index及alter index
--如全局临时表正在使用alter table不能运行
SQL> alter table t_global_temp add b int;
alter table t_global_temp add b int
*
ERROR at line 1:
ORA-14450: attempt to access a transactional temp table already in use
---只有退出当前会话
SQL> exit
Disconnected from Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64
bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
C:\Users\123>sqlplus scott/system
SQL*Plus: Release 11.2.0.1.0 Production on Wed Jan 9 16:07:10 2013
Copyright (c) 1982, 2010, Oracle. All rights reserved.
Connected to:
Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
---再次alter table即可成功
SQL> alter table t_global_temp add b int;
Table altered.
---在全局临时表构建索引
SQL> create index idx_temp on t_global_temp(a);
Index created.
--删除全局临时表索引
SQL> drop index idx_temp;
Index dropped.
SQL> select count(*) from t_global_temp;
COUNT(*)
----------
0
SQL> insert into t_global_temp select 1,3 from dual connect by level<3e5;
299999 rows created.
SQL> commit;
Commit complete.
---收集全局临时表的统计信息
SQL> exec dbms_stats.gather_table_stats(user,'t_global_temp');
PL/SQL procedure successfully completed.
SQL> select * from table(dbms_xplan.display);
PLAN_TABLE_OUTPUT
-----------------------------------------------------------------------------
Plan hash value: 62698482
----------------------------------------------------------------------------
| Id | Operation | Name | Rows | Cost (%CPU)| Time |
----------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 257 (4)| 00:00:04 |
| 1 | SORT AGGREGATE | | 1 | | |
| 2 | TABLE ACCESS FULL| T_GLOBAL_TEMP | 599K| 257 (4)| 00:00:04 |
----------------------------------------------------------------------------
9 rows selected.
---仅插一条a值888888888888888888888的记录到全局临时表
SQL> insert into t_global_temp select 888888888888888888888,1 from dual;
1 row created.
SQL> commit;
Commit complete.
SQL> explain plan for select count(*) from t_global_temp where a=88888888888888
888888;
Explained.
--执行计划显示走了索引
SQL> select * from table(dbms_xplan.display);
PLAN_TABLE_OUTPUT
-------------------------------------------------------------------------------
Plan hash value: 1743356947
------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 3 | 3 (0)| 00:00:01 |
| 1 | SORT AGGREGATE | | 1 | 3 | | |
|* 2 | INDEX RANGE SCAN| IDX_TEMP | 1 | 3 | 3 (0)| 00:00:01 |
------------------------------------------------------------------------------
Predicate Information (identified by operation id):
PLAN_TABLE_OUTPUT
-------------------------------------------------------------------------------
---------------------------------------------------
2 - access("A"=888888888888888888888)
14 rows selected.
----全局临时表的操作限制
----不能分区,不能集簇化,不能iot化
Temporary tables cannot be partitioned, clustered, or index organized.
---不能指定外键约束
You cannot specify any foreign key constraints on temporary tables.
---不能包含nested table column
Temporary tables cannot contain columns of nested table.
----不能指定lob_storage_clause的参数:tablespace,storage_clause or logging_clause
You cannot specify the following clauses of the LOB_storage_clause: TABLESPACE, storage_clause, or logging_clause.
---不能启用并行update,delte,merge
Parallel UPDATE, DELETE and MERGE are not supported for temporary tables.
---在segment_atrributes_clause子句中,唯一可指定的参数是:tablespace
The only part of the segment_attributes_clause you can specify for a temporary table is TABLESPACE, which allows you to specify a single temporary tablespace.
---不支持分布式事务
Distributed transactions are not supported for temporary tables.
小结:全局临时表特别适用于存储中转结果,即临时计算的结果,非最终结果;
可用于报表统计存储过程.
Oracle 临时事务表 全局临时表_global temporary table的更多相关文章
- Postgresql中临时表(temporary table)的特性和用法
熟悉Oracle的人,相比对临时表(temporary table)并不陌生,很多场景对解决问题起到不错的作用,开源库Postgresql中,也有临时表的概念,虽然和Oracle中临时表名字相同,使用 ...
- Oracle临时表GLOBAL TEMPORARY TABLE
临时表:像普通表一样,有结构,但是对数据的管理上不一样,临时表存储事务或会话的中间结果集,临时表中保存的数据只对当前 会话可见,所有会话都看不到其他会话的数据,即使其他会话提交了,也看不到.临时表不存 ...
- Oracle临时表(Temporary Table)
GLOBAL TEMPORARY代表全局临时表临时表的元数据存储在数据字典里面 只当第一条DML命令发生的时候才为这张表的段分配空间 临时表数据的可见范围应该是会话级别或是事务级别的 会话或者事务级别 ...
- mysql 连接命令 表管理 ,克隆表,临时表,字符串属性,设定语句间的分隔符
连接和断开连接mysql -h host -u user -p (即,连接的主机.用户名和使用的密码).断开输入QUIT (或\q)随时退出: 表管理克隆表注意:create table ... li ...
- mysql优化: 内存表和临时表
由于直接使用临时表来创建中间表,其速度不如人意,因而就有了把临时表建成内存表的想法.但内存表和临时表的区别且并不熟悉,需要查找资料了.一开始以为临时表是创建后存在,当连接断开时临时表就会被删除,即临时 ...
- Hive ACID和事务表支持详解
一.ACID介绍 ACID就是常见数据库事务的四大特性:Atomicity(原子性).Consistency(一致性).Isolation(隔离性).Durability(持久性). 在Hive 0. ...
- 深入浅出Oracle数据读取一致性和事务表
保证Oracle数据库读取一致性的关键是SCN.每一个数据块头都会记录一个事务提交的SCN.同时每一数据块头都包含一个事务表(ITL),事务必须获得一个ITL事务表才能进行数据修改.该事务表用来确定当 ...
- Oracle错误览表
Oracle 错误总结及问题解决 ORA 本文转自:https://www.cnblogs.com/zhangwei595806165/p/4972016.html 作者@承影剑 ORA-0 ...
- oracle之二表的几种类型
Oracle中表的几种类型 1.表的功能:存储.管理数据的基本单元(二维表:有行和列组成)2.表的类型: 1)堆表:heap table :数据存储时,行是无序的,对它的访问采用全表扫描. 2)分区表 ...
随机推荐
- (转)为首次部署MongoDB做好准备:容量计划和监控
如果你已经完成了自己新的MongoDB应用程序的开发,并且现在正准备将它部署进产品中,那么你和你的运营团队需要讨论一些关键的问题: 最佳部署实践是什么? 为了确保应用程序满足它所必须的服务层次我们需要 ...
- mongodb gdal 矢量数据格式驱动
写了个mongodb的gdal driver,放在了github上,如果你需要,欢迎加入mongogis group. 直接的效果是使得QGIS, GeoServer, MapServer, ArcG ...
- 好书推荐:《Game Programming Patterns》
在线阅读点这里: http://gameprogrammingpatterns.com/contents.html 这是一个总结讨论和反思游戏客户端game play开发常用设计模式的书. 游戏开发和 ...
- [windows phone开发]新生助手的开发过程与体会一
功能需求分析: 1. 为到达学院的新生指路,给出所有路线,并给出必要提示: 2. 对学院建筑进行介绍: 3. 对学院周边环境(交通.购物.银行等)进行介绍: 4. 必要的应用设置 总体设计: ...
- 《Apache服务之php/perl/cgi语言的支持》RHEL6——服务的优先级
安装php软件包: 安装文本浏览器 安装apache的帮助文档: 测试下是否ok 启动Apache服务关闭火墙: 编辑一个php测试页测试下: perl语言包默认系统已经安装了,直接测试下: Apac ...
- Cadence Allegro小技巧-从外部文本文件添加文本
菜单“Add->Text”,然后在右侧Options栏设置好合适的Class and Subclass,Text block,然后在布板界面上点击鼠标左键,设置起始点,接着点击鼠标右键,在弹出的 ...
- Android Audio Play Out Channel
1: 7嘴8舌 扬声器, 耳机, 和听筒 就是通过: audiomanager.setmode(AudioManager.MODE_IN_COMMUNICATION)audiomanager.setS ...
- MongoDB与php的配合使用 【windows版】
通过学习了如何使用和部署MongoDB,尝试了一下如何将mongodb应用到php的程式中去. 1.预备工作 首先得准备好mongodb,并按照相关方法部署以及服务能正常运行中. 对于初学者,可以参考 ...
- cookie工作原理
当客户访问某个基于PHP技术的网站时,在PHP中可以使用setcookie()函数生成一个cookie,系统经处理把这个cookie发送到客户端并保存在C:\Documents andSettings ...
- PAT IO-04 混合类型数据格式化输入(5)
/* *PAT IO-04 混合类型数据格式化输入(5) *2015-08-01 作者:flx413 */ #include<stdio.h> int main() { int a; fl ...