Oracle临时表使用注意事项

此文将给出在使用Oracle临时表的过程中需要注意的事项,并对这些特点进行验证。
①临时表不支持物化视图
②可以在临时表上创建索引
③可以基于临时表创建视图
④临时表结构可被导出,但内容不可以被导出
⑤临时表通常是创建在用户的临时表空间中的,不同用户可以有自己的独立的临时表空间
⑥不同的session不可以互相访问对方的临时表数据
⑦临时表数据将不会上DML(Data Manipulation Language)锁

1.临时表不支持物化视图
1)环境准备
(1)创建基于会话的临时表

sec@ora10g> create global temporary table t_temp_session (x int) on commit preserve rows;

Table created.

sec@ora10g> col TABLE_NAME for a30
sec@ora10g> col TEMPORARY for a10
sec@ora10g> select TABLE_NAME,TEMPORARY from user_tables where table_name = 'T_TEMP_SESSION';

TABLE_NAME                     TEMPORARY
------------------------------ ----------
T_TEMP_SESSION                 Y

(2)初始化两条数据

sec@ora10g> insert into t_temp_session values (1);

1 row created.

sec@ora10g> insert into t_temp_session values (2);

1 row created.

sec@ora10g> commit;

Commit complete.

sec@ora10g> select * from t_temp_session;

         X
----------
         1
         2

(3)在临时表T_TEMP_SESSION上添加主键

sec@ora10g> alter table T_TEMP_SESSION add constraint PK_T_TEMP_SESSION primary key(x);

Table altered.

2)在临时表T_TEMP_SESSION上创建物化视图
(1)创建物化视图日志日志

sec@ora10g> create materialized view log on T_TEMP_SESSION with sequence, rowid (x) including new values;
create materialized view log on T_TEMP_SESSION with sequence, rowid (x) including new values
*
ERROR at line 1:
ORA-14451: unsupported feature with temporary table

可见,在创建物化视图时便提示,临时表上无法创建物化视图日志。

(2)创建物化视图

sec@ora10g> create materialized view mv_T_TEMP_SESSION build immediate refresh fast on commit enable query rewrite as select * from T_TEMP_SESSION;
create materialized view mv_T_TEMP_SESSION build immediate refresh fast on commit enable query rewrite as select * from T_TEMP_SESSION
                                                                                                                        *
ERROR at line 1:
ORA-23413: table "SEC"."T_TEMP_SESSION" does not have a materialized view log

由于物化视图日志没有创建成功,因此显然物化视图亦无法创建。

2.在临时表上创建索引

sec@ora10g> create index i_t_temp_session on t_temp_session (x);

Index created.

临时表上索引创建成功。

3.基于临时表创建视图

sec@ora10g> create view v_t_temp_session as select * from t_temp_session where x<100;

View created.

基于临时表的视图创建成功。

4.临时表结构可被导出,但内容不可以被导出
1)使用exp工具备份临时表

ora10g@secdb /home/oracle$ exp sec/sec file=t_temp_session.dmp log=t_temp_session.log tables=t_temp_session

Export: Release 10.2.0.1.0 - Production on Wed Jun 29 22:06:43 2011

Copyright (c) 1982, 2005, Oracle.  All rights reserved.


Connected to: Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Production
With the Partitioning, OLAP and Data Mining options
Export done in WE8ISO8859P1 character set and AL16UTF16 NCHAR character set

About to export specified tables via Conventional Path ...
. . exporting table                 T_TEMP_SESSION
Export terminated successfully without warnings.

可见在备份过程中,没有显示有数据被导出。

2)使用imp工具的show选项查看备份介质中的SQL内容

ora10g@secdb /home/oracle$ imp sec/sec file=t_temp_session.dmp full=y show=y

Import: Release 10.2.0.1.0 - Production on Wed Jun 29 22:06:57 2011

Copyright (c) 1982, 2005, Oracle.  All rights reserved.


Connected to: Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Production
With the Partitioning, OLAP and Data Mining options

