🗄️ Databases

Partitioning

Partitioning

Physically splitting a table. Range, list, hash.

📖 Detailed Explanation

Partitioning is a technique that physically divides a large table into multiple pieces (partitions) for storage. It splits a table within a single DB server to improve Query performance and make management easier. Unlike Sharding, it is handled at the DB level without changes at the application level.

Range partitioning divides data by date or numeric ranges. It is suitable for time-series data such as logs and orders, and old partitions can be dropped quickly. List partitioning divides data by a list of specific values and is useful for splitting by region or category.

Hash partitioning distributes data evenly using a hash function and prevents data from being concentrated on particular values. Key partitioning uses MySQL's hash function. Composite partitioning combines multiple methods.

Partition Pruning is an optimization that makes a Query scan only the necessary partitions. Pruning works only when the WHERE clause includes the partition key. You can check this in the partitions field of the Execution Plan.

💻 Code Example

-- Range 파티셔닝 (날짜 기준)
CREATE TABLE orders (
    id INT,
    user_id INT,
    order_date DATE,
    total DECIMAL(10,2)
)
PARTITION BY RANGE (YEAR(order_date)) (
    PARTITION p2022 VALUES LESS THAN (2023),
    PARTITION p2023 VALUES LESS THAN (2024),
    PARTITION p2024 VALUES LESS THAN (2025),
    PARTITION p_future VALUES LESS THAN MAXVALUE
);

-- List 파티셔닝 (지역 기준)
CREATE TABLE users (
    id INT,
    name VARCHAR(100),
    region VARCHAR(10)
)
PARTITION BY LIST COLUMNS(region) (
    PARTITION p_asia VALUES IN ('KR', 'JP', 'CN'),
    PARTITION p_europe VALUES IN ('UK', 'DE', 'FR'),
    PARTITION p_america VALUES IN ('US', 'CA', 'BR')
);

-- Hash 파티셔닝 (균등 분배)
CREATE TABLE sessions (
    id INT,
    user_id INT,
    created_at DATETIME
)
PARTITION BY HASH(user_id)
PARTITIONS 8;

-- 파티션 추가
ALTER TABLE orders ADD PARTITION (
    PARTITION p2025 VALUES LESS THAN (2026)
);

-- 오래된 파티션 삭제 (데이터 삭제보다 훨씬 빠름)
ALTER TABLE orders DROP PARTITION p2022;

-- 파티션 프루닝 확인
EXPLAIN SELECT * FROM orders WHERE order_date = '2024-06-15';

🗣️ Examples of Practical Conversations

Senior: The log table has 1 billion rows. We need to delete data older than 90 days, but DELETE takes too long.

Junior: Can't we delete it in batches?

Senior: Partitioning is better. If you create monthly Range partitions and use DROP PARTITION, the data is deleted instantly.

Interviewer: What is the difference between Partitioning and Sharding?

Candidate: Partitioning splits a table within a single DB server, while Sharding distributes it across multiple servers. Partitioning is handled transparently by the DB but is limited by the server, while Sharding can scale infinitely but requires application changes.

Interviewer: What about cases where Partition Pruning does not work?

Candidate: It is when the WHERE clause has no partition key or the key is wrapped in a function. For example, WHERE YEAR(order_date) = 2024 does not allow pruning, while WHERE order_date >= '2024-01-01' does.

Take a mock interview on this topic → WIA SOOM AI Interview · Start for free

Reviewer: There are more than 1000 partitions. Too many makes them hard to manage and degrades performance.

Author: That happened because I created partitions on a daily basis.

Reviewer: Change to monthly or quarterly, and consider subpartitioning. About 100 partitions or fewer is appropriate.

⚠️ Cautions

🔗 Related Terms

📚 Learn More