0% found this document useful (0 votes)
5 views

Managing+Tablespaces

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Managing+Tablespaces

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 32

Managing Tablespaces

By Ahmed Baraka

Oracle Database Administration from Zero to Hero - a course by Ahmed Baraka


Objectives
In this lecture, you will learn how to perform the following:
• Describe tablespace types
• Create permanent tablespaces
• Describe and enable Oracle-managed Files (OMF)
• Obtain information about tablespaces
• Enlarge tablespaces
• Drop tablespaces
• Create or move tables into tablespaces
• Describe and create Bigfile tablespaces
• Alter Tablespace availability
• Make tablespaces read-only or read-write
• Assign specific quota on tablespaces to users
Oracle Database Administration from Zero to Hero - a course by Ahmed Baraka
Logical and Physical Database Structures

Logical Physical

Database

Tablespace Data file

Segment

Extent

data
OS block
block

Oracle Database Administration from Zero to Hero - a course by Ahmed Baraka


About Tablespaces
• A logical group of datafiles
• Tablespace types:
- SYSTEM: basic to the functioning of the database server
- SYSAUX: schemas used by various database components and features
- Undo: contains undo records used for recovery and read consistency
- Temporary: transient data that persists only for the duration of the session,
such as session sort operations that do not fit into the memory
- Permanent (User): hosts application data

• Classification based on extent management:


- Dictionary managed tablespaces (deprecated)
- Locally managed tablespaces

Oracle Database Administration from Zero to Hero - a course by Ahmed Baraka


