溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

【TEMPORARY TABLE】Oracle臨時(shí)表使用注意事項(xiàng)

發(fā)布時(shí)間:2020-08-10 02:48:15 來源:ITPUB博客 閱讀:372 作者:eddy0lion 欄目:建站服務(wù)器
  此文將給出在使用Oracle臨時(shí)表的過程中需要注意的事項(xiàng),并對(duì)這些特點(diǎn)進(jìn)行驗(yàn)證。
  臨時(shí)表不支持物化視圖
  可以在臨時(shí)表上創(chuàng)建索引
 
可以基于臨時(shí)表創(chuàng)建視圖
 
臨時(shí)表結(jié)構(gòu)可被導(dǎo)出,但內(nèi)容不可以被導(dǎo)出
 
臨時(shí)表通常是創(chuàng)建在用戶的臨時(shí)表空間中的,不同用戶可以有自己的獨(dú)立的臨時(shí)表空間
 
不同的session不可以互相訪問對(duì)方的臨時(shí)表數(shù)據(jù)
  臨時(shí)表數(shù)據(jù)將不會(huì)上DML(Data Manipulation Language)鎖


1.
臨時(shí)表不支持物化視圖
1)環(huán)境準(zhǔn)備
(1)創(chuàng)建基于會(huì)話的臨時(shí)表
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)初始化兩條數(shù)據(jù)
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)在臨時(shí)表
T_TEMP_SESSION上添加主鍵
sec@ora10g> alter table T_TEMP_SESSION add constraint PK_T_TEMP_SESSION primary key(x);

Table altered.

2)在臨時(shí)表T_TEMP_SESSION上創(chuàng)建物化視圖
(1)創(chuàng)建物化視圖日志日志
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

可見,在創(chuàng)建物化視圖時(shí)便提示,臨時(shí)表上無(wú)法創(chuàng)建物化視圖日志。

(2)創(chuàng)建物化視圖
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

由于物化視圖日志沒有創(chuàng)建成功,因此顯然物化視圖亦無(wú)法創(chuàng)建。

2.在臨時(shí)表上創(chuàng)建索引
sec@ora10g> create index i_t_temp_session on t_temp_session (x);

Index created.

臨時(shí)表上索引創(chuàng)建成功。

3.基于臨時(shí)表創(chuàng)建視圖
sec@ora10g> create view v_t_temp_session as select * from t_temp_session where x<100;

View created.

基于臨時(shí)表的視圖創(chuàng)建成功。

4.臨時(shí)表結(jié)構(gòu)可被導(dǎo)出,但內(nèi)容不可以被導(dǎo)出
1)使用exp工具備份臨時(shí)表
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.


可見在備份過程中,沒有顯示有數(shù)據(jù)被導(dǎo)出。

2)使用imp工具的show選項(xiàng)查看備份介質(zhì)中的SQL內(nèi)容
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.


這里體現(xiàn)了創(chuàng)建臨時(shí)表和索引的語(yǔ)句,因此臨時(shí)表的結(jié)構(gòu)數(shù)據(jù)是可以被導(dǎo)出的。

3)嘗試導(dǎo)入數(shù)據(jù)
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.

依然顯示沒有記錄被導(dǎo)入。

5.查看臨時(shí)表空間的使用情況
可以通過查詢V$SORT_USAGE視圖獲得相關(guān)信息。
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用戶中創(chuàng)建的臨時(shí)表以及其上的索引均存放在TEMP臨時(shí)表空間中。
在創(chuàng)建用戶的時(shí)候,可以指定用戶的默認(rèn)臨時(shí)表空間,這樣不同用戶在創(chuàng)建臨時(shí)表的時(shí)候便可以使用各自的臨時(shí)表空間,互不干擾。

6.不同的session不可以互相訪問對(duì)方的臨時(shí)表數(shù)據(jù)
1)在第一個(gè)session中查看臨時(shí)表數(shù)據(jù)
sec@ora10g> select * from t_temp_session;

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

此數(shù)據(jù)為初始化環(huán)境時(shí)候插入的數(shù)據(jù)。

2)在單獨(dú)開啟一個(gè)session,查看臨時(shí)表數(shù)據(jù)。
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擁有各自獨(dú)立的臨時(shí)表操作特點(diǎn),不同的session之間是不能互相訪問數(shù)據(jù)。

7.臨時(shí)表數(shù)據(jù)將不會(huì)上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)向臨時(shí)表中插入數(shù)據(jù),查看鎖信息
(1)插入數(shù)據(jù)
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

此時(shí)出現(xiàn)TO和TX類型鎖。

(3)提交數(shù)據(jù)后再次查看鎖信息
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

事務(wù)所TX被釋放。TO鎖保留。

3)測(cè)試更新數(shù)據(jù)場(chǎng)景下鎖信息變化
(1)更新臨時(shí)表數(shù)據(jù)
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)提交數(shù)據(jù)
sec@ora10g> commit;

Commit complete.

(4)鎖信息情況
                               lock                                lock
USERNAME        SID    SERIAL# type           id1         id2      mode
-------- ---------- ---------- ------ ----------- ----------- ---------
SEC             142        425 TO           12125           1         3

4)測(cè)試刪除數(shù)據(jù)場(chǎng)景下鎖信息變化
(1)刪除臨時(shí)表數(shù)據(jù)
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)提交數(shù)據(jù)
sec@ora10g> commit;

Commit complete.

(4)鎖信息情況
                               lock                                lock
USERNAME        SID    SERIAL# type           id1         id2      mode
-------- ---------- ---------- ------ ----------- ----------- ---------
SEC             142        425 TO           12125           1         3

5)總結(jié)
在臨時(shí)表上的增刪改等DML操作都會(huì)產(chǎn)生TO鎖和TX事務(wù)所。TO鎖會(huì)從插入數(shù)據(jù)開始一直存在。
但整個(gè)過程中都不會(huì)產(chǎn)生DML的TM級(jí)別鎖。

8.小結(jié)
  本文就臨時(shí)表使用過程中常見的問題和特點(diǎn)進(jìn)行了介紹。臨時(shí)表作為Oracle的數(shù)據(jù)庫(kù)對(duì)象,如果能夠在理解這些特性基礎(chǔ)上加以利用將會(huì)極大地改善系統(tǒng)性能。

Good luck.

secooler
11.06.29

-- The End --

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI