Partitioning for Database Performance
Partitioning for Database Performance
How to use it ?
splitting one large table into smaller (called partitions) based on a key (created_at, user_id, etc.)
When to use it ?
🐘 very large tables (millions to billions of rows)
if you table is growing rapidly and you're noticing slower reads, writes, it's time to
think partitioning.
especially true for time-series data (logs, events, metrics, etc.)
🕢 queries that often target subsets of data
show me users from region X
give me data from the 2022
🪣 need to archive or delete data often
if you're regularly deleting old records (e.g. logs older than 90 days) you can just
DROP a whole partition - way faster than DELETE
INSERT INTO users (id, name, created_at) VALUES (1, 'Alice', '2024-04-10');
INSERT INTO users (id, name, created_at) VALUES (2, 'Bob', '2025-04-10');
📒 list partition
PARTITION BY LIST (country)
-- Partition 1: 'US', 'CA'
-- Partition 2: 'UK', 'DE'
🔢 hash partition
PARTITION BY HASH (user_id)
PARTITIONS 4;
-- Partition 0: user_id % 4 = 0
-- Partition 1: user_id % 4 = 1