Docsity

Oracle Tablespaces: Management and Configuration, Summaries of Database Management Systems (DBMS)

Toggle sidebar
bg1
Oracle Database Tablespace and
Data File Management
Understanding Storage and Space
Management
Managing Database Storage Structures
Data is stored in blocks within a database. Tablespaces are created and
managed to organize this storage.
Managing Space
The Oracle database server automates space management. Tablespace
space usage is proactively monitored and managed. The segment shrink
functionality reclaims wasted space from tables and indexes.
Physical and Logical Storage
Data is stored logically in tablespaces and physically in data files. A
tablespace can have multiple data files, but a data file belongs to only one
tablespace. Objects are stored on a single tablespace, but their physical
storage can span multiple data files within that tablespace. The size of a
tablespace is the total size of its data files. The database size is the total
size of all tablespaces, including data files, redo logs, and control files.
Adding data files or increasing their size increases tablespace size. Adding
tablespaces, adding data files to existing tablespaces, or increasing the size
of existing data files increases database size.
When a database is created, the SYSTEM tablespace is automatically
created. Additional space can be added to the SYSTEM tablespace by
adding data files or increasing their size. The SYSTEM tablespace must
remain online for the database to function. The SYSAUX tablespace is
created alongside the SYSTEM tablespace. Oracle recommends storing only
data dictionary objects in the SYSTEM tablespace.
Multiple tablespaces allow for: * Separating the Oracle dictionary from
other database objects. * Controlling I/O by allocating separate physical
storage disks. * Managing space quotas for users. * Having separate
tablespaces for temporary segments (TEMP) and undo management.
When a tablespace is created, data files are created with a specified size.
Extents are allocated from free space as needed. A segment must be
constructed from multiple extents located in separate data files to straddle
multiple data files. An extent is composed of a contiguous set of data blocks.
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff

Partial preview of the text

Download Oracle Tablespaces: Management and Configuration and more Summaries Database Management Systems (DBMS) in PDF only on Docsity!

Oracle Database Tablespace and

Data File Management

Understanding Storage and Space

Management

Managing Database Storage Structures

Data is stored in blocks within a database. Tablespaces are created and managed to organize this storage.

Managing Space

The Oracle database server automates space management. Tablespace space usage is proactively monitored and managed. The segment shrink functionality reclaims wasted space from tables and indexes.

Physical and Logical Storage

Data is stored logically in tablespaces and physically in data files. A tablespace can have multiple data files, but a data file belongs to only one tablespace. Objects are stored on a single tablespace, but their physical storage can span multiple data files within that tablespace. The size of a tablespace is the total size of its data files. The database size is the total size of all tablespaces, including data files, redo logs, and control files. Adding data files or increasing their size increases tablespace size. Adding tablespaces, adding data files to existing tablespaces, or increasing the size of existing data files increases database size.

When a database is created, the SYSTEM tablespace is automatically created. Additional space can be added to the SYSTEM tablespace by adding data files or increasing their size. The SYSTEM tablespace must remain online for the database to function. The SYSAUX tablespace is created alongside the SYSTEM tablespace. Oracle recommends storing only data dictionary objects in the SYSTEM tablespace.

Multiple tablespaces allow for: * Separating the Oracle dictionary from other database objects. * Controlling I/O by allocating separate physical storage disks. * Managing space quotas for users. * Having separate tablespaces for temporary segments (TEMP) and undo management.

When a tablespace is created, data files are created with a specified size. Extents are allocated from free space as needed. A segment must be constructed from multiple extents located in separate data files to straddle multiple data files. An extent is composed of a contiguous set of data blocks.

Contents of a Data Block

A data block is the minimum unit of I/O. Common data block sizes are 2K, 4K, 8K, 16K, or 32K. The DB_BLOCK_SIZE initialization parameter determines the data block size at database creation. A data block contains row data and free space. Oracle uses data block overhead information to manage the block. ROWID includes the data object number, the relative file number, the block number, and the row number.

Defining Block Storage Characteristics

PCTFREE and PCTUSED are data block storage parameters. Oracle allocates space from free space for each concurrent transaction.

Tablespaces

