1、前言
目前所有使用oracle作為數(shù)據(jù)庫(kù)支撐平臺(tái)的應(yīng)用,大部分?jǐn)?shù)據(jù)量比較龐大的系統(tǒng),即表的數(shù)據(jù)量一般情況下都是在百萬(wàn)級(jí)以上的數(shù)據(jù)量。
當(dāng)然在oracle中創(chuàng)建分區(qū)是一種不錯(cuò)的選擇,但是當(dāng)你發(fā)現(xiàn)你的應(yīng)用有多張表關(guān)聯(lián)的時(shí)候,并且這些表大部分都是比較龐大,而你關(guān)聯(lián)的時(shí)候發(fā)現(xiàn)其中的某一張或者某幾張表關(guān)聯(lián)之后得到的結(jié)果集非常小并且查詢得到這個(gè)結(jié)果集的速度非???,那么這個(gè)時(shí)候我考慮在oracle中創(chuàng)建“臨時(shí)表”。
我對(duì)臨時(shí)表的理解:在oracle中創(chuàng)建一張表,這個(gè)表不用于其他的什么功能,主要用于自己的軟件系統(tǒng)一些特有功能才用的,而當(dāng)你用完之后表中的數(shù)據(jù)就沒用了。oracle的臨時(shí)表創(chuàng)建之后基本不占用表空間,如果你沒有指定臨時(shí)表(包括臨時(shí)表的索引)存放的表空的時(shí)候,你插入到臨時(shí)表的數(shù)據(jù)是存放在oracle系統(tǒng)的臨時(shí)表空間中(temp)。
2、臨時(shí)表的創(chuàng)建
創(chuàng)建oracle臨時(shí)表,可以有兩種類型的臨時(shí)表:會(huì)話級(jí)的臨時(shí)表和事務(wù)級(jí)的臨時(shí)表。
1)會(huì)話級(jí)的臨時(shí)表因?yàn)檫@這個(gè)臨時(shí)表中的數(shù)據(jù)和你的當(dāng)前會(huì)話有關(guān)系,當(dāng)你當(dāng)前session不退出的情況下,臨時(shí)表中的數(shù)據(jù)就還存在,而當(dāng)你退出當(dāng)前session的時(shí)候,臨時(shí)表中的數(shù)據(jù)就全部沒有了,當(dāng)然這個(gè)時(shí)候你如果以另外一個(gè)session登陸的時(shí)候是看不到另外一個(gè)session中插入到臨時(shí)表中的數(shù)據(jù)的。即兩個(gè)不同的session所插入的數(shù)據(jù)是互不相干的。當(dāng)某一個(gè)session退出之后臨時(shí)表中的數(shù)據(jù)就被截?cái)啵╰runcate table,即數(shù)據(jù)清空)了。會(huì)話級(jí)的臨時(shí)表創(chuàng)建方法:
<!-- 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)事務(wù)級(jí)臨時(shí)表是指該臨時(shí)表與事務(wù)相關(guān),當(dāng)進(jìn)行事務(wù)提交或者事務(wù)回滾的時(shí)候,臨時(shí)表中的數(shù)據(jù)將自行被截?cái)?,其他的?nèi)容和會(huì)話級(jí)的臨時(shí)表的一致(包括退出session的時(shí)候,事務(wù)級(jí)的臨時(shí)表也會(huì)被自動(dòng)截?cái)啵?。事?wù)級(jí)臨時(shí)表的創(chuàng)建方法:
<!-- 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)、兩種不通類型的臨時(shí)表的區(qū)別:語(yǔ)法上,會(huì)話級(jí)臨時(shí)表采用on commit preserve rows而事務(wù)級(jí)則采用on commit delete rows;用法上,會(huì)話級(jí)別只有當(dāng)會(huì)話結(jié)束臨時(shí)表中的數(shù)據(jù)才會(huì)被截?cái)?,而且事?wù)級(jí)臨時(shí)表則不管是commit、rollback或者是會(huì)話結(jié)束,臨時(shí)表中的數(shù)據(jù)都將被截?cái)唷?
3、例子:
1)、會(huì)話級(jí) (session關(guān)閉掉之后數(shù)據(jù)就沒有了,當(dāng)commit的時(shí)候則數(shù)據(jù)還在,當(dāng)rollback的時(shí)候則數(shù)據(jù)也是一樣被回滾):
<!-- 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 張惠妹 廈門
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號(hào)聯(lián)系: 360901061
您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對(duì)您有幫助就好】元