Creating Permanent Tablespaces
• A simplified syntax:
CREATE TABLESPACE <tablespace-name>
[DATAFILE ['datafile fullpath'] [SIZE <size>] [REUSE]
[AUTOEXTEND OFF | ON [NEXT <size>] [MAXSIZE UNLIMITED | <size>] ]
[EXTENT MANAGEMENT LOCAL [AUTOALLOCATE | UNIFORM SIZE <size>]]
[SEGMENT SPACE MANAGEMENT AUTO | MANUAL]

- Default datafile size: 100M


- AUTOEXTEND default: ON for OMF and OFF for user-managed
- Default NEXT: the original datafile size or 100M whichever is smaller
• The provides syntax defines: datafile attributes, extent management
attributes, and space management attributes.
• CREATE TABLESPACE system privilege is required
Oracle Database Administration from Zero to Hero - a course by Ahmed Baraka
Maximum Possible Datafile Sizes for Data Block Sizes

Data Block Size Maximum Datafile Size


2K 8G

4K 16G
8K 32G
16K 64G
32K 128G

Oracle Database Administration from Zero to Hero - a course by Ahmed Baraka


Specifying Datafile Attribute Examples

CREATE TABLESPACE lmtbs DATAFILE '/u02/oracle/data/payrolltbs01.dbf'


SIZE 50M AUTOEXTEND ON NEXT 10M MAXSIZE 32G;

CREATE TABLESPACE lmtbs DATAFILE '/u02/oracle/data/statictbs01.dbf'


SIZE 10G AUTOEXTEND OFF;

Oracle Database Administration from Zero to Hero - a course by Ahmed Baraka


Specifying Extent Management Options
• A simplified syntax:
Option Description
AUTOALLOCATE Extent sizes are automatically determined by the database
UNIFORM SIZE <size> extents are created with uniform size (default 1 M)

• A tablespace with minimum extent size of 64K:


CREATE TABLESPACE lmtbsb DATAFILE '/u02/oracle/data/lmtbsb01.dbf'
SIZE 50M EXTENT MANAGEMENT LOCAL AUTOALLOCATE;

• A tablespace with uniform 128K extents:


CREATE TABLESPACE lmtbsb DATAFILE '/u02/oracle/data/lmtbsb01.dbf'
SIZE 50M EXTENT MANAGEMENT LOCAL UNIFORM SIZE 128K;

Oracle Database Administration from Zero to Hero - a course by Ahmed Baraka


Specifying Segment Space Management
CREATE TABLESPACE <tbs-name>
..
[SEGMENT SPACE MANAGEMENT AUTO | MANUAL]

• Options:
- Automatic: uses bitmaps to manage free space in the segment (this is
called automatic segment space management or ASSM)
- Manual: uses linked lists called "freelists" to manage free space in the
segment
• Automatic is the default and more efficient

Oracle Database Administration from Zero to Hero - a course by Ahmed Baraka


About Oracle-Managed Files (OMF)
• A set of options that controls the default locations for specific database
files like: tablespace datafiles, redo log files, archived log files,..etc.
• We can have a mixture of OMF and non-OMF database files
• File names are automatically set
• Default datafile size: 100M, auto-extesible
Parameter Description
DB_CREATE_FILE_DEST Defines the location of the default file system directory for data
files and temporary files (SYSTEM and SESSION modifiable)
DB_CREATE_ONLINE_LOG_DEST_n Defines the location for redo log files and control file creation
DB_RECOVERY_FILE_DEST Default location for the fast recovery area (FRA)

Oracle Database Administration from Zero to Hero - a course by Ahmed Baraka


Enabling OMF in dbca

Oracle Database Administration from Zero to Hero - a course by Ahmed Baraka


Creating Permanent Tablespace with OMF
• Enable OMF:
ALTER SYSTEM SET DB_CREATE_FILE_DEST = '/u01/app/oracle/oradata';
ALTER SESSION SET DB_CREATE_FILE_DEST = '/u01/app/oracle/oradata';

• Create tablespaces:
CREATE TABLESPACE hrtbs ;
CREATE TABLESPACE hrtbs SIZE 1G;

• For CDB, datafiles are created in the following directory path format:
<OMF>/<CDB DB name>/<pdb GUID>/datafile/<datafile name>.dbf
<OMF>/<$ORACLE_SID>/datafile/<datafile name>.dbf

Oracle Database Administration from Zero to Hero - a course by Ahmed Baraka


Obtaining Information About Tablespace and Datafiles

Clause Description
DBA_TABLESPACES Describes all tablespaces in the database.
V$TABLESPACE Displays tablespace information from the control file.
DBA_DATA_FILES Describes all the data files.
V$DATAFILE Displays datafile information from the control file.
DBA_TEMP_FILES Describes all temporary files (tempfiles) in the database.
V$TEMPFILE Displays temp file information.

Oracle Database Administration from Zero to Hero - a course by Ahmed Baraka


Obtaining Information About Tablespace Used and
Free Space

SELECT F.TABLESPACE_NAME "Tablespace Name"


,F.TOTALSPACE "Size MB"
, (F.TOTALSPACE - U.TOTALUSEDSPACE) "Free MB"
,ROUND (100 * ( (F.TOTALSPACE - U.TOTALUSEDSPACE) / F.TOTALSPACE)) || '%' "Free %"
,T.MAX_S "Max Size"
FROM (SELECT TABLESPACE_NAME, ROUND (SUM (BYTES) / (1024 * 1024)) TOTALSPACE
FROM DBA_DATA_FILES
GROUP BY TABLESPACE_NAME) F
,(SELECT TABLESPACE_NAME, ROUND (SUM (BYTES) / (1024 * 1024)) TOTALUSEDSPACE
FROM DBA_SEGMENTS
GROUP BY TABLESPACE_NAME) U
,( SELECT TABLESPACE_NAME, ROUND (MAX_SIZE / (1024 * 1024)) MAX_S
FROM DBA_TABLESPACES) T
WHERE F.TABLESPACE_NAME = U.TABLESPACE_NAME(+)
AND F.TABLESPACE_NAME = T.TABLESPACE_NAME;

Oracle Database Administration from Zero to Hero - a course by Ahmed Baraka


Obtaining Information About Tablespaces Space
Usage in SQL Developer
• Right-click on the connection node > select Manage Database
- The free space is calculated based on the current tablespace SIZE and not
the MAXSIZE

Oracle Database Administration from Zero to Hero - a course by Ahmed Baraka


Obtaining Information About Tablespaces in EM
Express
• In the Home page > Performance > Performance Hub > Storage >
Tablespace

Oracle Database Administration from Zero to Hero - a course by Ahmed Baraka


Enlarging the Database
• Creating a new tablespace
• Adding a data file to an existing tablespace:
ALTER TABLESPACE hrbsb ADD DATAFILE;

ALTER TABLESPACE hrbsb ADD DATAFILE


'/u02/oracle/data/payrolltbs02.dbf'
SIZE 50M AUTOEXTEND ON NEXT 10M MAXSIZE 32G;

• Increasing the size of a data file


ALTER DATABASE DATAFILE 15 RESIZE 10240M;

• Enable the dynamic growth of a data file

Oracle Database Administration from Zero to Hero - a course by Ahmed Baraka


Dropping Tablespaces
• Drop a tablespace, if the tablespace and its contents are no longer
required:
DROP TABLESPACE <tbs name> [INCLUDING CONTENTS [AND DATAFILES ]]

• DROP TABLESPACE system privilege is required

Oracle Database Administration from Zero to Hero - a course by Ahmed Baraka


Creating Tables in Tablespaces
• By default, tables are created in the owner’s default tablespace
• Tables can be created in specific tablespaces (assuming the user has
the required privileges on the target tablespace):
CREATE TABLE EMPLOYEES ( ... ) TABLESPACE HRTBS;

• An existing table can be moved to a different tablespace:


- The new table might take less space than the original table
ALTER TABLE EMPLOYEES MOVE TABLESPACE HRTBS;

• To know which tablespace is table is saved in:


SELECT TABLESPACE_NAME FROM USER_TABLES WHERE TABLE_NAME='EMPLOYEES';

Oracle Database Administration from Zero to Hero - a course by Ahmed Baraka


About Bigfile Tablespaces
• A permanent tablespace that is linked to
Tablespace
a single datafile (usually very large).
- For example: A bigfile tablespace with 8K blocks
can contain a 32-terabyte data file Data file
• Benefits:
- Reduce number of datafiles for a database
- Simplify managing so many datafiles
• The underlying filesystem should be able to accommodate the file size
- Supported by logical volume managers that support stripping, like ASM
• The other tablespace type is called small tablespace.

Oracle Database Administration from Zero to Hero - a course by Ahmed Baraka


Creating a Bigfile Tablespace
• Use the corresponding keyword:
CREATE [BIGFILE | SMALLFILE] TABLESPACE <tbs name> ...;

• The default tablespace type is set at database creation


• The AUTOEXTEND is by default turned off but you can enable it
• Can be identified by the column BIGFILE in the data dictionary views:
*_TABLESPACES and V$TABLESPACE
• Example:
CREATE BIGFILE TABLESPACE bigtbs
DATAFILE '/u02/oracle/data/bigtbs01.dbf' SIZE 500G;

Oracle Database Administration from Zero to Hero - a course by Ahmed Baraka


Supporting Bigfile Tablespaces During Database
Creation

Oracle Database Administration from Zero to Hero - a course by Ahmed Baraka


Supporting Bigfile Tablespaces During Database
Creation

Oracle Database Administration from Zero to Hero - a course by Ahmed Baraka


Specifying the Default Tablespace Type
• When creating the database manually:
CREATE DATABASE ... SET DEFAULT [ BIGFILE | SMALLFILE ] TABLESPACE

• To change it after database creation:


ALTER DATABASE SET DEFAULT BIGFILE TABLESPACE;

• Determine the current default tablespace type for the database:


SELECT PROPERTY_VALUE
FROM DATABASE_PROPERTIES
WHERE PROPERTY_NAME = 'DEFAULT_TBS_TYPE';

Oracle Database Administration from Zero to Hero - a course by Ahmed Baraka


Using Bigfile Tablespaces Guide
• Consider it when:
- The database size is in multi-TB
- The underlying volume manager supports stripping, like Oracle ASM

Oracle Database Administration from Zero to Hero - a course by Ahmed Baraka


Altering Tablespace Availability
• When a tablespace is taken offline, it becomes unavailable for general
use
• We might need to make a tablespace offline:
- To rename or relocate tablespace data file
- To perform an offline tablespace backup

• You cannot take the following tablespaces offline:


- SYSTEM
- The undo tablespace
- Temporary tablespaces

Oracle Database Administration from Zero to Hero - a course by Ahmed Baraka


Altering Tablespace Availability
• To take a tablespace offline:
- The database must be open
ALTER TABLESPACE <tbs-name> OFFLINE [NORMAL | TEMPORARY | IMMEDIATE]

Clause Description
NORMAL The database takes a checkpoint for all data files of the tablespace (clean)
TEMPORARY The database takes offline the data files that are not already offline. The
tablespace may require recovery before you can bring it back online.
IMMEDIATE No checkpoint is taken on any data file. Media recovery for the tablespace is
required before the tablespace can be brought online

• To take a tablespace online:


ALTER TABLESPACE <tbs-name> ONLINE;

Oracle Database Administration from Zero to Hero - a course by Ahmed Baraka


Making Tablespaces Read-Only or Read-Write
• To make a tablespace read-only:
ALTER TABLESPACE mytbs READ ONLY;

• To make a tablespace writable:


ALTER TABLESPACE mytbs READ WRITE;

• To check if a tablespace is set to read-only operations, query the


STATUS column in the view DBA_TABLESPACES

Oracle Database Administration from Zero to Hero - a course by Ahmed Baraka


Assigning Specific Quota on Tablespaces to Users
• The tablespace quota defines how much space a user may take
• Can be set at the time of creating the users:
CREATE USER scott IDENTIFIED BY password
DEFAULT TABLESPACE data_ts
QUOTA 100M ON data_ts
QUOTA 10M ON index_ts ...

• Can be altered for existing users:


ALTER USER user1 QUOTA UNLIMITED ON data_tbs;

• Quota is needed even for default tablespace.


• If the quota is exceeded, the following error is returned:
ORA-1536 space quota exceeded for tables

Oracle Database Administration from Zero to Hero - a course by Ahmed Baraka


About the UNLIMITED TABLESPACE Privilege
• To permit a user to use an unlimited amount of any tablespace in the
database
- The privilege overrides all explicit tablespace quotas for the user
- We cannot selectively revoke tablespace access from a user
• It is better to use the QUOTA UNLIMITED option.

Oracle Database Administration from Zero to Hero - a course by Ahmed Baraka


Managing Tablespace Guidelines
• Put application data and indexes in separate tablespaces
• Put static or read-mostly data in a separate tablespace
• Do not create user objects in the SYSTEM or SYSAUX tablespaces
• The underlying storage should support redundancy and fault-tolerance
• You must always monitor the tablespace space usage
Note: Some advanced tablespace settings have not been discussed in
this lecture.

Oracle Database Administration from Zero to Hero - a course by Ahmed Baraka


Summary
In this lecture, you should have learnt how to perform the following:
• Describe tablespace types
• Create permanent tablespaces
• Describe and enable Oracle-managed Files (OMF)
• Obtain information about tablespaces
• Enlarge tablespaces
• Drop tablespaces
• Create or move tables into tablespaces
• Describe and create Bigfile tablespaces
• Alter Tablespace availability
• Make tablespaces read-only or read-write
• Assign specific quota on tablespaces to users
Oracle Database Administration from Zero to Hero - a course by Ahmed Baraka

You might also like