What is partition pruning

Partition Pruning in Oracle is an optimization technique in which Oracle accesses only the required partitions instead of scanning the entire partitioned table.

In simple words, Oracle skips unnecessary partitions and scans only the partitions that contain the required data.

This improves query performance and reduces I/O operations.

1. Example: Create a Partitioned Table

Let's create an employee table partitioned by JOIN_DATE.

SQL - Create Partitioned Table
CREATE TABLE employee (
    emp_id     NUMBER,
    emp_name   VARCHAR2(100),
    salary     NUMBER,
    join_date  DATE
)
PARTITION BY RANGE (join_date)
(
    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')
);

Here, data is divided into four partitions:

Partition Data Range
P2022 Before 2023-01-01
P2023 2023 calendar year
P2024 2024 calendar year
P2025 2025 calendar year

2. Without Partition Pruning

Consider this query:

SQL Query
SELECT *
FROM employee;

Oracle needs to access all four partitions because there is no filter condition to eliminate any partition.

All partitions scanned
P2022 2022 Data SCANNED
P2023 2023 Data SCANNED
P2024 2024 Data SCANNED
P2025 2025 Data SCANNED

3. With Partition Pruning

Now consider this query:

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

What happens?

Oracle identifies that the required data exists in the P2024 partition.

Therefore, Oracle accesses only P2024 and skips the other partitions.

Partition Pruning Visualization
P2022 2022 Data ✕ SKIPPED
P2023 2023 Data ✕ SKIPPED
P2024 2024 Data ✓ SCANNED
P2025 2025 Data ✕ SKIPPED
Only 1 out of 4 partitions accessed.
This is called Partition Pruning.

4. Types of Partition Pruning

Oracle supports two major types:

A. Static Partition Pruning

Oracle identifies the required partitions at SQL parse time because the partition key value is known.

Example - Static Partition Pruning
SELECT *
FROM employee
WHERE join_date = DATE '2024-06-15';

Oracle can identify the relevant partition (P2024) before executing the query.

Key Point: The partition key value is known at parse time.

B. Dynamic Partition Pruning

Oracle determines which partitions to access during execution.

This can happen when partition key values are obtained dynamically, for example, from a bind variable or a subquery.

Example - Dynamic Partition Pruning
SELECT *
FROM employee
WHERE join_date = :input_date;

The actual partition is determined using the bind variable value during execution.

Key Point: The partition is determined dynamically during execution.

5. How to Check Partition Pruning in Oracle?

You can use EXPLAIN PLAN to check whether Oracle is accessing the required partitions.

SQL - Explain Plan
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);

Important Execution Plan Operations

Operation Meaning
PARTITION RANGE SINGLE Only one partition accessed.
PARTITION RANGE ITERATOR A range of partitions accessed.
PARTITION RANGE ALL All partitions accessed.

Also check PSTART and PSTOP columns in the execution plan to identify which partitions Oracle accesses.

Important: PSTART and PSTOP help identify the starting and ending partition boundaries. For dynamic pruning, you may see KEY values in the execution plan.

6. Important Interview Questions

Q1. What is the main benefit of partition pruning?
It reduces unnecessary partition scans, I/O operations and query execution time.
Q2. Is an index mandatory for partition pruning?
No. Partition pruning can happen even without an index.
Q3. Does partition pruning work with full table scans?
Yes. Oracle can perform a full table scan on only the required partition.
Q4. Can partition pruning happen with bind variables?
Yes. Oracle can perform dynamic partition pruning using bind variable values.

Interview Takeaway

Partition Pruning = Oracle scans only the required partitions and skips unnecessary partitions based on the partition key condition.

It is one of the most important performance benefits of partitioning in Oracle.

If you want, I can also demonstrate Static vs Dynamic Partition Pruning with actual Oracle execution plans and sample output.

No comments:

Post a Comment