temporary tablespace 2

Keywords:

basic concepts, principles

Characteristics of temporary tables and when to use them

 

 

Some time ago, the interviewer of the new company asked a question about the role of temporary tables. When we used to cache intermediate data, we built a temporary table ourselves. In fact, oracle itself has considered this aspect very well. Unless there are some advanced applications, I will consider creating temporary tables by myself. Since I don't know much about temporary tables, I came back to collect information in this area to make up for this lack.

1. Preface

 

    At present, all the applications that use Oracle as the database support platform, most of the systems with large data volume, that is, the data volume of the table is generally more than one million data volume.

 

    Of course, creating partitions in Oracle is a good choice, but when you find that your application is associated with multiple tables, and most of these tables are relatively large, you find one of them or The result set obtained after the association of certain tables is very small, and the result set obtained by the query is very fast, so at this time I consider creating a "temporary table" in Oracle .

 

 My understanding of temporary tables: create a table in Oracle , this table is not used for other functions, it is mainly used for some unique functions of your own software system, and when you use it up, the data in the table is gone. used. After the temporary table of Oracle is created, it basically does not occupy the table space. If you do not specify that the temporary table (including the index of the temporary table) stores the table empty, the data you insert into the temporary table is stored in the temporary table space of the ORACLE system ( TEMP ).

 

 

2. Creation of temporary table

To create an Oracle temporary table, there can be two types of temporary tables:

session-level temporary table

Transaction-level temporary tables .

1)  Session-level temporary table Because the data in this temporary table is related to your current session, when your current SESSION does not exit, the data in the temporary table still exists, and when you exit the current SESSION , all the data in the temporary table is gone. Of course, if you log in with another SESSION at this time, you cannot see the data inserted into the temporary table in another SESSION . That is, the data inserted by two different SESSIONs are irrelevant. When a SESSION exits, the data in the temporary table is truncated ( truncate table , that is, the data is emptied). Session-level temporary table creation method:

Create Global Temporary Table Table_Name

(Col1 Type1,Col2 Type2...)On Commit Preserve Rows 

Example:

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;用法上,会话级别只有当会话结束临时表中的数据才会被截断,而且事务级临时表则不管是commitrollback或者是会话结束,临时表中的数据都将被截断

 

4)什么时候使用临时表

 

1)、当某一个SQL语句关联的表在2张及以上,并且和一些小表关联。可以采用将大表进行分拆并且得到比较小的结果集合存放在临时表中

2)、程序执行过程中可能需要存放一些临时的数据,这些数据在整个程序的会话过程中都需要用的等等。

 

 

3例子:略

4.临时表的不足之处

 

    1)不支持lob对象,这也许是设计者基于运行效率的考虑,但实际应用中确实需要此功能时就无法使用临时表了。

2)不支持主外键关系

 

所以,由于以上原因,我们可以自己创建临时表,以弥补oracle临时表的不足之处

 

上面的都是本人经过测试的,但下面是在网上搜索到的方法,本人具体没有测试过,不过觉得可行性很强,有时间测试下

 

   创建方法:

      1、以常规表的形式创建临时数据表的表结构,但要在每一个表的主键中加入一个 SessionID <NUMBER> 列以区分不同的会话。(可以有lob列和主外键)

2、写一个用户注销触发器,在用户结束会话的时候删除本次会话所插入的所有记录(SessionID等于本次会话ID的记录)

3、程序写入数据时,要顺便将当前的会话ID(SessionID)写入表中。

4、程序读取数据时,只读取与当前会话ID相同的记录即可。

   功能增强的扩展设计:
  1、可以在数据表上建立一个视图,视图对记录的筛选条件就是当前会话的SessionID
  2、数据表中的SessionID列可以通过Trigger实现,以实现对应用层的透明性。
  3、高级用户可以访问全局数据,以实现更加复杂的功能。

  扩展临时表的优点:
  1、实现了与Oracle的基于会话的临时表相同的功能。
  2、支持SDO_GEOMETRYlob数据类型。
  3、支持表间的主外键连接,且主外键连接也是基于会话的。
  4、高级用户可以访问全局数据,以实现更加复杂的功能

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326987541&siteId=291194637