ORACLE数据库除了可以保存永久表外还可以建立临时表temporary tables这些临时表用来保存一个会话SESSION的数据或者保存在一个事务中需要的数据当会话退出或者用户提交commit和回滚rollback事务的时候临时表的数据自动清空(truncate)但是临时表的结构以及元数据还存储在用户的数据字典中 简介 ORACLE数据库除了可以保存永久表外还可以建立临时表temporary tables这些临时表用来保存一个会话SESSION的数据或者保存在一个事务中需要的数据当会话退出或者用户提交commit和回滚rollback事务的时候临时表的数据自动清空(truncate)但是临时表的结构以及元数据还存储在用户的数据字典中 In addition to permanent tables Oracle can create temporary tables to hold sessionprivate data that exists only for the duration of a transaction or session Temporary tables are supported by Oraclei and Oraclei 详细介绍 Oracle临时表分为 会话级临时表 和 事务级临时表 会话级临时表是指临时表中的数据只在会话生命周期之中存在当用户退出会话结束的时候Oracle自动清除临时表中数据 事务级临时表是指临时表中的数据只在事务生命周期中存在当一个事务结束(commit or rollback)Oracle自动清除临时表中数据 临时表中的数据只对当前Session有效每个Session都有自己的临时数据并且不能访问其它Session的临时表中的数据因此临时表不需要DML锁 The CREATE GLOBAL TEMPORARY TABLE statement creates a temporary table that can be transactionspecific or sessionspecific For transactionspecific temporary tables data exists for the duration of the transaction For sessionspecific temporary tables data exists for the duration of the session Data in a temporary table is private to the session Each session can only see and modify its own data DML locks are not acquired on the data of the temporary tables The LOCK statement has no effect on a temporary table because each session has its own private data DML操作的临时表不产生redo log重作日志但会产生回滚日志Undo logUndo的产生(rollback segment)会产生Redo log DML statements on temporary tables do not generate redo logs for the data changes However undo logs for the data and redo logs for the undo logs are generated 当一个会话结束(用户正常退出 用户不正常退出 ORACLE实例崩溃)或者一个事务结束的时候Oracle对这个会话的表执行 TRUNCATE 语句清空临时表数据但不会清空其它会话临时表中的数据 A TRUNCATE statement issued on a sessionspecific temporary table truncates data in its own session It does not truncate the data of other sessions that are using the same table DML statements on temporary tables do not generate redo logs for the data changes However undo logs for the data and redo logs for the undo logs are generated Data from the temporary table is automatically dropped in the case of session termination either when the user logs off or when the session terminates abnormally such as during a session or instance failure 你可以索引临时表和在临时表基础上建立视图同样建立在临时表上的索引也是临时的也是只对当前会话或者事务有效 临时表可以拥有触发器 You can create indexes for temporary tables using the CREATE INDEX statement Indexes created on temporary tables are also temporary and the data in the index has the same session or transaction scope as the data in the temporary table You can create views that access both temporary and permanent tables You can also create triggers on temporary tables 空间分配Segment Allocation(v$sort_usage) Temporary tables use temporary segments Unlike permanent tables temporary tables and their indexes do not automatically allocate a segment when they are created Instead segments are allocated when the first INSERT (or CREATE TABLE AS SELECT) is performed This means that if a SELECT UPDATE or DELETE is performed before the first INSERT then the table appears to be empty Temporary segments are deallocated at the end of the transaction for transactionspecific temporary tables and at the end of the session for sessionspecific temporary tables 临时表在一些版本中存在BUG可能产生过多的REDO LOGeygle 的站点 建立临时表 临时表的定义对所有会话SESSION都是可见的但是表中的数据只对当前的会话或者事务有效 建立方法: ) ON COMMIT DELETE ROWS 定义了建立事务级临时表的方法 CREATE GLOBAL TEMPORARY TABLE admin_work_area (startdate DATE enddate DATE class CHAR()) ON COMMIT DELETE ROWS; EXAMPLE: SQL> CREATE GLOBAL TEMPORARY TABLE admin_work_area (startdate DATE enddate DATE class CHAR()) ON COMMIT DELETE ROWS; SQL> create table permernate( a number); SQL> insert into admin_work_area values(sysdatesysdatetemperary table); SQL> insert into permernate values(); SQL> commit; SQL> select * from admin_work_area; SQL> select* from permernate; A
)ON COMMIT PRESERVE ROWS 定义了创建会话级临时表的方法 CREATE GLOBAL TEMPORARY TABLE admin_work_area (startdate DATE enddate DATE class CHAR()) ON COMMIT PRESERVE ROWS; EXAMPLE: 会话: SQL> drop table admin_work_area; SQL> CREATE GLOBAL TEMPORARY TABLE admin_work_area (startdate DATE enddate DATE class CHAR()) ON COMMIT PRESERVE ROWS; SQL> insert into permernate values(); SQL> insert into admin_work_area values(sysdatesysdatesession temperary); SQL> commit; SQL> select * from permernate; A
SQL> select * from admin_work_area; STARTDATEENDDATECLASS ?? ?? session temperary 会话: SQL> select * from permernate; A
SQL> select * from admin_work_area; 未选择行 会话看不见会话中临时表的数据 temporary tables but they have to support a) read consistency b) rollback c) rollback to savepoint_P_DISPLAYIDF_P_CRITERIA: they definitely totally definitely generate UNDO consider: ops$tkyte@ORAIUTF> create global temporary table t ( x int ) on commit preserve rows; Table created ops$tkyte@ORAIUTF> insert into t values ( ); row created ops$tkyte@ORAIUTF> variable x refcursor ops$tkyte@ORAIUTF> exec open :x for select * from t; PL/SQL procedure successfully completed that is (as always) a read consitent result set it is preordained we havent fetched a single row of data yet NO IO HAS BEEN performed the result set is not copied somewhere just like any other query ops$tkyte@ORAIUTF> savepoint foo; Savepoint created ops$tkyte@ORAIUTF> insert into t values ( ); row created ops$tkyte@ORAIUTF> select * from t; X
that shows we can see two rows but ops$tkyte@ORAIUTF> print x X
that query used UNDO to rol |