正文
oracle代替临时表,oracle临时表local和global
小程序:扫一扫查出行
【扫一扫了解最新限行尾号】
复制小程序
【扫一扫了解最新限行尾号】
复制小程序
ORACLE临时表总结
临时表概念
临时表就是用来暂时保存临时数据(亦或叫中间数据)的一个数据库对象,它和普通表有些类似,然而又有很大区别。它只能存储在临时表空间,而非用户的表空间。ORACLE临时表是会话或事务级别的,只对当前会话或事务可见。每个会话只能查看和修改自己的数据。
临时表语法
clip_image002
临时表分类
ORACLE临时表有两种类型:会话级的临时表和事务级的临时表。
1)ON COMMIT DELETE ROWS
它是临时表的默认参数,表示临时表中的数据仅在事物过程(Transaction)中有效,当事物提交(COMMIT)后,临时表的暂时段将被自动截断(TRUNCATE),但是临时表的结构 以及元数据还存储在用户的数据字典中。如果临时表完成它的使命后,最好删除临时表,否则数据库会残留很多临时表的表结构和元数据。
2)ON COMMIT PRESERVE ROWS
它表示临时表的内容可以跨事物而存在,不过,当该会话结束时,临时表的暂时段将随着会话的结束而被丢弃,临时表中的数据自然也就随之丢弃。但是临时表的结构以及元数据还存储在用户的数据字典中。如果临时表完成它的使命后,最好删除临时表,否则数据库会残留很多临时表的表结构和元数据。
1:会话级的临时表的数据和你当前会话有关系,当前SESSION不退出的情况下,临时表中的数据就还存在,临时表的数据只有当你退出当前SESSION的时候才被截断(TRUNCATE TABLE),如下所示:
会话级别的临时表创建:
复制代码
CREATE GLOBAL TEMPORARY TABLE TMP_TEST
(
ID NUMBER ,
NAME VARCHAR2(32)
) ON COMMIT PRESERVE ROWS;
或
CREATE GLOBAL TEMPORARY TABLE TMP_TEST ON COMMIT PRESERVE ROWS
AS
SELECT * FROM TEST;
操作示例:
SQL CREATE GLOBAL TEMPORARY TABLE TMP_TEST
(
ID NUMBER ,
NAME VARCHAR2(32)
) ON COMMIT PRESERVE ROWS;
Table created
SQL INSERT INTO TMP_TEST
SELECT 1, 'kerry' FROM DUAL;
1 row inserted
SQL COMMIT;
Commit complete
SQL SELECT * FROM TMP_TEST;
ID NAME
---------- ----------------
1 kerry
SQL INSERT INTO TMP_TEST
SELECT 2, 'rouce' FROM DUAL;
1 row inserted
SQL ROLLBACK;
Rollback complete
SQL SELECT * FROM TMP_TEST;
ID NAME
---------- ----------------------
1 kerry
SQL
复制代码
2:事务级的临时表(默认),这种类型的临时表与事务有关,当进行事务提交或者事务回滚的时候,临时表的数据将自行截断,即当COMMIT或ROLLBACK时,数据就会被TRUNCATE掉,其它的特性和会话级的临时表一致。
事务级临时表的创建方法:
复制代码
CREATE GLOBAL TEMPORARY TABLE TMP_TEST
(
ID NUMBER ,
NAME VARCHAR2(32)
) ON COMMIT DELETE ROWS;
或
CREATE GLOBAL TEMPORARY TABLE TMP_TEST ON COMMIT DELETE AS SELECT * FROM TEST;
SQL CREATE GLOBAL TEMPORARY TABLE TMP_TEST
(
ID NUMBER ,
NAME VARCHAR2(32)
) ON COMMIT DELETE ROWS;
Table created
SQL INSERT INTO TMP_TEST
SELECT 1, 'kerry' FROM DUAL;
1 row inserted
SQL SELECT * FROM TMP_TEST;
ID NAME
---------- ----------------------
1 kerry
SQL COMMIT;
Commit complete
SQL SELECT * FROM TMP_TEST;
ID NAME
---------- ------------------------
SQL
复制代码
3:关于临时表只对当前会话或事务可见。每个会话只能查看和修改自己的数据。
用DM用户登录数据库,打开SESSION 1后,创建临时表TMP_TEST
复制代码
CREATE GLOBAL TEMPORARY TABLE TMP_TEST
(
ID NUMBER ,
NAME VARCHAR2(32)
) ON COMMIT DELETE ROWS;
或
CREATE GLOBAL TEMPORARY TABLE TMP_TEST ON COMMIT DELETE AS SELECT * FROM TEST;
SQL CREATE GLOBAL TEMPORARY TABLE TMP_TEST
(
ID NUMBER ,
NAME VARCHAR2(32)
) ON COMMIT DELETE ROWS;
Table created
SQL INSERT INTO TMP_TEST
SELECT 1, 'kerry' FROM DUAL;
1 row inserted
SQL SELECT * FROM TMP_TEST;
ID NAME
---------- ---------------------
1 kerry
SQL COMMIT;
Commit complete
SQL SELECT * FROM TMP_TEST;
ID NAME
---------- -----------------------
SQL
复制代码
用sys用户登录数据库,打开SESSION 2
SELECT * FROM DBA_TABLES WHERE TABLE_NAME='TMP_TEST' --可以查到临时表数据
SELECT * FROM DM.TMP_TEST; --查不到数据,即使TMP_TEST临时表存在数据。
临时表与永久表区别
复制代码
SQL SELECT TABLE_NAME, TABLESPACE_NAME,"LOGGING",
"TEMPORARY", DURATION, "MONITORING"
FROM DBA_TABLES WHERE TABLE_NAME IN ('TMP_TEST', 'TEST') ;
TABLE_NAME TABLESPACE_NAME LOGGING TEMPORARY DURATION MONITORING
------------- -------------- ------- --------- ----------- ---------
TEST TBS_EDS_DATA YES N YES
TMP_TEST NO Y SYS$SESSION NO
复制代码
如上所示,临时表是存储在临时表空间里面的,但是上面脚本可以看出,临时表在数据字典中没有指定其表空间,临时表是NOLOGGING,DURATION为SYS$SESSION
临时表的DML操作速度比较快,但同样也是要产生 Redo Log ,只是同样的DML语句,比对 PERMANENT 的DML 产生的Redo Log 少其实在应用中,往往会创建一个NOLOGGING的永久表(中间表)来保存中间数据,从而代替临时表,至于这这两者有啥优劣,真是很难说清道明(欢迎大家探讨)。
临时表用途
什么时候使用临时表?用临时表和用中间表有啥区别呢?
我觉得是在需要的时候应用,下面是David Dai关于临时表的一个应用说明,我觉得非常形象的说明了临时表的应用场景:对于一个电子商务类网站,不同消费者在网站上购物,就是一个独立的 SESSION,选购商品放进购物车中,最后将购物车中的商品进行结算。也就是说,必须在整个SESSION期间保存购物车中的信息。同时,还存在有些消费者,往往最终结账时放弃购买商品。如果,直接将消费者选购信息存放在最终表(PERMANENT)中,必然对最终表造成非常大的压力。因此,对于这种案例,就可以采用创建临时表(ON COMMIT PRESERVE ROWS)的方法来解决。数据只在 SESSION 期间有效,对于结算成功的有效数据,转移到最终表中后,ORACLE自动TRUNCATE 临时数据;对于放弃结算的数据,ORACLE 同样自动进行 TRUNCATE ,而无须编码控制,并且最终表只处理有效订单,减轻了频繁的DML操作的压力。
1:当处理某一批临时数据,需要多次DML操作时(插入、更新等),建议使用临时表。
2:当某些表在查询里面,需要多次用来做连接时。(为了获取目标数据需要关联A、B、C, 同时为了获取另外一个目标数据,需要关联D、B、C....)
关于临时表和中间表(NOLOGGING,保存中间数据,使用完后删除)那个更适合用来存储中间数据,我个人更倾向于使用临时表,而不建议使用中间表。
注意事项
1 ) 不支持 lob 对象,这也许是设计者基于运行效率的考虑,但实际应用中确实需要此功能时就无法使用临时表了。这点网上很多资料都这么说,我没有追查到底是那个版本不支持lob对象,至少在ORACLE 10g这个版本中,临时表是支持lob对象的.
SQL CREATE GLOBAL TEMPORARY TABLE TMP_TEST
2 (
3 ID NUMBER ,
4 NAME CLOB
5 ) ON COMMIT PRESERVE ROWS;
Table created
SQL
SQL INSERT INTO TMP_TEST
2 SELECT 1, 'ADF' FROM DUAL;
1 row inserted
SQL SELECT * FROM V$VERSION;
BANNER
----------------------------------------------------------------
Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Prod
PL/SQL Release 10.2.0.1.0 - Production
CORE 10.2.0.1.0 Production
TNS for 32-bit Windows: Version 10.2.0.1.0 - Production
NLSRTL Version 10.2.0.1.0 - Production
2 ) 不支持主外键关系
3 )临时表不能永久的保存数据。
4 )临时表的数据不会备份,恢复,对其的修改也不会有任何日志信息
5 )临时表不会有DML 锁
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.
6 )尽管对临时表的DML操作速度比较快,但同样也是要产生 Redo Log ,只是同样的DML语句,比对 PERMANENT 的DML 产生的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. 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.
7 ) 临时表可以创建临时的索引、视图、触发器。
8 ) 如果要DROP会话级别临时表,并且其中包含数据时,必须先截断其中的数据。否则会报错。
SQL DROP TABLE TMP_TEST PURGE;
DROP TABLE TMP_TEST PURGE
ORA-14452: 试图创建, 更改或删除正在使用的临时表中的索引
SQL TRUNCATE TABLE TMP_TEST;
Table truncated
SQL DROP TABLE TMP_TEST PURGE;
Table dropped
ORACLE有没有类似SQL SERVER中的临时表
对比Oracle临时表和SQL Server临时表的不同点
1创建临时表不需要往编目表中插入条目,临时表的使用也不需要访问编目表,因此也没有对编目表的争用。
2仅有创建临时表的app才可存取临时表,所以在处理临时表时没有锁。
3如果指定NOT LOGGED选项,在处理临时表时不记日志。所以如果有仅在数据库的一个会话中使用的大量临时数据,把这些数据存入临时表能大大提高性能。
Oracle中的临时表用法汇总
语法 在Oracle中 可以创建以下两种临时表 ) 会话特有的临时表 CREATE GLOBAL TEMPORARY( ) ON MIT PRESERVE ROWS ) 事务特有的临时表 CREATE GLOBAL TEMPORARY( ) ON MIT DELETE ROWS CREATE GLOBAL TEMPORARY TABLE MyTempTable 所建的临时表虽然是存在的 但是如果insert 一条记录然后用别的连接登上去select 记录是空的 ON MIT DELETE ROWS 说明临时表是事务指定 每次提交后ORACLE将截断表(删除全部行) ON MIT PRESERVE ROWS 说明临时表是会话指定 当中断会话时ORACLE将截断表 动态创建 create or replace procedure pro_temp(v_col varchar v_col varchar ) as v_num number; begin select count(*) into v_num from user_tables where table_name= T_TEMP ; create temporary table if v_num then execute immediate CREATE GLOBAL TEMPORARY TABLE T_TEMP ( COL VARCHAR ( ) COL VARCHAR ( ) ) ON MIT delete ROWS ; end if; insert data execute immediate insert into t_temp values( v_col v_col ) ; execute immediate select col from t_temp into v_num; dbms_output put_line(v_num); execute immediate delete from t_temp ; mit; execute immediate drop table t_temp ; end pro_temp; 测试: : : SQL set serveroutput on : : SQL exec pro_temp( ); PL/SQL 过程已成功完成 已用时间: : : : : SQL desc t_temp; ERROR ORA : 对象 t_temp 不存在 通过查询创建临时表 create proecdure name_pro as str varchar ( ); begin str:= CREATE GLOBAL TEMPORARY TABLE TABLENAME ON MIT PRESERVE ROWS as select * from other_tables ; execute immediate str; end; 特性和性能(与普通表和视图的比较) 临时表只在当前连接内有效 临时表不建立索引 所以如果数据量比较大或进行多次查询时 不推荐使用 数据处理比较复杂的时候时表快 反之视图快点 在仅仅查询数据的时候建议用游标: open cursor for sql clause ; lishixinzhi/Article/program/Oracle/201311/18537
oracle有什么方法能够代替临时表
已经用oracle自定义函数中的管道函数模拟返回值为表的形式出来了。
1、自定义类型
2、用管道函数处理
查询时也是直接利用sql的查询语句,表名替换为函数名(函数名需要放在table关键字里面),select * from table(函数名)。
oracle临时表有几种
Oracle 临时表:在 Oracle 中创建一张表,这个表不用于其他的什么功能,主要用于自己的软件系统一些特有功能才用的,而当你用完之后表中的数据就没用了。 Oracle 的临时表创建之后基本不占用表空间,如果你没有指定临时表(包括临时表的索引)存放的表空的时候,你插入到临时表的数据是存放在 ORACLE 系统的临时表空间中( TEMP )。
1 、临时表的创建
创建Oracle 临时表,可以有两种类型的临时表:
会话级的临时表
事务级的临时表 。
1) 会话级的临时表因为这这个临时表中的数据和你的当前会话有关系,当你当前SESSION 不退出的情况下,临时表中的数据就还存在,而当你退出当前SESSION 的时候,临时表中的数据就全部没有了,当然这个时候你如果以另外一个SESSION 登陆的时候是看不到另外一个SESSION 中插入到临时表中的数据的。即两个不同的SESSION 所插入的数据是互不相干的。当某一个SESSION 退出之后临时表中的数据就被截断(truncate table ,即数据清空)了。会话级的临时表创建方法:
Create Global Temporary Table Table_Name (Col1 Type1,Col2 Type2...) On Commit Preserve Rows ;
举例:
create global temporary table Student (
Stu_id Number(5),
Class_id Number(5),
Stu_Name Varchar2(8),
Stu_Memo varchar2(200)) on Commit Preserve Rows ;
2) 事务级临时表是指该临时表与事务相关,当进行事务提交或者事务回滚的时候,临时表中的数据将自行被截断,其他的内容和会话级的临时表的一致(包括退出SESSION 的时候,事务级的临时表也会被自动截断)。事务级临时表的创建方法:
Create Global Temporary Table Table_Name (Col1 Type1,Col2 Type2...) On Commit Delete Rows ;
举例:
create global temporary table Classes (
Class_id Number(5),
Class_Name Varchar2(8),
Class_Memo varchar2(200)) on Commit delete Rows ;
3) 两中类型临时表的区别
会话级临时表采用 on commit preserve rows ;而事务级则采用 on commit delete rows ;用法上,会话级别只有当会话结束临时表中的数据才会被截断,而且事务级临时表则不管是 commit 、 rollback 或者是会话结束,临时表中的数据都将被截断
4 )什么时候使用临时表
1 )、当某一个 SQL 语句关联的表在 2 张及以上,并且和一些小表关联。可以采用将大表进行分拆并且得到比较小的结果集合存放在临时表中
2 )、程序执行过程中可能需要存放一些临时的数据,这些数据在整个程序的会话过程中都需要用的等等。
2 .临时表的不足之处
1 )不支持 lob 对象,这也许是设计者基于运行效率的考虑,但实际应用中确实需要此功能时就无法使用临时表了。
2 )不支持主外键关系。
在oracle中普通表和临时表的区别
临时表是只有创建的用户才可以使用的,用户退出或事务结束时(具体看on commit 选项)自动删除该表,也可以手动drop。
在临时表上的操作比在一般的表上的操作要快。
建临时表是动态编译的,所以对临时表的使用也必须放在DECLARE CURSER 后面。
对普通表的DML,建触发器等操作同样适合临时表,与普通表不同的是,临时表使用的是PGA,如果PGA空间不够了才会使用临时表空间。
【延展】
Oracle中临时表的使用:
我们在创建数据表的时候,若没有特殊的指明,那么我们创建的表是一个永久的关系型表格,也就是说,这个表格中对应的数据,除非是我们显示的删除的话,表中的数据是永远都存在的。相对应的,在Oracle数据库中还有一种类型的表,叫做临时表。这个临时表跟永久表最大的区别就是表中的数据不会永远的存在。当一个会话结束或者事务结束的时候,这个临时表中的数据,不用用户自己删除,数据库自己会自动清除。
关于oracle代替临时表和oracle临时表local和global的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。