What is range-range partitioning

Range-Range Partitioning in Oracle is a type of composite partitioning that combines Range Partitioning at the first level and Range Partitioning at the second level.

In this method:

  1. First, Oracle divides the table into main partitions based on a range of values using Range Partitioning.
  2. Then, each main partition is further divided into smaller subpartitions based on another range of values.
It is useful when we want to organize large amounts of data using two different date or numeric range columns.

1. What is Range-Range Partitioning in Oracle?

Range-Range Partitioning is a composite partitioning technique in Oracle where a table is partitioned using two range-based columns.

  • First level: The first column determines the main (parent) partitions.
  • Second level: The second column determines the subpartitions within each main partition.

In simple words, Oracle first divides the table based on one range column and then further divides each partition based on another range column.

2. Real-Time Example

Suppose we are working on an order management application.

We have an ORDERS table containing millions of records.

The table contains the following columns:

  • ORDER_ID
  • ORDER_DATE
  • SHIP_DATE
  • AMOUNT

We want to divide the data using two conditions:

  • First level: Divide orders year-wise using ORDER_DATE.
  • Second level: Divide each year's orders quarter-wise using SHIP_DATE.

Example: Partition Structure

Main Partition Subpartitions
P2024 Q1, Q2, Q3, Q4
P2025 Q1, Q2, Q3, Q4
Remember:
  • Range Partitioning separates orders based on ORDER_DATE.
  • Range Subpartitioning further separates each year's orders based on SHIP_DATE.

3. SQL Example

Let's create a Range-Range partitioned table in Oracle.

CREATE TABLE orders (
    order_id   NUMBER,
    order_date DATE,
    ship_date  DATE,
    amount     NUMBER
)
PARTITION BY RANGE (order_date)
SUBPARTITION BY RANGE (ship_date)
(
    PARTITION p2024 VALUES LESS THAN
        (DATE '2025-01-01')
    (
        SUBPARTITION p2024_q1 VALUES LESS THAN
            (DATE '2024-04-01'),

        SUBPARTITION p2024_q2 VALUES LESS THAN
            (DATE '2024-07-01'),

        SUBPARTITION p2024_q3 VALUES LESS THAN
            (DATE '2024-10-01'),

        SUBPARTITION p2024_q4 VALUES LESS THAN
            (DATE '2025-01-01')
    ),

    PARTITION p2025 VALUES LESS THAN
        (DATE '2026-01-01')
    (
        SUBPARTITION p2025_q1 VALUES LESS THAN
            (DATE '2025-04-01'),

        SUBPARTITION p2025_q2 VALUES LESS THAN
            (DATE '2025-07-01'),

        SUBPARTITION p2025_q3 VALUES LESS THAN
            (DATE '2025-10-01'),

        SUBPARTITION p2025_q4 VALUES LESS THAN
            (DATE '2026-01-01')
    )
);

4. Explanation of the SQL Query

  • PARTITION BY RANGE (order_date) divides orders based on the order date.
  • SUBPARTITION BY RANGE (ship_date) further divides each main partition based on shipping dates.
  • p2024 stores orders with ORDER_DATE before 01-Jan-2025.
  • p2025 stores orders with ORDER_DATE before 01-Jan-2026 and not already assigned to an earlier partition.
  • Each main partition contains four date-based subpartitions.

Total Number of Subpartitions

2 Main Partitions × 4 Subpartitions = 8 Subpartitions

5. Insert Sample Records

Let's insert some sample records into the orders table.

INSERT INTO orders
VALUES (
    101,
    DATE '2024-01-15',
    DATE '2024-02-15',
    5000
);

INSERT INTO orders
VALUES (
    102,
    DATE '2024-05-10',
    DATE '2024-06-20',
    7000
);

INSERT INTO orders
VALUES (
    103,
    DATE '2024-08-15',
    DATE '2024-09-20',
    8000
);

INSERT INTO orders
VALUES (
    104,
    DATE '2024-11-10',
    DATE '2024-12-15',
    9000
);

COMMIT;

What Happens Internally?

For each inserted record:

  1. Oracle checks the ORDER_DATE value.
  2. It identifies the appropriate main partition.
  3. Oracle checks the SHIP_DATE value.
  4. It stores the record in the appropriate range subpartition within that main partition.

Where Will the Records Be Stored?

ORDER_ID ORDER_DATE SHIP_DATE Stored In
101 15-Jan-2024 15-Feb-2024 P2024_Q1
102 10-May-2024 20-Jun-2024 P2024_Q2
103 15-Aug-2024 20-Sep-2024 P2024_Q3
104 10-Nov-2024 15-Dec-2024 P2024_Q4
Important: Oracle evaluates the subpartition boundaries within the selected parent partition. The example assumes that the shipping dates fall within the corresponding calendar year. Each VALUES LESS THAN boundary is exclusive.

6. How to Check Partitions and Subpartitions?

To Check Main Partitions

SELECT
    TABLE_NAME,
    PARTITION_NAME
FROM USER_TAB_PARTITIONS
WHERE TABLE_NAME = 'ORDERS';

To Check Subpartitions

SELECT
    TABLE_NAME,
    PARTITION_NAME,
    SUBPARTITION_NAME
FROM USER_TAB_SUBPARTITIONS
WHERE TABLE_NAME = 'ORDERS';
Note: USER_TAB_PARTITIONS and USER_TAB_SUBPARTITIONS display partition information for tables owned by the current user.

7. Difference Between Range-Range and Range-List Partitioning

Feature Range-Range Range-List
Main Partition Range Range
Subpartition Range List
Example Year → Quarter Year → Region
Suitable For Date or numeric ranges Range and categories

8. Advantages of Range-Range Partitioning

  1. Organizes data based on two different range columns.
  2. Makes large-table management easier.
  3. Supports partition and subpartition pruning.
  4. Helps retrieve specific date-based records efficiently.
  5. Simplifies data maintenance.
  6. Useful for applications that work with multiple date columns.

9. When Do We Use Range-Range Partitioning?

Range-Range Partitioning is useful when we need to organize large Oracle tables using two date or numeric range columns.

For example:

  • First level: ORDER_DATE (year-wise).
  • Second level: SHIP_DATE (quarter-wise).
Interview Tip: Remember the Concept
  • Range-Range: Year-wise → Quarter-wise.
  • Range-List: Year-wise → Region-wise.
  • List-Range: Department-wise → Year-wise.
  • Range-Hash: Year-wise → Hash distribution.

10. Interview-Ready Answer

Question: What is Range-Range Partitioning in Oracle?

Answer:

"Range-Range Partitioning in Oracle is a composite partitioning technique that combines Range Partitioning at the first level and Range Partitioning at the second level. First, data is divided into main partitions based on a range of values, such as ORDER_DATE (year-wise). Then each main partition is further divided into subpartitions based on another range of values, such as SHIP_DATE (quarter-wise). It helps organize large volumes of data and improves data management."

11. Important Point to Remember

The first partitioning method determines the main partition, and the second method determines the subpartitions.

In Range-Range Partitioning:

  • First Range Partitioning determines the order year.
  • Second Range Partitioning determines the shipping date range within that order year.

Conclusion

Range-Range Partitioning is useful when we need to divide large Oracle tables based on two different date or numeric range columns.

The first Range Partitioning handles the main partition, while the second Range Partitioning handles the subpartition.

Understanding this concept is important for Oracle Database Developers and DBAs, especially when working with large tables.

No comments:

Post a Comment