Insights

An Overview of Sharding in BigQuery and Why It May Offer Advantages Over Partitioning

by Shannon Gantt on Dec 01, 2023

Sharding and partitioning are both data management strategies used in systems like BigQuery. These strategies serve to manage large datasets while also improving query performance. Partitions and sharding each have their own pros and cons, but there are scenarios where sharding can prove to be more advantageous in BigQuery than partitions.

Understanding Partitioning and Sharding

Partitioning involves dividing a table into smaller manageable segments based on a defined column, often a date or timestamp. This segmentation allows for faster querying as the system can exclude irrelevant partitions while processing queries. In BigQuery, this can significantly enhance performance when working with time-based data, enabling the system to focus only on relevant partitions.

Sharding, on the other hand, breaks data into horizontal partitions across multiple tables or databases. Each shard holds a subset of the data based on predefined criteria such as geographical location, customer IDs, or any other discernible pattern. Sharding distributes the workload across different shards, enabling parallel processing and likely faster query execution.

Extracting historical data (via an ETL) is one of the most frequent scenarios where sharding is used. The sharding approach splits the data into multiple tables and provides each table with a naming prefix, such as MYTABLE_YYYYMMDD. This prevents data from being duplicated because each day in a query is in essence a unique table.

An example of a table sharded by month:



BigQuery will consider any two or more tables as date sharded tables if they have any of the following criteria:

  • The tables must all exist in the same dataset
  • The table schemas must all be exactly the same
  • Tables must all possess the same prefix (eg. "campaigns" in "campaigns_20230301")
  • All tables must have a suffix in the format _YYYYMMDD (eg. 20230301)

BigQuery automatically presents date sharded tables as a collective set of tables in the BigQuery Console UI. Rather than being shown a lengthy list of tables, the UI will group them together and add a date filter for easy preview selection:



Advantages of Sharding Over Partitioning

  1. Improved Parallelism:

    Sharding offers enhanced parallelism by distributing data across multiple shards or tables. Queries can be executed concurrently on these shards, leveraging parallel processing capabilities to expedite data retrieval. In scenarios where the workload needs to be distributed across nodes or clusters, sharding can outperform partitioning by allowing for more granular distribution.
  2. Customization and Flexibility:

    Sharding provides flexibility in organizing data based on specific criteria. For instance, in a multi-tenant application, sharding based on customer IDs can isolate data efficiently. Compared to partitioning, this approach offers better data segregation and helps optimize queries with specific subsets of data.
  3. Scalability:

    As data volumes grow, sharding can scale more effectively than traditional partitioning. It enables horizontal scaling by adding more shards, databases, or tables as needed. This scalability ensures consistent query performance, even with increased data volumes.
  4. Reduced Contention:

    In high-traffic scenarios, partitioned data might experience contention due to multiple queries targeting the same partition, which can cause performance bottlenecks. Sharding minimizes contention by spreading the data across different shards, reducing the likelihood of concurrency issues.
  5. Reduced Query Cost:

    Certain query interfaces and visualization tools, such as Looker Studio (formerly Data Studio), can connect natively to sharded tables in BigQuery. This reduces data-associated costs and increases query speed.

When Sharding May Be Preferable

In certain cases, sharding excels over partitioning in BigQuery:

  • Storing and Updating:

    Sharding can make it easier to store and access historical data, while also allowing for reduced costs when adding, dropping, or updating segments of same-type data.
    • For example, if data is sharded by date, then replacing a day's worth of data is accomplished by simply recreating the table for the corresponding date.

  • Complex Data Relationships:

    Sharding is beneficial when data relationships are intricate and require precise segregation.
  • Variable Access Patterns:

    Sharding can offer better performance for queries demanding diverse subsets of data that aren't aligned with a specific partitioning key.

Conclusion

While partitioning remains a powerful technique for managing and optimizing data access, sharding could serve to be a better approach. Sharding offers enhanced parallelism, flexibility in data organization, scalability, and reduction of contention. It definitely stands out in scenarios where more granular control and customization are required. Simply put, in some scenarios sharding can be better than partitioning in BigQuery because it offers greater availability and performance.

Great news! Launchpad ETL makes use of both strategies — sharding and partitioning — as well as clustering. Launchpad ETL offers flexible features, such as selecting between a sharded destination table or a partitioned table. This powerful tool can support multiple scenarios, allowing you to achieve the best results. For example, a majority of our predefined source schemas that support batching will automatically adopt the sharding mechanism, but can be changed to a standardized date partition. Launchpad ETL makes it easy to work with extremely large sets of historical data.

Contact Us

Share this post:
  • Shannon Gantt

    About the Author

    Shannon is head of technology at Calibrate Analytics. With over 24 years of experience focused on delivering technology solutions via a customer-first approach. Having successfully overseen the development and delivery of large-scale applications that span cloud, he is focused on developing creative business intelligence and e-commerce products.