Export file created by EXPORT:V10.02.01 via conventional path
import done in WE8ISO8859P1 character set and AL16UTF16 NCHAR character set
. importing SEC's objects into SEC
. importing SEC's objects into SEC
 "CREATE GLOBAL TEMPORARY TABLE "T_TEMP_SESSION" ("X" NUMBER(*,0)) ON COMMIT "
 "PRESERVE ROWS "
 "CREATE INDEX "I_T_TEMP_SESSION" ON "T_TEMP_SESSION" ("X" ) "
Import terminated successfully without warnings.

这里体现了创建临时表和索引的语句,因此临时表的结构数据是可以被导出的。

3)尝试导入数据

ora10g@secdb /home/oracle$ imp sec/sec file=t_temp_session.dmp full=y ignore=y

Import: Release 10.2.0.1.0 - Production on Wed Jun 29 22:07:16 2011

Copyright (c) 1982, 2005, Oracle.  All rights reserved.


Connected to: Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Production
With the Partitioning, OLAP and Data Mining options

Export file created by EXPORT:V10.02.01 via conventional path
import done in WE8ISO8859P1 character set and AL16UTF16 NCHAR character set
. importing SEC's objects into SEC
. importing SEC's objects into SEC
Import terminated successfully without warnings.

依然显示没有记录被导入。

5.查看临时表空间的使用情况
可以通过查询V$SORT_USAGE视图获得相关信息。

sec@ora10g> select username,tablespace,session_num sid,sqladdr,sqlhash,segtype,extents,blocks from v$sort_usage;

USERNAME TABLESPACE     SID SQLADDR     SQLHASH SEGTYPE EXTENTS  BLOCKS
-------- ---------- ------- -------- ---------- ------- ------- -------
SEC      TEMP           370 389AEC58 1029988163 DATA          1     128
SEC      TEMP           370 389AEC58 1029988163 INDEX         1     128

可见SEC用户中创建的临时表以及其上的索引均存放在TEMP临时表空间中。
在创建用户的时候,可以指定用户的默认临时表空间,这样不同用户在创建临时表的时候便可以使用各自的临时表空间,互不干扰。

6.不同的session不可以互相访问对方的临时表数据
1)在第一个session中查看临时表数据

sec@ora10g> select * from t_temp_session;

         X
----------
         1
         2

此数据为初始化环境时候插入的数据。

2)在单独开启一个session,查看临时表数据。

ora10g@secdb /home/oracle$ sqlplus sec/sec

SQL*Plus: Release 10.2.0.1.0 - Production on Wed Jun 29 22:30:05 2011

Copyright (c) 1982, 2005, Oracle.  All rights reserved.


Connected to:
Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Production
With the Partitioning, OLAP and Data Mining options

sec@ora10g> select * from t_temp_session;

no rows selected

说明不同的session拥有各自独立的临时表操作特点,不同的session之间是不能互相访问数据。

7.临时表数据将不会上DML(Data Manipulation Language)锁
1)在新session中查看SEC用户下锁信息

sec@ora10g> col username for a8
sec@ora10g> select
  2       b.username,
  3       a.sid,
  4       b.serial#,
  5       a.type "lock type",
  6       a.id1,
  7       a.id2,
  8       a.lmode
  9  from v$lock a, v$session b
 10  where a.sid=b.sid and b.username = 'SEC'
 11  order by username,a.sid,serial#,a.type;

no rows selected

不存在任何锁信息。

2)向临时表中插入数据,查看锁信息
(1)插入数据

sec@ora10g> insert into t_temp_session values (1);

1 row created.

(2)查看锁信息

sec@ora10g> select
  2       b.username,
  3       a.sid,
  4       b.serial#,
  5       a.type "lock type",
  6       a.id1,
  7       a.id2,
  8       a.lmode
  9  from v$lock a, v$session b
 10  where a.sid=b.sid and b.username = 'SEC'
 11  order by username,a.sid,serial#,a.type;

                               lock                                lock
USERNAME        SID    SERIAL# type           id1         id2      mode
-------- ---------- ---------- ------ ----------- ----------- ---------
SEC             142        425 TO           12125           1         3
SEC             142        425 TX           65554         446         6

此时出现TOTX类型锁。

(3)提交数据后再次查看锁信息

sec@ora10g> commit;

Commit complete.

