1、前言
目前所有使用oracle作為數據庫支撐平臺的應用,大部分數據量比較龐大的系統,即表的數據量一般情況下都是在百萬級以上的數據量。
當然在oracle中創建分區是一種不錯的選擇,但是當你發現你的應用有多張表關聯的時候,并且這些表大部分都是比較龐大,而你關聯的時候發現其中的某一張或者某幾張表關聯之后得到的結果集非常小并且查詢得到這個結果集的速度非常快,那么這個時候我考慮在oracle中創建“臨時表”。
我對臨時表的理解:在oracle中創建一張表,這個表不用于其他的什么功能,主要用于自己的軟件系統一些特有功能才用的,而當你用完之后表中的數據就沒用了。oracle的臨時表創建之后基本不占用表空間,如果你沒有指定臨時表(包括臨時表的索引)存放的表空的時候,你插入到臨時表的數據是存放在oracle系統的臨時表空間中(temp)。
2、臨時表的創建
創建oracle臨時表,可以有兩種類型的臨時表:會話級的臨時表和事務級的臨時表。
1)會話級的臨時表因為這這個臨時表中的數據和你的當前會話有關系,當你當前session不退出的情況下,臨時表中的數據就還存在,而當你退出當前session的時候,臨時表中的數據就全部沒有了,當然這個時候你如果以另外一個session登陸的時候是看不到另外一個session中插入到臨時表中的數據的。即兩個不同的session所插入的數據是互不相干的。當某一個session退出之后臨時表中的數據就被截斷(truncate table,即數據清空)了。會話級的臨時表創建方法:
<!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
-->
create
global
temporary
table
table_name(col1 type1,col2 type2...)
on
commit
preserve rows;
舉例:
<!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
-->
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的時候,事務級的臨時表也會被自動截斷)。事務級臨時表的創建方法:
<!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
-->
create
global
temporary
table
table_name(col1 type1,col2 type2...)
on
commit
delete
rows;
舉例:
<!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
-->
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或者是會話結束,臨時表中的數據都將被截斷。
3、例子:
1)、會話級 (session關閉掉之后數據就沒有了,當commit的時候則數據還在,當rollback的時候則數據也是一樣被回滾):
<!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
-->
insert
into
student(stu_id,class_id,stu_name,stu_memo)
values
(
1
,
1
,
''
張三
''
,
''
福建
''
);
insert
into
student(stu_id,class_id,stu_name,stu_memo)
values
(
2
,
1
,
''
劉德華
''
,
''
福州
''
);
insert
into
student(stu_id,class_id,stu_name,stu_memo)
values
(
3
,
2
,
''
s.h.e
''
,
''
廈門
''
);sql
>
select
*
from
student ; stu_id class_id stu_name stu_memo
1
1
張三 福建2
1
劉德華 福州3
2
s.h.e 廈門4
2
張惠妹 廈門 sql
>
commit
;
commit
complete sql
>
select
*
from
student ; stu_id class_id stu_name stu_memo
1
1
張三 福建2
1
劉德華 福州3
2
s.h.e 廈門4
2
張惠妹 廈門 sql
>
insert
into
student(stu_id,class_id,stu_name,stu_memo)
values
(
4
,
2
,
''
張惠妹
''
,
''
廈門
''
);
1
row inserted sql
>
select
*
from
student ; stu_id class_id stu_name stu_memo
1
1
張三 福建2
1
劉德華 福州3
2
s.h.e 廈門4
2
張惠妹 廈門4
2
張惠妹 廈門 sql
>
rollback
;
rollback
complete sql
>
select
*
from
student ; stu_id class_id stu_name stu_memo
1
1
張三 福建2
1
劉德華 福州3
2
s.h.e 廈門4
2
張惠妹 廈門
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061
微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元

