Partition pruning behavior in PostgreSQL - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
When using partitioned tables, the database can skip checking some parts to speed up queries.
We want to see how this skipping affects the work done as data grows.
Analyze the time complexity of the following query on a partitioned table.
SELECT * FROM sales
WHERE sale_date = '2024-01-01';
-- sales is partitioned by sale_date
-- partitions are by month
This query fetches sales for one day, using partitions by month to limit data scanned.
Look for repeated work done by the query.
- Primary operation: Scanning partitions that match the date condition.
- How many times: Only the partition for January 2024 is scanned, not all partitions.
As the total number of partitions grows, the query only checks the relevant ones.
| Input Size (partitions) | Approx. Partitions Scanned |
|---|---|
| 10 | 1 |
| 100 | 1 |
| 1000 | 1 |
Pattern observation: The number of partitions scanned stays the same, no matter how many partitions exist.
Time Complexity: O(1)
This means the query work stays constant because it only looks at the needed partitions.
[X] Wrong: "The query scans all partitions every time, so it gets slower as partitions grow."
[OK] Correct: Partition pruning lets the database skip irrelevant partitions, so it does not scan all partitions.
Understanding partition pruning shows you know how databases handle big data efficiently, a useful skill for real projects.
"What if the query uses a condition that does not match the partition key? How would the time complexity change?"
Practice
Solution
Step 1: Understand partition pruning concept
Partition pruning means the database avoids scanning partitions that do not match the query filter.Step 2: Identify the main benefit
This skipping reduces query time by focusing only on relevant partitions.Final Answer:
To skip scanning partitions that cannot contain matching rows -> Option CQuick Check:
Partition pruning = skip irrelevant partitions [OK]
- Thinking pruning merges partitions
- Assuming pruning creates partitions
- Confusing pruning with backup
region?Solution
Step 1: Identify pruning conditions
Partition pruning works best with simple direct comparisons on the partition key.Step 2: Analyze each option
WHERE region = 'US' uses a direct equality onregion, enabling pruning. Options A, B, and D use functions or patterns, preventing pruning.Final Answer:
WHERE region = 'US' -> Option AQuick Check:
Simple equality on partition key enables pruning [OK]
- Using functions on partition keys disables pruning
- Using LIKE patterns disables pruning
- Assuming any WHERE clause prunes partitions
sales partitioned by year with partitions for 2021 and 2022, what will the query below scan?SELECT * FROM sales WHERE year = 2021;
Solution
Step 1: Understand partition pruning with equality filter
The query filters onyear = 2021, which matches exactly one partition.Step 2: Determine scanned partitions
PostgreSQL will prune and scan only the 2021 partition, skipping 2022.Final Answer:
Only the 2021 partition -> Option AQuick Check:
Filter on partition key = value scans matching partition only [OK]
- Assuming all partitions scan regardless of filter
- Thinking query returns empty if filter matches a partition
- Believing full table scan always happens
orders partitioned by order_date:SELECT * FROM orders WHERE EXTRACT(YEAR FROM order_date) = 2023;
Why might partition pruning NOT occur?
Solution
Step 1: Identify pruning limitation
Partition pruning requires direct use of the partition key in filters without wrapping functions.Step 2: Analyze the query
The query usesEXTRACT(YEAR FROM order_date), a function on the partition key, preventing pruning.Final Answer:
Using a function on the partition key disables pruning -> Option BQuick Check:
Functions on partition keys disable pruning [OK]
- Thinking pruning needs indexes
- Believing pruning works with any filter
- Assuming query syntax error causes pruning failure
events is range partitioned by event_date with monthly partitions. You want to query events in March 2023. Which query will maximize partition pruning?Solution
Step 1: Understand pruning with range partitions
Range partitions work best with direct range conditions on the partition key.Step 2: Evaluate each query
SELECT * FROM events WHERE event_date >= '2023-03-01' AND event_date < '2023-04-01'; uses a direct range filter onevent_date, enabling pruning. Options B and C use functions, disabling pruning. SELECT * FROM events WHERE event_date BETWEEN '2023-02-28' AND '2023-03-31'; includes dates outside March, scanning extra partitions.Final Answer:
SELECT * FROM events WHERE event_date >= '2023-03-01' AND event_date < '2023-04-01'; -> Option DQuick Check:
Direct range filters maximize pruning [OK]
- Using functions disables pruning
- Including extra dates scans more partitions
- Assuming BETWEEN always prunes perfectly
