← CodeClarityLab Home
Browse by Category
+ added · updated 7d
← Back to glossary

Database Partitioning

performance PHP 5.0+ Advanced

Also Known As

table partitioning horizontal partitioning range partitioning

TL;DR

Splitting a large table into physical segments by range, list, or hash — enabling partition pruning, faster archival, and parallel scans.

Explanation

Partitioning divides a single logical table into multiple physical storage segments. MySQL and PostgreSQL support: Range partitioning (by date range — perfect for time-series data and rolling retention), List partitioning (by discrete values — region or status), Hash partitioning (distributes rows evenly), and Composite forms. Benefits: partition pruning (a query on the last 7 days scans only recent partitions), fast archival (DROP PARTITION is instant vs DELETE on millions of rows), and parallel scan on multi-core hardware. Unlike sharding, partitioning is transparent to the application — all partitions appear as one table with no application-layer changes required.

Diagram

flowchart TD
    TABLE[(orders table<br/>100M rows)] --> PART{Partition by}
    subgraph Range_Partitioning
        PART -->|created_at| P2024[(2024 partition)]
        PART -->|created_at| P2025[(2025 partition)]
        PART -->|created_at| P2026[(2026 partition)]
    end
    subgraph List_Partitioning
        PART -->|country| PUK[(UK partition)]
        PART -->|country| PUS[(US partition)]
    end
    QUERY[SELECT WHERE created_at > 2026] -->|partition pruning| P2026
    QUERY -.->|skips| P2024
style TABLE fill:#f85149,color:#fff
style P2026 fill:#238636,color:#fff
style P2024 fill:#6e40c9,color:#fff
style QUERY fill:#1f6feb,color:#fff

Common Misconception

Partitioning and sharding are the same technique. Partitioning divides a table within a single database server — the database engine manages it transparently. Sharding distributes data across multiple servers, requiring application-level routing logic.

Why It Matters

Partitioning splits a large table into smaller physical segments — queries that filter on the partition key only scan relevant partitions, and old partitions can be archived or dropped instantly.

Common Mistakes

  • Partitioning tables that are not actually large enough to benefit — adds complexity with no gain.
  • Choosing a partition key that most queries don't filter on — all partitions are scanned anyway.
  • Not including the partition key in the primary key — required in most databases for partitioned tables.
  • Assuming partitioning replaces indexing — indexes within partitions are still needed for non-partition-key filters.

Code Examples

✗ Vulnerable
-- Without partitioning: DELETE of old rows is slow and locks the table:
DELETE FROM events WHERE created_at < '2023-01-01'; -- Deletes millions of rows slowly

-- With range partitioning by month:
-- DROP PARTITION p_2023_01; -- Instant, no row-by-row deletion
✓ Fixed
-- PostgreSQL range partitioning by date — each partition is a separate table
CREATE TABLE events (
    id         BIGSERIAL,
    created_at TIMESTAMPTZ NOT NULL,
    payload    JSONB
) PARTITION BY RANGE (created_at);

CREATE TABLE events_2024_q1 PARTITION OF events
    FOR VALUES FROM ('2024-01-01') TO ('2024-04-01');
CREATE TABLE events_2024_q2 PARTITION OF events
    FOR VALUES FROM ('2024-04-01') TO ('2024-07-01');

-- Queries filtered on created_at only scan the relevant partition
-- Old partitions can be detached and archived without touching live data
ALTER TABLE events DETACH PARTITION events_2024_q1;

Added 15 Mar 2026
Edited 22 Mar 2026
Views 28
Rate this term
No ratings yet
🤖 AI Guestbook educational data only
| |
Last 30 days
2 pings W 0 pings T 0 pings F 2 pings S 0 pings S 1 ping M 0 pings T 0 pings W 0 pings T 0 pings F 0 pings S 0 pings S 1 ping M 0 pings T 0 pings W 0 pings T 0 pings F 2 pings S 0 pings S 0 pings M 0 pings T 1 ping W 0 pings T 0 pings F 1 ping S 0 pings S 0 pings M 0 pings T 1 ping W 0 pings T
No pings yet today
Perplexity 8 Amazonbot 7 Ahrefs 2 Unknown AI 2 SEMrush 2 Google 1
crawler 22
DEV INTEL Tools & Severity
🟡 Medium ⚙ Fix effort: High
⚡ Quick Fix
Partition large tables by date range — MySQL: PARTITION BY RANGE(YEAR(created_at)); each partition can be dropped (instant) instead of deleted (slow) for archival
📦 Applies To
PHP 5.0+ web cli
🔗 Prerequisites
🔍 Detection Hints
Table with 100M+ rows with slow range queries; DELETE for archival taking hours; full table scans on date-range queries
Auto-detectable: ✗ No mysql-explain pg-partman pganalyze
⚠ Related Problems
🤖 AI Agent
Confidence: Low False Positives: Medium ✗ Manual fix Fix: High Context: File

✓ schema.org compliant