Tablespaces can store zero or more segments. Tables and indexes are examples of segments. Constraints and sequences do not have segments. Tables and indexes associated with an application can be placed into a set of tablespaces for easier management. The SYSAUX tablespace stores schema objects associated with Oracle-provided features. One or more temporary tablespaces, an undo tablespace, and several application tablespaces are usually created in a database.

Creating and Maintaining Tablespaces

Tablespaces are created using the CREATE DATABASE or CREATE TABLESPACE statement. Choices must be made regarding bigfile or smallfile, extent management (locally or with the dictionary), and segment space management (automatically or manually). Specialized tablespaces exist for temporary and undo segments.

Creating Bigfile and Smallfile Tablespaces

Bigfile tablespaces are built on a single data file, which can be as many as 232 data blocks in size. Bigfile tablespaces are intended for very large databases. Reducing the number of data files can speed up operations that update data file headers.

CREATE BIGFILE TABLESPACE PO_ARCHIVE DATAFILE '/u02/oradata/12CR11/po_arch

Smallfile tablespace is the new terminology for the old Oracle tablespace data file option. Smallfile tablespaces can have multiple data files. Data files in a smallfile tablespace that uses 8KB data blocks are limited to 32GB. A smallfile tablespace can have as many as 1,022 data files.

CREATE SMALLFILE TABLESPACE PO_DETAILS DATAFILE '/u02/oradata/12CR11/po_de

By default, Oracle Database 12c creates SMALLFILE tablespaces.

management uses bitmaps instead of free lists. The PCTFREE and PCTUSED parameters are ignored. Automatic segment space management is available only on tablespaces configured for local extent management. Automatic segment space management performs better and reduces maintenance tasks.

CREATE TABLESPACE hr_index DATAFILE '/u02/oradata/12CR11/hr_index01.dbf' S

When automatic segment space management is used, Oracle ignores the storage parameters PCTUSED, FREELISTS, and FREELIST GROUPS. Segment space management is more accurately regarded as block space management.

Tablespace Creation Options

When creating tablespaces, several options are available beyond the basic requirements. These include specifying a nondefault block size, defining default storage characteristics, specifying logging and flashback clauses, and creating offline tablespaces.

Dictionary-Managed Tablespace Example

The following example demonstrates optional clauses for a dictionary- managed tablespace:

CREATE TABLESPACE APPL_DATA DATAFILE '/disk3/oradata/DB01/appl_data01.dbf' 100M DEFAULT STORAGE ( INITIAL 256K NEXT 256K MINEXTENTS 2 PCTINCREASE 0 MAXEXTENTS 4096) BLOCKSIZE 16K MINIMUM EXTENT 256K LOGGING ONLINE FORCE LOGGING FLASHBACK ON EXTENT MANAGEMENT DICTIONARY SEGMENT SPACE MANAGEMENT MANUAL;

Locally-Managed Tablespace Example

The following example demonstrates optional clauses for a locally-managed tablespace:

CREATE TABLESPACE APPL_DATA DATAFILE '/disk3/oradata/DB01/appl_data01.dbf' 100M DEFAULT STORAGE COMPRESS BLOCKSIZE 16K LOGGING ONLINE FORCE LOGGING FLASHBACK ON EXTENT MANAGEMENT LOCAL SEGMENT SPACE MANAGEMENT AUTO;

Tablespace Characteristics

Oracle efficiently manages tablespace characteristics with default values, but finer control can be achieved by specifying various clauses. Storage parameters are applicable only to dictionary-managed tablespaces. For locally managed tablespaces, only the COMPRESS option can be specified.

BLOCKSIZE Clause

A block size different from the standard database block size can be specified when creating a new tablespace. If no block size is specified, the database block size is assumed. Multiple block sizes are beneficial for large databases with both OLTP and DSS data, and for storing large tables. To create tablespaces with nonstandard block sizes, the appropriate initialization parameter must be set to define a buffer cache size for the block size.

Creating Temporary Tablespaces

Temporary tablespaces improve space management for sort operations. By designating a tablespace exclusively for temporary segments, Oracle avoids allocating and deallocating temporary segments in permanent tablespaces. A temporary tablespace is used solely for sort segments and other operations like hash joins.

A temporary tablespace can be created during database creation using the DEFAULT TEMPORARY TABLESPACE clause of the CREATE DATABASE statement, or after database creation using the CREATE TEMPORARY TABLESPACE statement.

