CodeBlanks Logo Icon CodeBlanks

What is sharding?

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

query.sql
-- sharding
CREATE TABLE orders (id INT, amount NUMERIC)

Sample solution

query.sql
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

Question
CREATE TABLE orders (id INT, amount NUMERIC) PARTITION BY ____ (customer_id)
Answer
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

All lessons in Expert