What are the differences between horizontal and vertical partitioning in a database?

Image
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 columns UserID, Name, Email, and Country.
    • 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.
Image
Horizontal Partitioning vs. Vertical Partitioning

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 and Name.
      • Another table with UserID, Email, and Country.
    • 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.

Key Differences:

  1. Partitioning Criteria:

    • Horizontal: Partitioned based on rows.
    • Vertical: Partitioned based on columns.
  2. 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.
  3. 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.
  4. 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
Explore Answers
Related Courses
Image
Grokking the Coding Interview: Patterns for Coding Questions
Image
Grokking Data Structures & Algorithms for Coding Interviews
Image
Grokking 75: Top Coding Interview Questions