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

query.sql SQLite
Result

Worth knowing

Partitioning splits one table into pieces by a rule on a column. The table still looks like one table.
Range partitions by an ordered value, usually a date. Hash spreads rows evenly by hashing a key.
The win is partition pruning: a query with a predicate on the partition key touches only the partitions that can match.
Dropping a partition is instant. Deleting the equivalent rows is not, which is why time-series data is almost always range-partitioned.

Overview

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.

In this guide
  1. One table, several partitions
  2. Choose the key from access patterns
  3. Partitioning is not sharding
  4. A time-based retention example
  5. Test pruning with the real predicate
  6. Balance partition size and count
  7. Further reading

One table, several partitions

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.

Choose the key from access patterns

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.

Partitioning is not sharding

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 time-based retention example

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.

Test pruning with the real predicate

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.

Balance partition size and count

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.

Further reading

Check yourself

0 of 3

Answer without scrolling back up.

  1. What is partition pruning?

  2. Why can hash partitioning not prune a range query?

  3. Why is time-series data almost always range-partitioned?

Cheat sheet

Partitioning by Range and Hash

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.

DATABASE · vizlearn.in/database/partitioning_in_databases.html

About the author

Ashish Jangra builds and maintains VizLearn. Every module here is written and the visualisation behind it hand-built, so the numbers in a readout come from the same code that draws the picture. Corrections are genuinely welcome and get priority over everything else — if a page states something wrong, or an animation misrepresents what the algorithm does, get in touch.

Teaching a course with this? Individual teachers can use it freely; for a whole department or programme, see institutional licences.