Creating tablespaces Changing the size of tablespaces using different methods Changing the status and storage settings of tablespaces Relocating tablespaces Preparing necessary tablespaces Overview Data files Control files Log files Database Overview The database architecture includes the logical and physical structures that make up the database The physical structure consists of control files, online redo log files, and data files The logical database structure includes tablespaces, segments, extents, and data blocks The separation of logical and physical structure provides a fine grained control of disk space management. When logical storage structures are created, space is allocated in the database according to predefined parameters. The database administrator has the ability to override or configure these parameters. To use space efficiently and effectively it is important to understand the logical database structure Logical Database Structure Database Structure Database Tablespace Segment Extent Oracle block Data file O/S block Logical Physical Tablespaces A tablespace can belong to only one database Each tablespace consists of one or more operating system files Tablespaces can be brought online while the database is running Except for the SYSTEM tablespace or a tablespace with an active rollback segment, tablespaces can be taken offline, leaving the database running Tablespaces can be switched between read-write and read-only status Tablespace Uses Controlling space allocation Assigning space quotas to users Controlling availability of data by taking individual tablespaces online or offline Distributing data storage across devices to improve I/O performance and to reduce I/O contention against a single disk Performing partial backup and partial recovery operations Keeping large amounts of static data on read only devices
Data Files Each tablespace in an Oracle database consists of one or more files called data files. These are physical structures that conform with the operating system on which the Oracle server is running.
A data file can belong to only one tablespace An Oracle server creates a data file for a tablespace by allocating the specified amount of disk space plus a small overhead The database administrator can change the size of the data file after its creation or can specify that a data file should dynamically grow as objects in the tablespace grow. Segments A segment is the space allocated for a specific type of logical storage structure within a tablespace. The following are examples of segments:
Table segment Index segment Temporary segment Rollback segment
Note: A segment such as a data segment may span multiple files that belong to the same tablespace. Extents The next level of logical database space is called an extent. An extent is a set of contiguous number of blocks. Each type of segment is made up of one or more extents. An extent may not span a data file, but must exist in one data file. Data Blocks At the finest level of granularity, the data in the Oracle database is stored in data blocks. One data block corresponds to one or more physical file blocks allocated from an existing data file.
Data block size is specified for each Oracle database by the initialization parameter DB_BLOCK_SIZE when the database is created. A database block is the smallest unit of input-output. Types of Tablespaces A database consists of at least one tablespace: the SYSTEM tablespace. Create additional tablespaces in the database for increased control and long term ease of maintenance.
Essentially, the Oracle server perceives two types of tablespaces: SYSTEM and others that are not the SYSTEM tablespace. System and Non-system Tablespaces The SYSTEM tablespace is
Required in all databases for database operation Contains data dictionary information, definitions of stored procedures, packages, and database triggers Contains the SYSTEM rollback segment Should not contain user data although it is allowed
The non- SYSTEM tablespaces
Enable more flexibility in database administration Can store rollback segments, temporary segments, application data, and application indexes Creating Tablespaces SQL> create tablespace app_data datafile /DISK4/app01.dbf size 100m minimum extent 500k default storage(initial 500k next 500k maxextents 500 pctincrease 0);
Where Datafile specifies the data file or files that make up the tablespace Default Storage specifies the default storage parameters for all objects created in the tablespace Minimum Extent ensures that every used extent size in the tablespace is a multiple of the integer value Storage Parameters The following parameters influence the segment storage allocation:
Initial Next Maxextents Minextents PctIncrease
Note: The amount of space used by a segment is determined by the storage clause. These parameters are determined at the creation of the segment. If no specific storage parameters are given in the Create commands of the segment such as Create table then the segment will use the default storage parameters for the tablespace in which it is stored. Storage Parameters INITIAL defines the size of the first extent. The minimum size of the first extent is two blocks, that is (2*DB_BLOCK_SIZE) The default size is five blocks, that is (5*DB_BLOCK_SIZE) NEXT refers to the size of the second extent. The minimum size of the next extent is one block. The default size is five blocks, that is (5*DB_BLOCK_SIZE) MINEXTENTS is the number of extents allocated when the segment is created The minimum and default is 1 PCTINCREASE is the percent by which the extent size grows. The size of an extent is calculated using the following formula: (n-2) Size(n) = NEXT * (1 + PCTINCREASE/100)
Where size(n) is the size of the nth extent Storage Parameters For example, if NEXT is set to 200k, and PCTINCREASE is 50 the second extent would be 200k in size, the third extent would be 300k (50% more than the second extent), the fourth extent would be 450k (50% more than the third extent), and so on. The minimum value is 0, and the default value is 50. Also, the calculated value is rounded up to next data block which is a multiple of 5*DB_BLOCK_SIZE Storage Parameters MAXEXTENTS determines the maximum number of extents that a segment can have. The minimum value is 1. The default maximum value depends on the data block size. The maximum size can also be specified by the keyword UNLIMITED, which is equivalent to a value of 2147483645. Temporary Tablespace Used for sort operations Cannot contain any permanent data
Note: Existing tablespaces can be altered so that they become temporary as long as they do not have any permanent objects in them Changing the Size of Tablespaces You can enlarge a tablespace in two ways:
Add a data file to a tablespace Change the size of a data file Adding Data Files to a tablespace SQL> alter tablespace app_data add datafile DISK5/app02.dbf size 200m;
The tablespace app_data now consists of two data files: app01.dbf and app02.dbf Enabling Automatic Extension of Data Files You can alter the size of a data file in one of two ways: Automatically using the AUTOEXTEND option Manually using the ALTER DATABASE command The Autoextend Clause You can use the autoextend option when adding a datafile:
SQL> alter tablespace app_data add datafile /DISK6/app03.dbf size 200m autoextend on next 10m maxsize 500m;
You can also autoextend an existing datafile:
SQL> alter database datafile /DISK6/app02.dbf autoextend on; Changing the Size of Data Files Manually Instead of adding space to the database by adding data files, the database administrator can change the size of a data file. The database administrator can correct errors in estimates of space requirements and reclaim unused space in the database. Use the following ALTER DATABASE command to manually increase or decrease the size of the data file:
SQL> alter database datafile /DISK5/app02.dbf resize 100m;
Note: The absolute size of the resulting data file is now 100m
Changing the Storage Settings Use the following ALTER TABLESPACE command to alter the default storage default storage definition of a tablespace:
SQL> alter tablespace app_data minimum extent 2m;
SQL> alter tablespace app_data default storage(initial 2m next 2m maxextents 999); OFFLINE Status Users can access a tablespace only if it is online. The database administrator might take a tablespace offline to make: A portion of the database unavailable, while allowing normal access to the remainder of the database Tables or an application unavailable while relocating data files The tablespace that is offline is not available for data access The SYSTEM tablespace and any tablespace with active rollback segments cannot be taken offline
SQL> alter tablespace app_data offline;
SQL> alter tablespace app_data online; The Read-only Tablespace Status Making tablespaces read-only prevents write operations on the data files. The purpose of read-only tablespaces is to ensure that no changes are made and to eliminate the need to perform backup and recovery of large static portions of the database.
SQL> alter tablespace app_data read only;
SQL> alter tablespace app_data read write; Making Tablespace Read-only The tablespace must be online No active transactions are allowed The tablespace must not contain active rollback segments The tablespace must not currently be involved in an online backup Moving Data Files:ALTER TABLESPACE Depending on the type of tablespace, the database administrator can move data files. There are two methods for doing this. The first method uses the alter tablespace command:
1. Take the tablespace offline 2. Use an operating system command to move or copy files 3. Execute the alter tablespace rename datafile command 4. Bring the tablespace online 5. Use an operating system command to delete the file if necessary
Note: For this method to succeed the target data files must exist Moving Data Files: ALTER DATABASE Use the following process to rename files in tablespaces that Cannot be taken offline:
1. Shut down the database 2. Use an operating system command to move the data files 3. Mount the database 4. Execute the ALTER DATABASE RENAME FILE command 5. Open the database
Dropping the Tablespace SQL> drop tablespace app_data;
A read-only tablespace can also be dropped You should take the tablespace offline before dropping it