1、前言
    目前所有使用oracle作为数据库支撑平台的应用,大部分数据量比较庞大的系统,即表的数据量一般情况下都是在百万级以上的数据量。
    当然在oracle中创建分区是一种不错的选择,但是当你发现你的应用有多张表关联的时候,并且这些表大部分都是比较庞大,而你关联的时候发现其中的某一张或者某几张表关联之后得到的结果集非常小并且查询得到这个结果集的速度非常快,那么这个时候我考虑在oracle中创建“临时表”。
    我对临时表的理解:在oracle中创建一张表,这个表不用于其他的什么功能,主要用于自己的软件系统一些特有功能才用的,而当你用完之后表中的数据就没用了。oracle的临时表创建之后基本不占用表空间,如果你没有指定临时表(包括临时表的索引)存放的表空的时候,你插入到临时表的数据是存放在oracle系统的临时表空间中(temp)。
    2、临时表的创建
    创建oracle临时表,可以有两种类型的临时表:会话级的临时表和事务级的临时表。   
    1)会话级的临时表因为这这个临时表中的数据和你的当前会话有关系,当你当前session不退出的情况下,临时表中的数据就还存在,而当你退出当前session的时候,临时表中的数据就全部没有了,当然这个时候你如果以另外一个session登陆的时候是看不到另外一个session中插入到临时表中的数据的。即两个不同的session所插入的数据是互不相干的。当某一个session退出之后临时表中的数据就被截断(truncate table,即数据清空)了。会话级的临时表创建方法:
create global temporary table table_name(col1 type1,col2 type2...)    
     on commit preserve rows;
   举例:
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的时候,事务级的临时表也会被自动截断)。事务级临时表的创建方法:
create global temporary table table_name(col1 type1,col2 type2...)
on commit delete rows;
   举例:
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的时候则数据也是一样被回滚):
     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 张惠妹 厦门