SQL Topics
Partitioning
title: Partitioning
In the previous lesson, you learned about:
Indexing Strategies
Composite Indexes
Covering Indexes
Index MaintenanceIndexes improve query performance by helping SQL find data faster.
However, when tables grow extremely large:
Millions of Rows
Hundreds of Millions of Rows
Billions of Rowseven indexes may not be enough.
To handle very large datasets efficiently, databases use:
PartitioningPartitioning divides large tables into smaller, manageable pieces while allowing users to query the table as if it were a single table.
It is one of the most powerful optimization techniques used in:
Banking Systems
E-Commerce Platforms
Data Warehouses
Telecommunication Systems
Big Data ApplicationsWhat is Partitioning?
Partitioning is the process of dividing a large table into smaller logical pieces called partitions.
Instead of:
One Huge Tablewe create:
Multiple Smaller Partitionsthat together behave like a single table.
Simple Definition
Partitioning is a database technique that splits large tables into smaller parts to improve performance and manageability.
Why is Partitioning Needed?
Imagine a transaction table:
Transactionscontains:
1 Billion RowsQuery:
SELECT *
FROM Transactions
WHERE TransactionDate
BETWEEN '2025-01-01'
AND '2025-01-31';Without partitioning:
Entire Table ExaminedWith partitioning:
Only January Partition AccessedMuch faster.
Partitioning Goals
Partitioning helps:
Improve Query Performance
Reduce I/O
Improve Manageability
Simplify Maintenance
Increase ScalabilityHow Partitioning Works
Without Partitioning:
Transactions Table
1 Billion RowsWith Partitioning:
SQL accesses only relevant partitions.
Types of Partitioning
Major types:
Horizontal Partitioning
Vertical Partitioning
Range Partitioning
List Partitioning
Hash Partitioning
Composite PartitioningHorizontal Partitioning
Rows are divided into partitions.
Example:
| EmployeeID | Department |
|---|---|
| 1 | HR |
| 2 | IT |
| 3 | HR |
| 4 | IT |
Partitions:
HR Employees
IT EmployeesRows split across partitions.
Vertical Partitioning
Columns are divided.
Example:
Employees Table:
| ID | Name | Address | Salary |
|---|
Partition 1:
| ID | Name |
|---|
Partition 2:
| ID | Address | Salary |
|---|
Columns split into separate tables.
Horizontal vs Vertical Partitioning
| Feature | Horizontal | Vertical |
|---|---|---|
| Splits Rows | Yes | No |
| Splits Columns | No | Yes |
| Common Usage | Very High | Moderate |
| Large Tables | Excellent | Good |
Range Partitioning
Most popular partitioning method.
Rows are assigned based on value ranges.
Example
Transaction Table:
2023 Data
2024 Data
2025 Datastored in different partitions.
Range Partitioning Syntax (MySQL)
CREATE TABLE Transactions
(
TransactionID INT,
TransactionDate DATE
)
PARTITION BY RANGE
(
YEAR(TransactionDate)
)
(
PARTITION P2023
VALUES LESS THAN (2024),
PARTITION P2024
VALUES LESS THAN (2025),
PARTITION P2025
VALUES LESS THAN (2026)
);How Range Partitioning Works
Query:
SELECT *
FROM Transactions
WHERE YEAR(TransactionDate)=2025;Only:
P2025partition accessed.
Advantages of Range Partitioning
Easy Data Management
Historical data separated.
Faster Date Queries
Very common use case.
Easy Archiving
Old partitions removable.
List Partitioning
Rows assigned based on specific values.
Example
Employees:
HR
IT
FinancePartitions:
HR Partition
IT Partition
Finance PartitionSyntax
PARTITION BY LIST
(
DepartmentID
)
(
PARTITION HR
VALUES IN (1),
PARTITION IT
VALUES IN (2),
PARTITION Finance
VALUES IN (3)
);Hash Partitioning
Rows distributed using a hash algorithm.
Purpose
Evenly distribute data.
Example
PARTITION BY HASH
(
EmployeeID
)
PARTITIONS 4;Data distributed automatically:
Partition 1
Partition 2
Partition 3
Partition 4Advantages of Hash Partitioning
Balanced Distribution
Data spread evenly.
Better Parallelism
Supports concurrent access.
Reduced Hotspots
Prevents uneven storage.
Composite Partitioning
Combination of multiple partitioning methods.
Example
Range Partition
+
Hash PartitionStructure:
Partition Pruning
Very important interview topic.
What is Partition Pruning?
Partition pruning means SQL accesses only relevant partitions instead of scanning all partitions.
Example:
Query:
SELECT *
FROM Transactions
WHERE TransactionDate
BETWEEN '2025-01-01'
AND '2025-01-31';Database accesses:
January 2025 Partition OnlyBenefit:
Less I/O
Better PerformancePartition Elimination
Another term often used for:
Partition PruningLocal Indexes
Indexes stored separately inside each partition.
Example:
Partition A → Index A
Partition B → Index BGlobal Indexes
Single index spanning all partitions.
Example:
One Index
Across Entire TablePartition Maintenance
Partitioning simplifies maintenance.
Add Partition
Example:
ALTER TABLE Transactions
ADD PARTITION
(
PARTITION P2027
VALUES LESS THAN (2028)
);Drop Partition
Example:
ALTER TABLE Transactions
DROP PARTITION P2023;Benefit
Old data removed instantly.
Partition Exchange
Swap data between:
Partition
and
Tablewithout moving data.
Used in large data warehouses.
Real-World Example: Banking
Transactions:
Partition By YearQuerying recent transactions becomes faster.
Real-World Example: E-Commerce
Orders:
Partition By Order DateOlder orders stored separately.
Real-World Example: Payroll
Salary records:
Partition By Financial YearReal-World Example: University
Student records:
Partition By Admission YearReal-World Example: Telecom
Call records:
Partition By MonthMassive performance improvement.
Advantages of Partitioning
Faster Queries
Major benefit.
Reduced Disk I/O
Less data scanned.
Better Scalability
Handles massive datasets.
Easier Maintenance
Partitions managed individually.
Faster Archiving
Old partitions removable.
Disadvantages
Increased Complexity
Requires planning.
More Administration
Additional maintenance.
Poor Design Risks
Wrong partitioning strategy hurts performance.
Not Useful For Small Tables
Overhead outweighs benefits.
Common Mistakes
Partitioning Small Tables
Unnecessary complexity.
Wrong Partition Key
Poor performance.
Ignoring Query Patterns
Partitions should match workload.
Too Many Partitions
Management becomes difficult.
Best Practices
Partition Large Tables Only
Usually millions of rows.
Choose Partition Keys Carefully
Most important decision.
Align Partitions With Queries
Optimize common workloads.
Use Partition Pruning
Ensure queries benefit.
Monitor Performance
Verify improvements.
Common Interview Questions
What is Partitioning?
A technique that divides large tables into smaller partitions.
What is Range Partitioning?
Partitioning data based on value ranges.
What is Hash Partitioning?
Partitioning data using a hash algorithm.
What is Partition Pruning?
Accessing only relevant partitions during query execution.
Is Partitioning a Replacement for Indexing?
No.
Partitioning and indexing are complementary techniques.
Summary
Partitioning is an advanced database optimization technique used to divide very large tables into smaller, manageable partitions. It improves performance, scalability, maintenance, and data management while enabling efficient access to massive datasets.
In this lesson, you learned:
- What Partitioning is
- Horizontal Partitioning
- Vertical Partitioning
- Range Partitioning
- List Partitioning
- Hash Partitioning
- Composite Partitioning
- Partition Pruning
- Local and Global Indexes
- Best Practices
Mastering Partitioning is essential for handling enterprise-scale databases containing millions or billions of rows.
Next Step
Continue to the next lesson:
SQL Performance Tips →
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for Partitioning.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this SQL topic.
Search Terms
sql, sql complete guide, sql tutorial, sql notes, complete, guide, optimization, partitioning
Related SQL Topics