Partition Pruning is an important Oracle Database optimization technique that improves query performance by reducing the number of partitions Oracle needs to scan to retrieve the required data.

Instead of scanning the entire partitioned table, Oracle accesses only the relevant partitions based on the query's filter conditions.

This helps reduce:

  • Disk I/O operations.
  • Data blocks read from storage.
  • CPU usage.
  • Query execution time.
  • Overall resource consumption.

Let's understand how partition pruning works with a practical Oracle SQL example.

1. Create a Partitioned Table

Suppose we have an employee table containing 10 million records, partitioned by year using the JOIN_DATE column.

CREATE TABLE employee (
    emp_id     NUMBER,
    emp_name   VARCHAR2(100),
    salary     NUMBER,
    join_date  DATE
)
PARTITION BY RANGE (join_date)
(
    PARTITION p2021 VALUES LESS THAN (DATE '2022-01-01'),
    PARTITION p2022 VALUES LESS THAN (DATE '2023-01-01'),
    PARTITION p2023 VALUES LESS THAN (DATE '2024-01-01'),
    PARTITION p2024 VALUES LESS THAN (DATE '2025-01-01'),
    PARTITION p2025 VALUES LESS THAN (DATE '2026-01-01')
);

Assume the data is distributed equally across all five partitions.

Partition Records
P202120 lakh
P202220 lakh
P202320 lakh
P202420 lakh
P202520 lakh
Total1 crore

2. Query Without Partition Pruning

Consider the following SQL query:

SELECT *
FROM employee
WHERE TO_CHAR(join_date, 'YYYY') = '2024';

Here, we are applying the TO_CHAR() function to the partition key JOIN_DATE.

Important: Because the partition key is wrapped inside a function, Oracle may not be able to derive the required partition range from this predicate. Consequently, it may scan all five partitions.

In this scenario, Oracle may examine up to 10 million records.

5 Partitions All partitions may be scanned.

10 million records

3. Query With Partition Pruning

Now rewrite the query using a date range condition.

SELECT *
FROM employee
WHERE join_date >= DATE '2024-01-01'
AND join_date < DATE '2025-01-01';

Now Oracle can identify that the required records belong to the P2024 partition.

Partition Pruning: Oracle can eliminate the unnecessary partitions and access only P2024.

Instead of scanning all five partitions, Oracle accesses only one relevant partition.

10 Million Without partition pruning

5 partitions

2 Million With partition pruning

1 partition

In this example, the number of records to examine is reduced by approximately 80%, assuming equal data distribution and a full scan of the selected partitions.

Note: This is an illustrative comparison. Actual disk I/O, blocks read, and execution time depend on the execution plan, indexes, data distribution, and storage characteristics.

4. How Does Oracle Achieve Partition Pruning Internally?

When Oracle receives a query, it performs the following steps:

  1. Checks the WHERE clause conditions.
  2. Compares the conditions with the table's partition key.
  3. Identifies the relevant partition or partitions.
  4. Eliminates unnecessary partitions from the scan.
  5. Reads data from the selected partitions.

For example, consider this query condition:

WHERE join_date >= DATE '2024-01-01'
  AND join_date < DATE '2025-01-01'

Oracle identifies the partition selection as follows:

P2021SKIP
P2022SKIP
P2023SKIP
P2024READ ✓
P2025SKIP
Result: Oracle reads only P2024 and skips the other four partitions.

5. How to Verify Partition Pruning?

We can verify partition pruning using Oracle's EXPLAIN PLAN and DBMS_XPLAN.DISPLAY.

Execute the following SQL statements:

EXPLAIN PLAN FOR
SELECT *
FROM employee
WHERE join_date >= DATE '2024-01-01'
AND join_date < DATE '2025-01-01';

SELECT *
FROM TABLE(DBMS_XPLAN.DISPLAY);

Look for the following operations in the execution plan:

Operation Meaning
PARTITION RANGE SINGLE One partition accessed.
PARTITION RANGE ITERATOR Multiple selected partitions accessed.
PARTITION RANGE ALL All partitions accessed.

Also check the PSTART and PSTOP columns in the execution plan.

  • PSTART: Starting partition selected by Oracle.
  • PSTOP: Ending partition selected by Oracle.
Interview Tip: For a single partition access, you may see PARTITION RANGE SINGLE. For multiple partitions, you may see PARTITION RANGE ITERATOR. The actual operation depends on the query and partitioning strategy.

6. Important Oracle Partition Pruning Interview Questions

Q1. Does Partition Pruning Always Improve Performance?

Answer: No, not always.

Partition pruning may provide little or no benefit in the following scenarios:

  • If the query needs data from all partitions, pruning cannot eliminate partitions.
  • If the query filters on a non-partitioning column, Oracle may not be able to prune partitions.
  • If the query retrieves only a small number of rows, an appropriate index may be more efficient than a full partition scan.

Q2. Do We Need an Index for Partition Pruning?

Answer: No.

An index is not mandatory for partition pruning. Oracle can perform partition pruning even when using a full table scan.

Partition pruning identifies which partitions need to be accessed, while indexes help locate rows within the accessed data.

Q3. What Is the Difference Between Partition Pruning and Index Range Scan?

Partition pruning eliminates unnecessary partitions, whereas an index range scan retrieves qualifying rows through an index.

Both techniques can work together to improve query performance.

Final Takeaway

Remember This Simple Example

  • Without partition pruning: Oracle may scan 10 million records.
  • With partition pruning: Oracle scans only the relevant 2 million records.
  • Result: Less I/O, fewer blocks read, and potentially faster query execution.

Partitioning + Partition Pruning = Reduced Data Scanning + Potentially Better Query Performance.

Understanding partition pruning is essential for every Oracle DBA and SQL developer because it helps in identifying unnecessary partition scans and optimizing SQL queries.

Next Topic: Partition Pruning vs Index Range Scan in Oracle.

No comments:

Post a Comment