What is sharding?
- SQL course
- Expert
- Lesson 27/45
- 5 min
Split data across servers.
What is sharding?
Sharding splits a large table across multiple nodes horizontally. Each shard holds a subset of the table. Replication copies data, sharding splits it.
CREATE TABLE orders (id INT, ...)
PARTITION BY HASH (customer_id);Sharding splits data.
The table orders is created.
What is sharding? — details
Hash sharding places a row on a shard by hashing the key. Range sharding splits by interval (date, region). Each method distributes data differently.
Practice What is sharding?
Shard the orders table with PARTITION BY HASH (customer_id).
Starter code
-- sharding
CREATE TABLE orders (id INT, amount NUMERIC)Sample solution
CREATE TABLE orders (id INT, amount NUMERIC) PARTITION BY HASH (customer_id);Expected output
CREATE TABLE
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 How does sharding split data?
- Horizontally Correct answer
- Vertically
- By type
- By column
Explanation: Sharding splits data horizontally.
2 Partition method
CREATE TABLE orders (id INT, amount NUMERIC) PARTITION BY ____ (customer_id)
CREATE TABLE orders (id INT, amount NUMERIC) PARTITION BY HASH (customer_id)
Explanation: HASH partitioning spreads rows evenly across partitions by the hash of customer_id.
3 What is sharding: match the terms
| Term | Meaning |
|---|---|
sharding |
sharding |
shard |
shard |
hash |
hash |
range |
range |