Partitioning by Range and Hash
One logical table, several physical pieces. The query planner skips the pieces it can prove are irrelevant.
Partitioning by Range and Hash
Compare the access patterns
One logical table, several physical pieces. The query planner skips the pieces it can prove are irrelevant.
Compare the access patterns
With table partitioning, queries and applications can work with one logical table while the database stores rows in separate partitions. A partitioning rule assigns each row—for example, a range of dates per month or a hash bucket based on a tenant key.
With table partitioning, queries and applications can work with one logical table while the database stores rows in separate partitions. A partitioning rule assigns each row—for example, a range of dates per month or a hash bucket based on a tenant key.
Partitioning can help with lifecycle operations and queries that filter by the partition key. When a predicate excludes partitions, partition pruning lets the engine skip those pieces. It does not automatically make every query faster, and queries that scan all partitions may gain little.
A useful partition key appears in frequent filters and divides data with predictable boundaries. Range partitioning suits ordered values such as time; hash partitioning can distribute values more evenly when range scans are not the main need. Too many tiny partitions increase planning and operational overhead.
Constraints, unique indexes, foreign keys, and maintenance behavior vary by engine. PostgreSQL, for example, has rules for unique constraints across partitioned tables. Test inserts, query plans, retention, and backups with the actual database and version.
Partitions are usually managed inside one database system; sharding distributes data across separate servers or nodes and adds routing and cross-shard coordination. Partitioning does not by itself add horizontal capacity. See sharding.
A log table partitioned by month can let a query for one month avoid scanning older data, provided the filter exposes the partition key and the planner prunes other partitions. Retention may also be simpler when an old partition can be detached or dropped instead of deleting millions of individual rows. This depends on engine features, foreign-key relationships, backup policy, and whether the partition is still needed for historical queries. Partition boundaries should cover inserts deliberately; an uncovered date can cause writes to fail or enter a catch-all partition.
A query can fail to prune partitions if it wraps the key in a function, casts it unexpectedly, or uses a predicate the planner cannot prove. Compare the plan for a bounded date range with a query that omits the date condition. Confirm that only the intended partitions are scanned and that boundary timestamps land in the correct interval. Partitioning creates operational work too: monitor partition creation, statistics, backups, and retention so the table does not accumulate empty or obsolete pieces.
A monthly partition is not automatically right for every time series. A small table may gain nothing from partitioning, while a very high-volume table may need daily or weekly boundaries to keep maintenance manageable. Too many partitions increase planning and catalog overhead; partitions that are too large can make retention and archival operations cumbersome. Choose a size using row volume, query ranges, maintenance windows, and engine limits. Revisit that choice as data grows rather than treating it as a one-time schema detail.
Answer without scrolling back up.
What is partition pruning?
It requires the partition key in the WHERE clause. Without it every partition is read, and the scheme costs more than it saves.
Why can hash partitioning not prune a range query?
That is the trade for even distribution. Range partitioning prunes ranges well but can leave one partition hot when recent data gets all the traffic.
Why is time-series data almost always range-partitioned?
Retention is usually the bigger win over pruning. DROP TABLE unlinks a file; DELETE generates enormous WAL traffic and leaves bloat behind.
Partition a table by range and by hash, run the same query on each layout, and see how the planner skips every partition it does not need.