Generally, we use date columns or numeric columns for range partitioning.
For example, we can divide an employee transaction table based on transaction year, such as 2024, 2025, and 2026.
1. Real-time Example
Suppose we have an EMPLOYEE_TRANSACTIONS table containing 10 crore records.
We want to divide the data year-wise based on the transaction date.
Consider a banking application that stores millions of transaction records. Instead of maintaining all records in one large table partition, we can divide the data year-wise using Range Partitioning.
| Partition Name | Data Stored |
|---|---|
| P2024 | Records before 01-Jan-2025 |
| P2025 | Records from 01-Jan-2025 to 31-Dec-2025 |
| P2026 | Records from 01-Jan-2026 to 31-Dec-2026 |
2. SQL Example – Create Range Partitioned Table
Let's create a range-partitioned table using the transaction_date column.
CREATE TABLE employee_transactions
(
transaction_id NUMBER,
employee_name VARCHAR2(100),
transaction_date DATE,
amount NUMBER
)
PARTITION BY RANGE (transaction_date)
(
PARTITION p2024 VALUES LESS THAN
(DATE '2025-01-01'),
PARTITION p2025 VALUES LESS THAN
(DATE '2026-01-01'),
PARTITION p2026 VALUES LESS THAN
(DATE '2027-01-01')
);
Explanation:
PARTITION BY RANGE (transaction_date)specifies the column used for partitioning.p2024stores records with dates before 01-Jan-2025.p2025stores records from 01-Jan-2025 to before 01-Jan-2026.p2026stores records from 01-Jan-2026 to before 01-Jan-2027.
3. Insert Sample Records
Let's insert some sample records into the employee_transactions table.
INSERT INTO employee_transactions
VALUES (101, 'Ravi', DATE '2024-05-10', 5000);
INSERT INTO employee_transactions
VALUES (102, 'Kumar', DATE '2025-06-15', 7000);
INSERT INTO employee_transactions
VALUES (103, 'Raj', DATE '2026-03-20', 9000);
COMMIT;
Oracle automatically stores the records in the appropriate partitions based on the transaction_date column.
| Transaction ID | Employee Name | Transaction Date | Partition |
|---|---|---|---|
| 101 | Ravi | 10-May-2024 | P2024 |
| 102 | Kumar | 15-Jun-2025 | P2025 |
| 103 | Raj | 20-Mar-2026 | P2026 |
4. How to Check Partition Data?
To check the partitions available in the table, execute the following query:
SELECT
TABLE_NAME,
PARTITION_NAME,
HIGH_VALUE
FROM USER_TAB_PARTITIONS
WHERE TABLE_NAME = 'EMPLOYEE_TRANSACTIONS';
To retrieve data from a particular partition:
SELECT *
FROM employee_transactions
PARTITION (p2025);
This query retrieves records stored in the P2025 partition.
Partition pruning is an Oracle optimization technique that eliminates unnecessary partitions when executing a query. This can improve query performance by accessing only the relevant partitions.
5. Advantages of Range Partitioning
- Improves query performance through partition pruning.
- Easy management of historical data.
- Easy deletion or removal of old data.
- Useful for date-based transaction tables.
- Simplifies data archival.
- Helps manage large database tables efficiently.
6. Interview-Ready Answer
Question: What is Range Partitioning in Oracle?
Answer:
Range partitioning in Oracle is a technique used to divide a large table into smaller partitions based on a range of values in a particular column. For example, we can partition a transaction table year-wise based on the transaction date. Oracle automatically stores records in the appropriate partitions based on the specified range boundaries. It helps improve query performance through partition pruning and makes historical data management easier.
7. Important Oracle Interview Question
Question: What happens if we insert a record with a date of 2027-05-10 into the above table?
INSERT INTO employee_transactions
VALUES (104, 'Suresh', DATE '2027-05-10', 10000);
Answer:
We need to add another partition to store 2027 records.
ALTER TABLE employee_transactions
ADD PARTITION p2027
VALUES LESS THAN (DATE '2028-01-01');
After adding the new partition, we can insert the 2027 record successfully.
Instead of manually adding partitions every year, we can also use Oracle Interval Partitioning to automatically create new partitions for future date ranges.
Conclusion
Range partitioning is one of the most commonly used partitioning techniques in Oracle Database. It is especially useful for large transaction tables where data is maintained based on dates or numeric ranges.
Understanding range partitioning, partition pruning, and partition maintenance operations is important for Oracle SQL and PL/SQL developer interviews.
No comments:
Post a Comment