Articles

Database Partitioning vs Sharding: What's the Difference

Partitioning splits a table within one database; sharding splits data across separate database instances entirely. Here's how each works and when to use them.

The Lycoris Team The Lycoris Team · · 4 min read
Abstract illustration representing databases

Partitioning splits a single table’s rows into smaller physical pieces while it stays inside one database instance; sharding splits data across multiple separate database instances, often on different machines entirely. Both address the same underlying problem — a table too large for good performance on a single set of resources — but they solve it at different scopes, and confusing the two leads to picking the wrong tool for the actual bottleneck.

Partitioning: one database, divided storage

Partitioning divides a large table into smaller pieces, called partitions, that the database engine manages internally while presenting a single logical table to queries. The engine decides which partition holds which rows and routes queries accordingly, transparently to the application.

Common partitioning schemes:

  • Range partitioning — rows are split by a range of values, commonly a date column. An orders table partitioned by month keeps each month’s rows in a separate partition, so a query for last month’s orders only scans that one partition instead of the entire table’s history.
  • List partitioning — rows are split by a fixed set of values, such as region or category.
  • Hash partitioning — a hash function distributes rows evenly across a fixed number of partitions, useful when there’s no natural range or list key that distributes data evenly on its own.

Because partitioning happens inside a single database, it still shares that database’s connection pool, CPU, memory, and disk. Queries that touch only relevant partitions get faster (partition pruning), and maintenance operations like rebuilding an index or archiving old data become cheaper — you can drop an entire month-old partition instantly instead of running a slow DELETE over millions of rows. But partitioning does nothing to relieve pressure on the single machine’s overall write throughput or total storage ceiling.

Sharding: multiple databases, divided data

Sharding takes the same idea a level higher: rather than splitting a table within one database, it splits the entire dataset across multiple independent database instances — each one a full, standalone database with its own compute, memory, and storage. Each shard typically holds a distinct subset of rows, most often determined by a shard key such as customer ID or geographic region.

This directly addresses what partitioning can’t: total capacity. If a single machine can’t hold the data or handle the write volume, adding shards adds more machines, each handling its own slice of both reads and writes independently. This is core to horizontal scaling at the database layer, and to how systems built around consistent hashing decide which shard owns a given key.

The tradeoff is operational complexity. Queries that need data from multiple shards — a report spanning all customers, for instance — must fan out to every shard and merge results in the application, since there’s no single database engine that can join across shard boundaries the way it joins across partitions in one instance. Rebalancing shards when one grows disproportionately large, and maintaining consistent replication per shard, both add meaningfully to operational overhead compared to a single partitioned database.

Side by side

PartitioningSharding
ScopeWithin a single database instanceAcross multiple database instances
SolvesQuery and maintenance performance on large tablesTotal capacity — storage and write throughput
Cross-partition/shard queriesHandled by the database engine transparentlyRequires application-level fan-out and merging
Operational complexityLow — managed by the databaseHigh — the application (or a routing layer) must be shard-aware
Typical triggerA single table is too large or slow to query/maintain efficientlyA single machine can’t hold or serve the whole dataset

Using them together

These aren’t mutually exclusive — many large-scale systems partition within each shard. A multi-tenant SaaS application might shard customers across ten database instances by customer ID, then partition each shard’s events table by month internally. The sharding layer solves “we have more data than one machine can hold”; the partitioning layer inside each shard solves “this table on this machine is still too large to query efficiently as a whole.”

Choosing where to start

Partitioning is the lower-risk, lower-effort first move — it’s usually a feature of the database engine itself (available in PostgreSQL, MySQL, and most managed database services) and doesn’t require any application changes to route queries. It’s worth reaching for whenever a specific table has grown large enough that queries filtering on the partition key (like a date range) are noticeably slow, or maintenance windows for that table have become painful.

Sharding is a bigger commitment: it changes how the application connects to and queries data, and it’s hard to undo cleanly once data is distributed. It’s worth the complexity when a single database instance is genuinely out of headroom — not before. Reaching for sharding to fix a slow-query problem that partitioning (or better indexing) would solve is a common and expensive overcorrection.

The takeaway

Partitioning divides a table within one database and is managed transparently by the engine; sharding divides an entire dataset across independent database instances and pushes routing complexity up to the application. Partitioning fixes query and maintenance performance on oversized tables; sharding fixes capacity ceilings a single machine can’t clear. Start with partitioning and proper indexing, and reach for sharding only once you’ve confirmed the bottleneck is total capacity, not just an unpartitioned table.

The Lycoris Team The Lycoris Team · · 5 min read

What Is a Database Trigger?

A database trigger is a procedure that runs automatically on an insert, update, or delete — enforcing rules the application layer can't guarantee.

#Databases #SQL #Backend
The Lycoris Team The Lycoris Team · · 4 min read

Primary Key vs Foreign Key vs Unique Constraint

Primary keys identify a row, foreign keys link one table to another, and unique constraints just prevent duplicates. How the three differ in SQL.

#Databases #SQL #Backend