The symptom
Partitioning splits a table into pieces by the value of a partition key, usually a date. A query that filters on the key only has to read the pieces that can contain matching rows; the engine skips the rest, which is called partition pruning or partition elimination. A query that does not filter on the key, or filters on it in a way the engine cannot use, reads every partition. It is then slower than the same query on an unpartitioned table, because it repeats its work once per partition.
The plan says exactly which partitions were read. The query plan viewer flags a filtered query that touches all of them. The examples partition 400,000 orders by year: 2023, 2024 and 2025.
Diagnose it
PostgreSQL: the partitions are listed in the plan
Each partition is a table of its own, and the plan names the ones it scans.
CREATE TABLE orders_part (LIKE orders) PARTITION BY RANGE (order_date);
CREATE TABLE orders_part_2023 PARTITION OF orders_part FOR VALUES FROM ('2023-01-01') TO ('2024-01-01');
CREATE TABLE orders_part_2024 PARTITION OF orders_part FOR VALUES FROM ('2024-01-01') TO ('2025-01-01');
CREATE TABLE orders_part_2025 PARTITION OF orders_part FOR VALUES FROM ('2025-01-01') TO ('2026-01-01');
INSERT INTO orders_part SELECT * FROM orders;
ANALYZE orders_part;
EXPLAIN (COSTS OFF)
SELECT count(*) FROM orders_part
WHERE order_date >= '2024-03-01' AND order_date < '2024-04-01';
Finalize Aggregate
-> Gather
Workers Planned: 1
-> Partial Aggregate
-> Parallel Seq Scan on orders_part_2024 orders_part
Filter: ((order_date >= '2024-03-01 00:00:00'::timestamp without time zone) AND (order_date < '2024-04-01 00:00:00'::timestamp without time zone))
Seulement orders_part_2024 appears. A filter on another column cannot prune, and neither can a function around the key:
EXPLAIN (COSTS OFF)
SELECT count(*) FROM orders_part WHERE customer_id = 4242;
EXPLAIN (COSTS OFF)
SELECT count(*) FROM orders_part WHERE date_trunc('month', order_date) = '2024-03-01';
Finalize Aggregate
-> Gather
Workers Planned: 2
-> Partial Aggregate
-> Parallel Append
-> Parallel Seq Scan on orders_part_2024 orders_part_2
Filter: (customer_id = 4242)
-> Parallel Seq Scan on orders_part_2023 orders_part_1
Filter: (customer_id = 4242)
-> Parallel Seq Scan on orders_part_2025 orders_part_3
Filter: (customer_id = 4242)
Finalize Aggregate
-> Gather
Workers Planned: 2
-> Partial Aggregate
-> Parallel Append
-> Parallel Seq Scan on orders_part_2024 orders_part_2
Filter: (date_trunc('month'::text, order_date) = '2024-03-01 00:00:00'::timestamp without time zone)
-> Parallel Seq Scan on orders_part_2023 orders_part_1
Filter: (date_trunc('month'::text, order_date) = '2024-03-01 00:00:00'::timestamp without time zone)
-> Parallel Seq Scan on orders_part_2025 orders_part_3
Filter: (date_trunc('month'::text, order_date) = '2024-03-01 00:00:00'::timestamp without time zone)
When the key is compared with a parameter, pruning happens when the query starts, not when it is planned. The plan then reports how many partitions it dropped as Subplans Removed:
SET plan_cache_mode = force_generic_plan;
PREPARE recent_orders (timestamp) AS
SELECT count(*) FROM orders_part WHERE order_date >= $1;
EXPLAIN (ANALYZE, COSTS OFF, TIMING OFF, SUMMARY OFF, BUFFERS OFF)
EXECUTE recent_orders ('2025-02-01');
DEALLOCATE recent_orders;
RESET plan_cache_mode;
Finalize Aggregate (actual rows=1.00 loops=1)
-> Gather (actual rows=3.00 loops=1)
Workers Planned: 2
Workers Launched: 2
-> Partial Aggregate (actual rows=1.00 loops=3)
-> Parallel Append (actual rows=11413.67 loops=3)
Subplans Removed: 2
-> Parallel Seq Scan on orders_part_2025 orders_part_1 (actual rows=11413.67 loops=3)
Filter: (order_date >= $1)
Rows Removed by Filter: 4960
MySQL: the partitions column
EXPLAIN a un partitions column that lists what the query will read.
CREATE TABLE orders_part (
order_id int NOT NULL,
customer_id int NOT NULL,
status varchar(20) NOT NULL,
order_date datetime NOT NULL,
total decimal(10,2) NOT NULL,
reference varchar(20) NOT NULL,
PRIMARY KEY (order_id, order_date)
)
PARTITION BY RANGE COLUMNS (order_date) (
PARTITION p2023 VALUES LESS THAN ('2024-01-01'),
PARTITION p2024 VALUES LESS THAN ('2025-01-01'),
PARTITION p2025 VALUES LESS THAN (MAXVALUE)
);
INSERT INTO orders_part SELECT * FROM orders;
ANALYZE TABLE orders_part;
EXPLAIN SELECT count(*) FROM orders_part
WHERE order_date >= '2024-03-01' AND order_date < '2024-04-01'\G
partitions: p2024
type: index
rows: 170976
EXPLAIN SELECT count(*) FROM orders_part WHERE customer_id = 4242\G
EXPLAIN SELECT count(*) FROM orders_part WHERE MONTH(order_date) = 3\G
partitions: p2023,p2024,p2025
type: ALL
rows: 395072
partitions: p2023,p2024,p2025
type: index
rows: 395072
Every unique key on a partitioned MySQL table, the primary key included, must contain the partition column. That is why the primary key here is (order_id, order_date).
SQL Server: Actual Partition Count
SQL Server keeps one plan operator for the whole table and records the partitions it touched in the operator's properties: Actual Partition Count et Actual Partitions Accessed in Management Studio, RunTimePartitionSummary in the plan XML.
CREATE PARTITION FUNCTION pfOrderYear (datetime2(0))
AS RANGE RIGHT FOR VALUES ('2024-01-01', '2025-01-01');
CREATE PARTITION SCHEME psOrderYear
AS PARTITION pfOrderYear ALL TO ([PRIMARY]);
CREATE TABLE dbo.OrdersPart (
OrderId int NOT NULL,
CustomerId int NOT NULL,
Status varchar(20) NOT NULL,
OrderDate datetime2(0) NOT NULL,
Total decimal(10,2) NOT NULL,
Reference varchar(20) NOT NULL,
CONSTRAINT PK_OrdersPart PRIMARY KEY CLUSTERED (OrderId, OrderDate)
) ON psOrderYear (OrderDate);
INSERT INTO dbo.OrdersPart SELECT * FROM dbo.Orders;
GO
SET STATISTICS XML ON;
GO
DECLARE @from datetime2(0) = '2024-03-01', @to datetime2(0) = '2024-04-01';
SELECT COUNT(*) FROM dbo.OrdersPart
WHERE OrderDate >= @from AND OrderDate < @to;
<PartitionsAccessed PartitionCount="1"><PartitionRange Start="2" End="2" /></PartitionsAccessed>
One partition of three. A filter on another column reads them all:
SELECT COUNT(*) FROM dbo.OrdersPart WHERE CustomerId = 4242;
<PartitionsAccessed PartitionCount="3"><PartitionRange Start="1" End="3" /></PartitionsAccessed>
The type of the value matters here. The same date range written as plain string literals reads all three partitions on this table:
SELECT COUNT(*) FROM dbo.OrdersPart
WHERE OrderDate >= '2024-03-01' AND OrderDate < '2024-04-01';
<PartitionsAccessed PartitionCount="3"><PartitionRange Start="1" End="3" /></PartitionsAccessed>
SQL Server turned the literals into varchar parameters and converted them to datetime2(7), which is not the type of the partition column, and elimination was lost. Variables or parameters declared as datetime2(0), ou un CAST of each literal to that type, bring it back. This is an implicit conversion problem showing up as a partitioning problem.
To see how the rows are spread over the partitions:
SET STATISTICS XML OFF;
SELECT partition_number, rows
FROM sys.partitions
WHERE object_id = OBJECT_ID('dbo.OrdersPart') AND index_id = 1
ORDER BY partition_number;
1 175199
2 175680
3 49121
Fix it
Filter on the partition key
If the query knows the period it wants, say so in the WHERE clause, even when another condition already identifies the rows. Adding the date range to the customer filter cuts the work to one partition:
EXPLAIN (COSTS OFF)
SELECT count(*) FROM orders_part
WHERE customer_id = 4242
AND order_date >= '2024-01-01' AND order_date < '2025-01-01';
Finalize Aggregate
-> Gather
Workers Planned: 1
-> Partial Aggregate
-> Parallel Seq Scan on orders_part_2024 orders_part
Filter: ((order_date >= '2024-01-01 00:00:00'::timestamp without time zone) AND (order_date < '2025-01-01 00:00:00'::timestamp without time zone) AND (customer_id = 4242))
EXPLAIN SELECT count(*) FROM orders_part
WHERE customer_id = 4242
AND order_date >= '2024-01-01' AND order_date < '2025-01-01'\G
partitions: p2024
type: ALL
rows: 170976
Compare the bare key with a range
A function around the key hides it from the pruning logic in the same way it hides a column from an index. Write the condition as a range on the column itself: order_date >= '2024-03-01' AND order_date < '2024-04-01', et non date_trunc('month', order_date) = '2024-03-01'. The rewrite is the same one that fixes non-sargable predicates.
Index the partitions for queries that cannot use the key
Some queries have no period to filter on. They will visit every partition, and the goal becomes making each visit cheap. An index created on the partitioned table is built on every partition:
CREATE INDEX orders_part_customer_idx ON orders_part (customer_id);
EXPLAIN (COSTS OFF)
SELECT count(*) FROM orders_part WHERE customer_id = 4242;
Aggregate
-> Append
-> Bitmap Heap Scan on orders_part_2023 orders_part_1
Recheck Cond: (customer_id = 4242)
-> Bitmap Index Scan on orders_part_2023_customer_id_idx
Index Cond: (customer_id = 4242)
-> Bitmap Heap Scan on orders_part_2024 orders_part_2
Recheck Cond: (customer_id = 4242)
-> Bitmap Index Scan on orders_part_2024_customer_id_idx
Index Cond: (customer_id = 4242)
-> Bitmap Heap Scan on orders_part_2025 orders_part_3
Recheck Cond: (customer_id = 4242)
-> Bitmap Index Scan on orders_part_2025_customer_id_idx
Index Cond: (customer_id = 4242)
Three index probes are cheap. Three hundred are not, which is a reason to keep the number of partitions modest when many queries cannot prune.
PostgreSQL: check that pruning is enabled
SHOW enable_partition_pruning;
enable_partition_pruning
on
It is on by default. If it reports off, someone changed it, and no query on the server prunes.
When to leave it alone
A query that needs rows from every period is supposed to read every partition: a count of all orders per customer has nothing to prune. Leave it alone too when the partitions are few and each one is read through an index. Pruning matters for the queries the partitioning was designed for, the ones that ask for a slice of time.
The checklist
Work through it in order. Each step gives you the evidence for the next one.
- Find which partitions the plan reads: the scan nodes (PostgreSQL), the partitions column (MySQL), Actual Partition Count (SQL Server).
- Check that the WHERE clause filters on the partition key at all.
- Remove any function, cast or calculation around the key.
- Add the period to the query when the application knows it.
- For queries that cannot filter on the key, index each partition on the column they do filter on.
- Re-capture the plan and confirm fewer partitions are read.
Questions that come up
What is partition pruning?
It is the step where the engine works out, from the WHERE clause and the partition boundaries, which partitions cannot contain matching rows and leaves them out of the plan. SQL Server calls the same thing partition elimination.
Why does my query scan all partitions?
Either it does not filter on the partition key, or the filter wraps the key in a function or cast so the engine cannot compare it with the partition boundaries. A filter on any other column, even an indexed one, does not prune.
What does Subplans Removed mean in PostgreSQL?
It is the number of partitions PostgreSQL dropped when the query started running, after it learned the values of parameters. Partitions pruned at planning time do not appear in the plan at all; those pruned at execution start are counted in Subplans Removed.
Does partitioning make queries faster?
Only the queries that prune. A query that reads one partition of twenty touches a twentieth of the data. A query that cannot prune pays a small extra cost for every partition. Partitioning mainly helps with maintenance, such as dropping old data, and with time-sliced queries.
Does a join on the partition key prune?
It can at run time. PostgreSQL prunes partitions inside a nested loop when the key is compared with a value from the outer row; those scans are shown as never executed. A direct filter with a constant or parameter is the more reliable way to get pruning.