CodeBlanks Logo Icon CodeBlanks

Hash partition

Partition by a hash.

Hash partition

Hash partition splits by a hash value. Ideal for even distribution.

CREATE TABLE orders (...) PARTITION BY HASH (id);
CREATE TABLE orders_0 PARTITION OF orders
  FOR VALUES WITH (MODULUS 4, REMAINDER 0);

4 partitions, by id mod 4.

The table orders is created.

Hash partition — details

Hash partition defines partition count with MODULUS and REMAINDER.

For example, four partitions with MODULUS 4 and REMAINDER 0 to 3 spread rows roughly evenly by the hash of the id.

Practice Hash partition

Create the orders_0 partition with FOR VALUES WITH (MODULUS 4, REMAINDER 0).

Starter code

query.sql
-- hash partition
CREATE TABLE orders_0 PARTITION OF orders

Sample solution

query.sql
CREATE TABLE orders_0 PARTITION OF orders
  FOR VALUES WITH (MODULUS 4, REMAINDER 0);

Expected output

Bölüm oluşturuldu

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 What does hash partition provide?

  • A range
  • Even distribution Correct answer
  • A date
  • A list

Explanation: Hash partition provides even distribution.

2 Add HASH

Question
PARTITION BY ____ (id)
Answer
PARTITION BY HASH (id)

Explanation: HASH partitioning spreads rows evenly across partitions by the key's hash.

3 Hash partition: match the terms

Term Meaning
HASH hash
MODULUS partition count
REMAINDER remainder
even balanced

All lessons in Advanced