CREATE TEMPORARY TABLESPACE temp TEMPFILE '/u01/oradata/12CR11/temp01.dbf' SIZE 1G;

The keyword TEMPFILE is used instead of DATAFILE for temporary tablespaces. Temp files are allocated differently than data files. Data files are fully allocated and initialized at creation, while temp files may not allocate the specified disk space until needed.

It is good practice, and required when the SYSTEM tablespace is locally managed, to create a separate temporary tablespace.

If a default temporary tablespace is defined, new users will have it assigned as their temporary tablespace by default, unless another is specified. A data tablespace for application tables can also be designated during database creation using the DEFAULT TABLESPACE clause.

Temporary Tablespace Groups

If multiple temporary tablespaces exist, temporary tablespace groups can be used to utilize space in multiple tablespaces for a user's sort operation. The TABLESPACE GROUP clause is used when creating the temporary tablespace.

CREATE TEMPORARY TABLESPACE TEMP01 TEMPFILE '/u01/oradata/12CR11/temp01a.d size 200M EXTENT MANAGEMENT LOCAL UNIFORM SIZE 5M TABLESPACE GROUP ALL_TEMPS;

In this example, the tablespace is added to the ALL_TEMPS temporary tablespace group. Tablespace groups are only applicable to temporary

If the tablespace is not empty, the INCLUDING CONTENTS clause recursively removes segments.

DROP TABLESPACE dba_sandbox INCLUDING CONTENTS;

If referential integrity constraints from other tablespaces refer to objects in the tablespace being dropped, the CASCADE CONSTRAINTS clause must be specified.

DROP TABLESPACE USER_DATA INCLUDING CONTENTS CASCADE CONSTRAINTS;

Dropping a tablespace updates the control file. It does not automatically remove the data files. The INCLUDING CONTENTS AND DATAFILES clause removes the underlying data files.

DROP TABLESPACE hr_data INCLUDING CONTENTS AND DATAFILES;

If Oracle Managed Files are used, the files are automatically removed. Otherwise, OS commands or the AND DATAFILES clause can be used to remove the data files.

Modifying Tablespaces

The ALTER TABLESPACE statement modifies tablespace attributes. Actions include changing default storage clauses and MINIMUM_EXTENT for dictionary-managed tablespaces, adding data or temporary files, resizing files, renaming tablespaces or files, and shrinking temporary files or space.

Adding a Data File

Smallfile tablespaces can have multiple data files and can be spread across multiple file systems. The ADD DATAFILE clause with the ALTER TABLESPACE statement adds a data file.

ALTER TABLESPACE receivables ADD DATAFILE '/u02/oradata/ORA10/receivables SIZE 2G;

Taking Tablespaces Offline or Online

The availability of tablespaces can be controlled by taking them offline or online. Other tablespaces remain available. Oracle writes dirty buffer blocks to the data files and closes them when taking a tablespace offline.

ALTER TABLESPACE USER_DATA ONLINE;

The OFFLINE keyword makes a tablespace offline. SQL statements cannot reference objects in an offline tablespace. Unsaved changes result in rollback data being saved in a deferred rollback segment in the SYSTEM tablespace. When the tablespace is brought online, Oracle applies the rollback data.

Making a Tablespace Read-Only

Tablespaces with static data can be marked read-only. All data files must be online, and there can be no active transactions. Objects can be dropped, but new objects cannot be created. The READ ONLY clause with the ALTER TABLESPACE statement marks a tablespace read-only.

ALTER TABLESPACE sales2007 READ ONLY;

To make changes, the tablespace must be made read-write again with the READ WRITE keywords.

ALTER TABLESPACE sales2007 READ WRITE;

Oracle checks the availability of data files during database startup, but only checks read-only tablespace data files when an object in the tablespace is accessed.

Putting a Tablespace in Backup Mode

For non-RMAN online backups, a tablespace must be put in backup mode before copying data files. While in backup mode, checkpoints occur, but are not recorded in the data file headers.

ALTER TABLESPACE system BEGIN BACKUP;

The END BACKUP keywords take a tablespace out of backup mode.

ALTER TABLESPACE system END BACKUP;

