Partition
About
Partitioning in Oracle is a database design technique that involves dividing a large table, index, or table-partitioned object into smaller, more manageable pieces, called partitions, while retaining it as a single logical entity. Each partition stores a subset of the data and can be managed and accessed individually, which improves performance, manageability, and scalability of large datasets.
Partition pruning in Oracle works effectively when the partition key is included in the WHERE
clause of the query. This ensures that Oracle can identify and access only the relevant partitions instead of scanning the entire table. Without referencing the partition key, Oracle cannot take full advantage of partitioning, and the query may need to scan all partitions.
Key Features
Logical Transparency: To users and applications, a partitioned table appears as a single table.
Partition Independence: Each partition can be accessed and managed individually without affecting others.
Automatic Pruning: Oracle automatically determines which partitions to access based on query conditions, reducing the amount of data scanned.
Partition-Wise Operations: Queries, updates, and maintenance can be parallelized at the partition level.
Data Organization: Data is segmented based on business logic (e.g., time ranges, regions), improving query performance.
Types of Partitioning
Oracle supports several types of partitioning.
1. Range Partitioning
Partitions data based on a range of values in a column (e.g., dates, numeric ranges).
Commonly used for time-series data.
2. List Partitioning
Partitions data based on discrete values.
Suitable for categorical data (e.g., regions, product categories).
3. Hash Partitioning
Distributes rows across partitions using a hash function on a column value.
Useful for evenly distributing data when partition key values are not sequential or range-based.
4. Composite Partitioning
Combines two partitioning strategies, like range and hash or range and list.
Useful for advanced use cases.
5. Interval Partitioning
Extends range partitioning by automatically creating new partitions as data arrives.
Eliminates the need for manually creating partitions.
NUMTOYMINTERVAL
is a built-in Oracle SQL function used to create an interval of time in terms of years or months. It converts a numeric value into an interval datatype that represents either years or months, which is commonly used in partitioning or date/time arithmetic.
Parameters:
n
: A numeric value representing the quantity of the interval.unit
: Specifies the type of interval. It must be one of the following:'YEAR'
: The interval represents a number of years.'MONTH'
: The interval represents a number of months.
Example -
SELECT NUMTOYMINTERVAL(1, 'YEAR') AS year_interval FROM DUAL; //+01-00
SELECT NUMTOYMINTERVAL(6, 'MONTH') AS month_interval FROM DUAL; //+00-06
6. Reference Partitioning
Partitions a table based on the partitioning of a parent table in a foreign key relationship.
Ensures related data is stored in the same partition.
7. System Partitioning
Allows manual assignment of data to partitions without any partition key.
Rarely used as it requires custom logic for data insertion.
Querying a partitioned table
When querying a partitioned table in Oracle, we use a SELECT
statement just as we would with a non-partitioned table. Oracle handles partition pruning and optimization internally, based on the query's filter conditioions. However, to make the most of partitioning, ensure our SELECT
statement aligns with the partitioning column.
Basic Usage
If our table is partitioned by a DATE
column (e.g., record_date
), we can query it like this:
Optimized Select Queries
Use Partition Key in WHERE Clause: To enable partition pruning, include the partition key (e.g.,
record_date
) in ourWHERE
clause:
Query Specific Partitions (Optional): If we know the partition name and want to query it directly, use the
PARTITION
clause:
Avoid Functions on Partition Columns: Using functions on the partitioning column can prevent partition pruning. For example, avoid this:
Instead, rewrite the query to allow pruning:
Dynamic Range Queries: If we need to fetch data for the current month dynamically, we can use Oracle's date functions:
Partition Maintenance
Check the partitions in the database
Dropping a partition
When we drop a partition from a partitioned table, only the partition and its data are removed. The tablespace in which the partition was stored remains intact and can still hold other data, objects, or partitions.
Example: Suppose we have a table with range partitions, and each partition is stored in a different tablespace
If we drop the p_2024
partition:
The data and metadata associated with the
p_2024
partition will be removed.The tablespace
ts_2024
will not be dropped. It remains available for use by other objects or partitions.Once a partition is dropped, the data in that partition is permanently deleted, and reusing the same tablespace does not bring the data back. If we later attach the same tablespace to a new partition, it is treated as a new and empty storage area. The data previously stored in the dropped partition is not retained or restored.
Partition Must Exist for the Data
If the DBA is manually creating new partitions each month, it's important that:
The partition for the current month exists before our service tries to insert records with dates corresponding to that partition.
If the partition is missing, Oracle will throw an error (e.g.,
ORA-14400: inserted partition key does not map to any partition
).
How Tablespaces Work with Partitions?
When we create partitions in a table, we can specify a tablespace for each partition. This allows us to distribute the data for different partitions across multiple tablespaces and physical storage devices.
A tablespace is a logical storage unit in an Oracle database. It is used to organize and manage the physical storage of data. Tablespaces group together related database objects, such as tables, indexes, and partitions, and map them to one or more physical files called datafiles.
A tablespace can contain multiple datafiles, and each datafile is a physical file on the disk.
A database can have multiple tablespaces to separate and organize data logically.
Tablespaces make it easier to manage storage, control disk space usage, and optimize performance.
Specifying Tablespaces for Partitions
When creating a partitioned table, we can assign each partition to a specific tablespace using the TABLESPACE
clause.
Example: Range Partitioning with Tablespaces
Here:
Partition
p_jan2025
is stored in tablespacets_january
.Partition
p_feb2025
is stored in tablespacets_february
.Partition
p_mar2025
is stored in tablespacets_march
.
Example: Hash Partitioning with Tablespaces
For hash-partitioned tables, we can distribute partitions across multiple tablespaces:
Here:
The hash partitions are automatically distributed across the tablespaces
ts_part1
,ts_part2
,ts_part3
, andts_part4
.
Example: Composite Partitioning with Tablespaces
For composite partitioning (e.g., range and list), we can specify tablespaces for subpartitions:
Managing Tablespaces for Partitions
Checking Partition Tablespaces: To see which tablespace a partition is stored in:
Moving Partitions to a New Tablespace: We can move a partition to a different tablespace using
ALTER TABLE
.
Dropping a Tablespace: If a tablespace is no longer needed, ensure it does not contain any partitions before dropping it:
Resizing Tablespace: If a tablespace is running out of space, we can add more datafiles or resize existing ones:
Benefits of Using Tablespaces with Partitions:
Data Distribution: Store different partitions on different disks for better I/O performance.
Storage Management: Allocate specific storage resources for partitions with high data growth.
Backup and Recovery: Manage backups at the tablespace level for specific partitions.
Performance Optimization: Reduce contention and improve query performance by isolating partitions
Best practices to follow for partitions
Partitioning in Oracle databases is a powerful feature that improves performance, manageability, and scalability.
1. Design Partitions Based on Access Patterns
Understand Query Patterns: Partition the table using the column most frequently used in filters or joins (e.g.,
sale_date
for time-based queries).Avoid Over-Partitioning: Keep the number of partitions manageable. Too many partitions can lead to overhead in metadata management.
Example:
For a sales table, if most queries are time-based (e.g., monthly sales), use Range Partitioning on the sale_date
column.
2. Choose the Right Partitioning Strategy
Select the appropriate partitioning type based on your data and requirements:
Range Partitioning: For sequential data, such as dates.
List Partitioning: For specific categories, such as regions or product types.
Hash Partitioning: To distribute data evenly when values are unpredictable (e.g., customer IDs).
Composite Partitioning: Combine strategies (e.g., range and hash) for better granularity.
3. Partition Key Selection
Use a highly selective column (a column with diverse values) as the partition key.
Avoid frequently updated columns as partition keys to minimize maintenance overhead.
Ensure the partition key aligns with your business logic (e.g., partitions by region for regional sales data).
4. Use Tablespaces Effectively
Distribute Partitions Across Tablespaces: Spread partitions across different tablespaces for better I/O performance.
Separate Hot and Cold Data: Use faster storage for current (hot) partitions and slower storage for historical (cold) partitions.
Example:
5. Manage Partition Growth
Plan for future data growth by pre-creating partitions or using interval partitioning.
For example, automatically create monthly partitions:
6. Partition Pruning
Ensure queries benefit from partition pruning by including the partition key in WHERE clauses. This avoids scanning unnecessary partitions.
Example:
Good Query (Uses Partition Pruning):
Bad Query (No Partition Pruning):
7. Use Global and Local Indexes Appropriately
Local Indexes: Partitioned indexes align with table partitions and improve partition-specific queries.
Global Indexes: Use for queries spanning multiple partitions but require additional maintenance.
8. Partition Maintenance
Add/Drop Partitions: Manage historical or new data by adding or dropping partitions as needed.
Merge Small Partitions: Consolidate small partitions to reduce metadata overhead.
9. Monitor Partition Usage
Use
DBA_TAB_PARTITIONS
andDBA_TAB_SUBPARTITIONS
views to monitor partition sizes and data distribution.Regularly check tablespace usage and adjust tablespaces if necessary.
10. Drop Unused Partitions
Remove partitions with outdated or unnecessary data to reclaim storage space:
Benefits of Partitioning
Improved Query Performance: Partition pruning ensures that only the relevant partitions are scanned for queries, reducing I/O overhead.
Scalability: Large datasets are broken into smaller partitions, making them easier to manage.
Enhanced Manageability: Partitions can be added, dropped, or archived individually.
Backup and Recovery: Partitions stored in different tablespaces can be backed up and recovered independently.
Parallel Processing: Enables parallel execution of queries and operations across partitions.
Data Lifecycle Management: Makes it easy to manage "hot" (frequently accessed) and "cold" (historical) data using separate partitions.
Last updated
Was this helpful?