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.
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:
SELECT *
FROM employee;
Oracle needs to access all four partitions because there is no filter condition to eliminate any partition.
3. With Partition Pruning
Now consider this 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.
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.
SELECT *
FROM employee
WHERE join_date = DATE '2024-06-15';
Oracle can identify the relevant partition (P2024) before executing the query.
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.
SELECT *
FROM employee
WHERE join_date = :input_date;
The actual partition is determined using the bind variable value during execution.
5. How to Check Partition Pruning in Oracle?
You can use EXPLAIN PLAN to check whether Oracle is accessing the required partitions.
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.
6. Important Interview Questions
Q1. What is the main benefit of partition pruning?
Q2. Is an index mandatory for partition pruning?
Q3. Does partition pruning work with full table scans?
Q4. Can partition pruning happen with bind variables?
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