If a tablespace is left in backup mode, the database will report that media recovery is required upon restart.

Obtaining Tablespace Information

Data Dictionary Views

Several data dictionary views provide information about tablespaces. These include DBA_TABLESPACES, DBA_DATA_FILES, DBA_TEMP_FILES, and V$TABLESPACE.

The DBA_TABLESPACES view contains one row for each tablespace. It provides information such as tablespace block size, status (online, offline, or read-only), contents (undo, temporary, or permanent), extent management (dictionary-managed or locally managed), segment space management (automatic or manual), and whether it is a bigfile or smallfile tablespace.

The DBA_DATA_FILES and DBA_TEMP_FILES views contain information on data files and temp files, respectively, including tablespace name, filename, file size, and autoextend settings.

The RESIZE clause of the ALTER DATABASE DATAFILE command can increase or decrease the size of a data file or temporary file.

Example:

ALTER DATABASE DATAFILE '/disk2/oradata/DB01/appl_data01.dbf' RESIZE 1500M

Taking Files Offline/Online

Data files can be made unavailable to the database using the OFFLINE clause.

Example:

ALTER DATABASE DATAFILE '/u01/oradata/12CR11/tools02.dbf' OFFLINE;

The ONLINE clause brings the data file back online, after which Oracle performs media recovery.

Example:

ALTER DATABASE DATAFILE '/u01/oradata/12CR11/tools02.dbf' ONLINE;

Renaming Files

Data files can be renamed using the RENAME FILE clause of the ALTER DATABASE command or the RENAME DATAFILE clause of the ALTER TABLESPACE command. The MOVE DATAFILE clause can also be used.

Example:

ALTER DATABASE RENAME FILE '/disk1/oradata/DB01/user_data01.dbf', '/disk1/

or

ALTER TABLESPACE USER_DATA RENAME DATAFILE '/disk1/oradata/DB01/user_data

The ALTER DATABASE RENAME FILE syntax does not support online operation.

Oracle Managed Files (OMF)

Benefits of OMF

The Oracle Managed Files (OMF) feature is suitable for databases on disks using Logical Volume Manager (LVM) or Oracle ASM. OMF prevents accidental removal of files belonging to active tablespaces and automatically removes files associated with dropped tablespaces, freeing up disk space. It also allows using the same script to create tablespaces on different OS platforms.

Example:

ALTER DATABASE MOVE DATAFILE 'H:\ORACLE\ORADATA\ORA12\RECEIVABLES02.DBF' T

The KEEP clause can be used to prevent Oracle from deleting the original file.

Example:

ALTER DATABASE MOVE DATAFILE 'H:\ORACLE\ORADATA\ORA12\RECEIVABLES02.DBF' T

Enabling OMF

To enable OMF, set the DB_CREATE_FILE_DEST parameter, which defines the directory where Oracle can create data files. The OMF feature can be used with CREATE DATABASE, CREATE TABLESPACE, and ALTER TABLESPACE statements.

For data files, the format is ora_%t_%u.dbf. For temp files, the format is ora_%t_%u.tmp, where %t is the tablespace name and %u is a unique eight- character string.

The DB_CREATE_ONLINE_LOG_DEST_n parameter (where n can be 1, 2, 3, 4, or 5) specifies the location for control files and redo log files.

Example:

CREATE DATABASE MYDB DEFAULT TEMPORARY TABLESPACE TEMP;

This creates the SYSTEM tablespace data file, the TEMP tablespace temp file, control files, and redo log groups in the directories specified by OMF parameters.

Example:

ALTER SESSION SET DB_CREATE_FILE_DEST = '/ora5/oradata/MYDB'; CREATE TABLESPACE APP_DATA; ALTER SESSION SET DB_CREATE_FILE_DEST = '/ora6/oradata/MYDB'; CREATE TABLESPACE APP_INDEX;

Example:

CREATE TABLESPACE PAY_DATA DATAFILE SIZE 10M AUTOEXTEND OFF;

Example:

CREATE TABLESPACE PAY_INDEX DATAFILE SIZE 20M AUTOEXTEND OFF, SIZE 30M AUT

Example:

