此文將給出在使用Oracle臨時(shí)表的過(guò)程中需要注意的事項(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不可以互相訪問(wèn)對(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
可見(jiàn),在創(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
由于物化視圖日志沒(méi)有創(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.
可見(jiàn)在備份過(guò)程中,沒(méi)有顯示有數(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.
依然顯示沒(méi)有記錄被導(dǎo)入。
5.查看臨時(shí)表空間的使用情況
可以通過(guò)查詢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
可見(jiàn)SEC用戶中創(chuàng)建的臨時(shí)表以及其上的索引均存放在TEMP臨時(shí)表空間中。
在創(chuàng)建用戶的時(shí)候,可以指定用戶的默認(rèn)臨時(shí)表空間,這樣不同用戶在創(chuàng)建臨時(shí)表的時(shí)候便可以使用各自的臨時(shí)表空間,互不干擾。
6.不同的session不可以互相訪問(wèn)對(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ú)開(kāi)啟一個(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
說(shuō)明不同的session擁有各自獨(dú)立的臨時(shí)表操作特點(diǎn),不同的session之間是不能互相訪問(wèn)數(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ù)開(kāi)始一直存在。
但整個(gè)過(guò)程中都不會(huì)產(chǎn)生DML的TM級(jí)別鎖。
8.小結(jié)
本文就臨時(shí)表使用過(guò)程中常見(jiàn)的問(wèn)題和特點(diǎn)進(jìn)行了介紹。臨時(shí)表作為Oracle的數(shù)據(jù)庫(kù)對(duì)象,如果能夠在理解這些特性基礎(chǔ)上加以利用將會(huì)極大地改善系統(tǒng)性能。
Good luck.
secooler
11.06.29
-- The End --
當(dāng)前題目:【TEMPORARYTABLE】Oracle臨時(shí)表使用注意事項(xiàng)
瀏覽地址:http://aaarwkj.com/article26/gjdpcg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供靜態(tài)網(wǎng)站、網(wǎng)站建設(shè)、做網(wǎng)站、面包屑導(dǎo)航、虛擬主機(jī)、品牌網(wǎng)站設(shè)計(jì)
廣告
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源:
創(chuàng)新互聯(lián)