What are the differences between horizontal and vertical partitioning in a database?
Free Coding Questions Catalog
Boost your coding skills with our essential coding questions catalog. Take a step towards a better tech career now!
Horizontal and vertical partitioning are techniques used in databases to distribute data across multiple tables or databases. They are key strategies for managing large datasets and improving performance, scalability, and manageability. Let's explore their differences with examples:
Horizontal Partitioning (Sharding):
- Concept: In horizontal partitioning, the rows of a table are divided across multiple tables or databases. Each partition (or shard) holds a subset of the rows but the same columns as the original table.
- When to Use: It's used to improve performance and manageability when dealing with a large number of rows.
- Example:
- Suppose you have a table
Users
with columnsUserID
,Name
,Email
, andCountry
. - In horizontal partitioning, you might divide this table into multiple tables based on
Country
. For example, all users from the USA in one table, users from the UK in another, and so on. - Each partitioned table has the same columns (
UserID
,Name
,Email
,Country
), but only a portion of the rows.
- Suppose you have a table
Vertical Partitioning:
- Concept: In vertical partitioning, the columns of a table are divided into multiple tables. Each partition holds a subset of the columns and all rows.
- When to Use: Useful when a table has many columns, and queries often access only a subset of those columns. It helps in reducing I/O by loading only the necessary columns.
- Example:
- Continuing with the
Users
table example (UserID
,Name
,Email
,Country
), in vertical partitioning, you might split this into two tables:- One table with
UserID
andName
. - Another table with
UserID
,Email
, andCountry
.
- One table with
- Each table holds part of the columns but all user rows. The
UserID
column is common in both tables and acts as a link between them.
- Continuing with the
Key Differences:
-
Partitioning Criteria:
- Horizontal: Partitioned based on rows.
- Vertical: Partitioned based on columns.
-
Use Case:
- Horizontal: Useful for handling large volumes of data with many rows.
- Vertical: Beneficial when the table has many columns, and different queries access different subsets of these columns.
-
Performance Optimization:
- Horizontal: Can significantly improve query performance in scenarios with large datasets by reducing the number of rows scanned.
- Vertical: Can improve performance by reducing disk I/O when only a subset of columns is frequently accessed.
-
Scalability:
- Horizontal: Enhances scalability by distributing the data across multiple servers (shards).
- Vertical: Can aid in scalability by allowing different columns to be stored and managed separately, but it's more about optimizing access patterns than scalability.
In practice, databases can use both horizontal and vertical partitioning in tandem, depending on the specific needs of the application and the nature of the data being stored.
TAGS
Data Partitioning
System Design Fundamentals
System Design Interview
CONTRIBUTOR
Design Gurus Team
GET YOUR FREE
Coding Questions Catalog
Boost your coding skills with our essential coding questions catalog.
Take a step towards a better tech career now!
Explore Answers
Related Courses
Grokking the Coding Interview: Patterns for Coding Questions
Grokking Data Structures & Algorithms for Coding Interviews
Grokking Advanced Coding Patterns for Interviews
One-Stop Portal For Tech Interviews.
Copyright © 2024 Designgurus, Inc. All rights reserved.