---
title: "Temporary Segments "
author: "edwards0307"
date: "2011-11-29T15:52:44Z"
tags: []
categories: ["数据库"]
slug: "temporary-segments"
draft: false
lastmod: "2011-11-30T15:11:51Z"
id: 1284072
url: "https://killdbs.iteye.com/blog/1284072"
---

Temporary Segments:  including Temporary Segments for Queries  and Temporary Tables and Indexes

 

**Allocation of Temporary Segments for Queries(sort segments)**

[]()**One or more temporary tablespaces can be used only for sort segments. A temporary tablespace is not the same as a tablespace that a user designates for temporary segments, which can be any tablespace available to the user. No permanent schema objects can reside in a temporary tablespace.**

**Sort segments are used when a segment is shared by multiple sort operations. One sort segment exists for every instance that performs a sort operation in a given tablespace.**

**Temporary tablespaces provide performance improvements when you have multiple sorts that are too large to fit into memory. The sort segment of a given temporary tablespace is created at the time of the first sort operation. The sort segment expands by allocating extents until the segment size is equal to or greater than the total storage demands of all of the active sorts running on that instance.**

[]()[]()[]()O[]()racle allocates temporary segments as needed during a use[]()r session []()[]()[]()in one of the temporary tablespaces of the user issuing the statement. Specify these tablespaces with a `CREATE` `USER` or an `ALTER` `USER` statement using the `TEMPORARY` `TABLESPACE` clause.

Note:

You cannot assign a permanent tablespace as a user's temporary tablespace.

If no temporary tablespace is defined for the user, then the default temporary tablespace is the `SYSTEM` tablespace. The default storage characteristics of the containing tablespace determine those of the extents of the temporary segment. Oracle drops temporary segments when the statement completes.

Because allocation and deallocation of temporary segments occur frequently, create at least one special tablespace for temporary segments. By doing so, you can distribute I/O across disk devices, and you can avoid fragmentation of the `SYSTEM` and other tablespaces that otherwise hold temporary segments.

Note:

When the `SYSTEM` tablespace is locally managed, you must define a default temporary tablespace when creating a database. A locally managed `SYSTEM` tablespace cannot be used for default temporary storage.

[]()[]()Entries for changes to temporary segments used for sort operations are not stored in the redo log, except for space management operations on the temporary segment.

##### Allocation of Temporary Segments for Temporary Tables and Indexes

[]()[]()In addition to permanent tables, Oracle can create temporary tables to hold session-private data that exists only for the duration of a transaction or session.

 

Oracle allocates segments for a temporary table when the first `INSERT` into that table is issued. (This can be an internal insert operation issued by `CREATE` `TABLE` `AS` `SELECT`.) The first `INSERT` into a temporary table allocates the segments for the table and its indexes, creates the root page for the indexes, and allocates any `LOB` segments.

 

Segments for a temporary table are allocated in a temporary tablespace of the user who created the temporary table.

Oracle drops segments for a transaction-specific temporary table at the end of the transaction and drops segments for a session-specific temporary table at the end of the session. If other transactions or sessions share the use of that temporary table, the segments containing their data remain in the table.

 

[]()The `CREATE` `GLOBAL` `TEMPORARY` `TABLE` statement creates a temporary table that can be transaction-specific or session-specific. For transaction-specific temporary tables, data exists for the duration of the transaction. For session-specific temporary tables, data exists for the duration of the session. Data in a temporary table is private to the session. Each session can only see and modify its own data. DML locks are not acquired on the data of the temporary tables. The `LOCK` statement has no effect on a temporary table, because each session has its own private data.

A `TRUNCATE` statement issued on a session-specific temporary table truncates data in its own session. It does not truncate the data of other sessions that are using the same table.

DML statements on temporary tables do not generate redo logs for the data changes. However, undo logs for the data and redo logs for the undo logs are generated. Data from the temporary table is automatically dropped in the case of session termination, either when the user logs off or when the session terminates abnormally such as during a session or instance failure.

You can create indexes for temporary tables using the `CREATE` `INDEX` statement. Indexes created on temporary tables are also temporary, and the data in the index has the same session or transaction scope as the data in the temporary table.

You can create views that access both temporary and permanent tables. You can also create triggers on temporary tables.

Oracle utilities can export and import the definition of a temporary table. However, no data rows are exported even if you use the `ROWS` clause. Similarly, you can replicate the definition of a temporary table, but you cannot replicate its data.

[]()

#### Segment Allocation

[]()[]()Temporary tables use temporary segments. Unlike permanent tables, temporary tables and their indexes do not automatically allocate a segment when they are created. Instead, segments are allocated when the first `INSERT` (or `CREATE` `TABLE AS` `SELECT`) is performed. This means that if a `SELECT`, `UPDATE`, or `DELETE` is performed before the first `INSERT`, then the table appears to be empty.

You can perform DDL statements (`ALTER` `TABLE`, `DROP` `TABLE`, `CREATE` `INDEX,` and so on) on a temporary table only when no session is currently bound to it. A session gets bound to a temporary table when an `INSERT` is performed on it. The session gets unbound by a `TRUNCATE`, at session termination, or by doing a `COMMIT` or `ROLLBACK` for a transaction-specific temporary table.

Temporary segments are deallocated at the end of the transaction for transaction-specific temporary tables and at the end of the session for session-specific temporary tables.