Oracle数据库中的临时表

概述

写这篇文章的起因,是由于客户存在这样的场景。每次登录时,需要创建一些临时表(业务上临时),然后插入一些数据,返回到客户端,然后删除这些表。最初用户用的是普通表,带来很大的开销。后来改为Global Temporary Table得到了改善。

概念

Oracle数据库中的临时表分为两类,Global Temporary Table和Private Temporary Table。以下简称GTT和PTT。
GTT是8i就有的特性,而PTT是18c开始支持,从database feature网站可以查阅。GTT是各个会话共享表定义,但数据是私有的。PTT则每个会话可以有自己的表定义。无论是PTT还是GTT,数据都是在事务提交或会话退出时删除。
由于临时表不经过buffer cache,数据存于临时表空间;同时只有undo不产生redo(因为无需持久化),因此性能很好。详见:OCA/OCP Oracle 数据库12c考试指南读书笔记:第5章: DDL and Schema Objects

临时表的语法帮助参见官方文档

GLOBAL TEMPORARY

Specify GLOBAL TEMPORARY to create a temporary table, whose definition is visible to all sessions with appropriate privileges. The data in a temporary table is visible only to the session that inserts the data into the table.
When you first create a temporary table, its metadata is stored in the data dictionary, but no space is allocated for table data. Space is allocated for the table segment at the time of the first DML operation on the table. The temporary table definition persists in the same way as the definitions of regular tables, but the table segment and any data the table contains are either session-specific or transaction-specific data. You specify whether the table segment and data are session- or transaction-specific with the ON COMMIT clause.
You can perform DDL operations (such as ALTER TABLE, DROP TABLE, CREATE INDEX) on a temporary table only when no session is bound to it. A session becomes bound to a temporary table with an INSERT operation on the table. A session becomes unbound to a temporary table with a TRUNCATE statement or at session termination, or, for a transaction-specific temporary table, by issuing a COMMIT or ROLLBACK statement.

PRIVATE TEMPORARY

Specify PRIVATE TEMPORARY to create a private temporary table.
A private temporary table differs from a temporary table in that its definition and data are visible only within the session that created it. Use the ON COMMIT clause to define the scope of a private temporary table: either transaction or session. The ON COMMIT clause used with the keywords DROP DEFINITION creates a transaction-specific table whose data and definition are dropped when the transaction commits. This is the default behavior. The ON COMMIT clause used with keywords PRESERVE DEFINITION creates a session-specific table whose definition is preserved when the transaction commits. See here for usage details of theON COMMIT clause.
Three DDL statements are supported for private temporary tables: CREATE, DROP, and TRUNCATE.

非SYS用户才可创建临时表

全局临时表练习

ON COMMIT DELETE ROWS - 默认,表示存活期为事务,commit时清空数据。
ON COMMIT PRESERVE ROWS - 表示存活期为会话,会话退出时清空数据。
如果用户用了连接池,那么存活期为会话就不适用了。
GTT可以创建索引,不过生命周期与数据是一致的。
GTT的数据存于默认临时表空间,也可以另指定。

参考

  1. https://blogs.oracle.com/optimizer/global-temporary-tables-and-upgrading-to-oracle-database-12c-dont-get-caught-out
  2. https://blogs.oracle.com/database/oracle-database-18c-:-now-available-on-the-oracle-cloud-and-oracle-engineered-systems
  3. https://blogs.oracle.com/sql/how-to-create-alter-and-drop-tables-in-sql
  4. https://blogs.oracle.com/oraclemagazine/on-oracle-database-12c-part-3
  5. https://www.oracletutorial.com/oracle-basics/oracle-global-temporary-table/
  6. https://oracle-base.com/articles/misc/temporary-tables
发布了342 篇原创文章 · 获赞 42 · 访问量 54万+

猜你喜欢

转载自blog.csdn.net/stevensxiao/article/details/103321526