sec@ora10g> select
  2       b.username,
  3       a.sid,
  4       b.serial#,
  5       a.type "lock type",
  6       a.id1,
  7       a.id2,
  8       a.lmode
  9  from v$lock a, v$session b
 10  where a.sid=b.sid and b.username = 'SEC'
 11  order by username,a.sid,serial#,a.type;

                               lock                                lock
USERNAME        SID    SERIAL# type           id1         id2      mode
-------- ---------- ---------- ------ ----------- ----------- ---------
SEC             142        425 TO           12125           1         3

事务所TX被释放。TO锁保留。

3)测试更新数据场景下锁信息变化
(1)更新临时表数据

sec@ora10g> update t_temp_session set x=100;

1 row updated.

(2)锁信息如下

                               lock                                lock
USERNAME        SID    SERIAL# type           id1         id2      mode
-------- ---------- ---------- ------ ----------- ----------- ---------
SEC             142        425 TO           12125           1         3
SEC             142        425 TX          524317         464         6

(3)提交数据

sec@ora10g> commit;

Commit complete.

(4)锁信息情况

                               lock                                lock
USERNAME        SID    SERIAL# type           id1         id2      mode
-------- ---------- ---------- ------ ----------- ----------- ---------
SEC             142        425 TO           12125           1         3

4)测试删除数据场景下锁信息变化
(1)删除临时表数据

sec@ora10g> delete from t_temp_session;

1 row deleted.

(2)查看锁信息

                               lock                                lock
USERNAME        SID    SERIAL# type           id1         id2      mode
-------- ---------- ---------- ------ ----------- ----------- ---------
SEC             142        425 TO           12125           1         3
SEC             142        425 TX          327713         462         6

(3)提交数据

sec@ora10g> commit;

Commit complete.

(4)锁信息情况

                               lock                                lock
USERNAME        SID    SERIAL# type           id1         id2      mode
-------- ---------- ---------- ------ ----------- ----------- ---------
SEC             142        425 TO           12125           1         3

5)总结
在临时表上的增删改等DML操作都会产生TO锁和TX事务所。TO锁会从插入数据开始一直存在。
但整个过程中都不会产生DMLTM级别锁。

8.小结

本文就临时表使用过程中常见的问题和特点进行了介绍。临时表作为Oracle的数据库对象,如果能够在理解这些特性基础上加以利用将会极大地改善系统性能。

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Oracle数据库中的临时表空间(Temporary Tablespace)主要用于存储SQL语句执行过程中生成的临时结果集和排序中间结果。它在数据库运行过程中起到了重要的作用。 Oracle数据库中的临时表空间使用历史可以追溯到早期版本的Oracle数据库。在Oracle 7中,临时表空间的引入就大大提高了数据库的性能和可伸缩性。在早期版本的Oracle数据库中,排序和临时结果集的存储通常是通过使用数据库内部的排序区(Sort Area)和排序段(Sort Segment)来完成的,这种方式对内存的需求较大,并且容易导致性能瓶颈。为了解决这个问题,Oracle引入了临时表空间的概念。 临时表空间的引入提供了一种从磁盘读取和写入排序结果的方法,从而减轻了内存的压力,并改善了排序操作的性能。临时表空间可以由系统管理员在数据库中手动创建,或者可以由自动管理的表空间管理(Automatic Storage Management)来创建和管理。 临时表空间的使用方式通常是在SQL语句执行之前,临时表空间会被分配给用户会话。当SQL语句执行期间需要排序或者产生临时结果集时,数据被写入临时表空间。一旦排序或者查询结束,临时表空间会被释放,以便其他会话使用临时表空间的大小通常需要根据系统的负载和需求来决定,过小的临时表空间可能导致临时表空间不足的错误,而过大的临时表空间则会占用过多的磁盘空间。因此,管理者需要根据实际情况来调整临时表空间的大小。 总之,Oracle数据库中的临时表空间在SQL语句的执行过程中起到了重要的作用,它提供了一种存储临时结果集和排序结果的方法,并提升了数据库的性能和可伸缩性。通过合理地设置临时表空间的大小和管理,可以确保数据库的高效运行。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值