ALTER SYSTEM SET DB_CREATE_FILE_DEST = '/ora5/oradata/MYDB'; ALTER TABLESPACE USERS ADD DATAFILE; ALTER SYSTEM SET DB_CREATE_FILE_DEST = '/ora8/oradata/MYDB'; ALTER TABLESPACE APP_DATA ADD DATAFILE SIZE 200M AUTOEXTEND OFF;

ALTER DATABASE DATAFILE 'C:\ORACLE\ORADATA\ORA10\DATA01.DBF' AUTOEXTEND ON

It is important to use MAXSIZE to limit the file size to the available disk space when using AUTOEXTEND.

Space Management Features

Oracle Database 12c offers several space management features:

Using EXTENT MANAGEMENT LOCAL when creating tablespaces. Using SEGMENT SPACE MANAGEMENT AUTO when creating tablespaces. Using AUTOEXTEND ON for data files when creating tablespaces or adding files to tablespaces. Using Oracle Managed Files (OMF) to automate file creation and deletion. Automatic monitoring of free space and generation of alerts based on default thresholds.

Creating Locally Managed Tablespaces

SQL Developer provides the option to create tablespaces as dictionary managed or locally managed.

Automatic Segment Space Management

Segment space management is a configurable option when creating tablespaces. The Segment Space Management setting allows specifying whether Oracle Database 12c should track used and free space in segments using free lists or bitmaps. Use MANUAL for free lists and AUTOMATIC for bitmaps. For bigfile tablespaces, segment management must be automatic.

Compression

Basic compression has been available since Oracle 9i, compressing blocks in batch mode. An example of creating a compressed table is:

CREATE TABLE bookings_oltp COMPRESS FOR OLTP AS SELECT * FROM bookings WHE INSERT INTO bookings_oltp SELECT * FROM bookings;

More compression options are available in Oracle Database 12c.

Monitoring Tablespace Free Space

Tablespace space allocation and usage can be determined using dictionary views such as DBA_DATA_FILES, DBA_TEMP_FILES, DBA_FREE_SPACE, and DBA_SEGMENTS. The following query shows space usage information from data tablespaces:

--Example query would be placed here

When data files are set to AUTOEXTEND, the query needs to be modified to account for this.

Tablespace Monitoring and Alerts

Oracle Database 12c monitors space consumption, alerts administrators about potential problems, and recommends solutions. By default, Oracle monitors tablespace space usage. Oracle Enterprise Manager Cloud Control proactively monitors free space and sends alerts when a space threshold is reached. Server-managed threshold alerts for tablespace space usage are supported for locally managed bigfile and smallfile tablespaces, and for undo and temporary tablespaces, but not for dictionary-managed tablespaces.

Segment Advisor

When large amounts of updates and deletes are performed on tables, fragmented space is created. By shrinking or compressing these segments, space is returned to the database, and the number of I/Os needed to access these objects is reduced.

The Compact Segments And Release Space option will first compact the segments and then release the recovered space to the tablespace. The Compact Segments option will compact segment data without releasing the recovered space. The Segment Shrink option is available only to segments belonging to tablespaces with automatic segment space management.

Space can be manually shrunk in a table, index-organized table, index, partition, subpartition, LOB segment, materialized view, or materialized view log. When rows move, any ROWID-based triggers will fire, so it is better to disable such triggers before performing the shrink space operation on tables. Specifying ALTER INDEX … SHRINK SPACE COMPACT is equivalent to specifying ALTER INDEX … COALESCE.

Resumable Space Allocation

To avoid space-related errors, Oracle Database 12c has the resumable space allocation feature. This feature addresses:

Out-of-space condition (ORA-01653, ORA-01654) Maximum extents reached condition (ORA-01631, ORA-01632) Space quota exceeded condition (ORA-01536)

These conditions are resumable for operations such as:

Queries involving sorting and causing an out-of-space condition in a temporary tablespace DML statements such as INSERT, UPDATE, DELETE, MERGE Import, SQL*Loader data load DDL statements such as CREATE TABLE … AS SELECT, CREATE INDEX, ALTER INDEX … REBUILD, ALTER TABLE … MOVE PARTITION, ALTER TABLE … SPLIT PARTITION, ALTER INDEX … REBUILD PARTITION, ALTER INDEX … SPLIT PARTITION, CREATE MATERIALIZED VIEW, CREATE MATERIALIZED VIEW LOG