Databases & System Design•Part 2: Relational Databases (RDBMS / PostgreSQL / MySQL) (6 - 25)
Table Partitioning vs Sharding:
In Simple Words:
- Table Partitioning: Splitting a huge table into smaller physical child tables on the same database server.
- Database Sharding: Distributing data horizontally across multiple independent physical database servers.
sql
-- Declarative Range Partitioning by Year in PostgreSQL:
CREATE TABLE logs (
id BIGSERIAL,
log_time TIMESTAMPTZ NOT NULL,
message TEXT
) PARTITION BY RANGE (log_time);
CREATE TABLE logs_2025 PARTITION OF logs
FOR VALUES FROM ('2025-01-01') TO ('2026-01-01');