What is range partitioning

Range Partitioning in Oracle is a partitioning technique in which a large table is divided into smaller partitions based on a range of values in a particular column.

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.

Real-time scenario:
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.
  • p2024 stores records with dates before 01-Jan-2025.
  • p2025 stores records from 01-Jan-2025 to before 01-Jan-2026.
  • p2026 stores records from 01-Jan-2026 to before 01-Jan-2027.
Important: VALUES LESS THAN specifies an exclusive upper boundary. For example, DATE '2025-01-01' means all dates before 01-Jan-2025.

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.

Interview Tip:
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

  1. Improves query performance through partition pruning.
  2. Easy management of historical data.
  3. Easy deletion or removal of old data.
  4. Useful for date-based transaction tables.
  5. Simplifies data archival.
  6. 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:

Oracle returns an error because no partition exists for the date 2027-05-10.

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.

Additional Interview Point:
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.

ORACLE SQL | DATABASE PARTITIONING | ORACLE INTERVIEW QUESTIONS

No comments:

Post a Comment