Partitioning
Партиционирование
A database can split one table into smaller pieces based on a value such as a date or region. Applications still use the table as one, while the database can skip pieces that cannot contain matching rows.
What partitioning changes
Suppose an events table holds two years of data, but most dashboards look at the past week. Rather than store every row in one piece, you can store each month's rows in a separate partition. The application still inserts into and queries the same logical table. The database routes new rows to the right partition and can rule out irrelevant partitions when reading.
How rows are assigned
A partition key is the column or set of columns used to assign rows to partitions. The method should fit the workload:
- Range partitioning uses intervals, such as calendar months. It fits time-window queries and lets you retire data in time-based batches.
- List partitioning assigns explicit values or groups of values to partitions. For example, you might group several country codes into a sales region.
- Hash partitioning uses a hash of the key to assign rows to buckets. It can spread rows across partitions, but a frequently occurring key still goes to the same bucket, and a time range does not naturally map to a subset of hash partitions on customer ID.
Example: skipping old data
In PostgreSQL, assume events is partitioned by range on event_date. This creates a partition and queries exactly the date range it covers:
CREATE TABLE events_2026_01 PARTITION OF events
FOR VALUES FROM ('2026-01-01') TO ('2026-02-01');
SELECT count(*) FROM events
WHERE event_date >= DATE '2026-01-01'
AND event_date < DATE '2026-02-01';
The upper bound is exclusive, so February 1 does not belong to the January partition. The query still names events, but the database can skip the other months because they cannot contain matching rows. This is partition pruning. Use EXPLAIN to see which partitions the plan will scan for this query.
When it helps, and what it costs
Pruning is not an index lookup: pruning decides which partitions to visit, while an index can help find rows inside a selected partition. A query that filters only on customer_id may still need to visit every monthly partition, even if each has a customer index. Creating thousands of tiny partitions can add planning and maintenance overhead without improving those queries.
Time-based partitioning is also useful for retention. If an entire month has expired, you can drop its partition instead of deleting each row individually. You also need to create future partitions in time: in PostgreSQL, an insert with no matching partition fails unless a default partition can accept the row. None of this requires multiple servers. Sharding distributes subsets of data across nodes; a partitioned table can live on a single server.
What interviewers look for
When asked to design a partitioned table, start with the most frequent or expensive queries and the retention policy. Explain which predicates will allow pruning, which queries will still need indexes or touch multiple partitions, and how you would verify the design with query plans and timings.