Skip to the page

Databricks interview question · medium

Thanos notices a long-running query against a Delta table in their Databricks SQL endpoint. The query plan suggests a full table scan. What Databricks-specific configurations or data organization techniques (beyond basic partitioning) should be investigated to improve query performance?

The short answerThanos is right to investigate further if a full table scan is occurring on a Delta table, especially for long-running queries.

The full answer

Thanos is right to investigate further if a full table scan is occurring on a Delta table, especially for long-running queries. Beyond basic partitioning, several Databricks-specific configurations and data organization techniques can dramatically improve query performance:

1. Z-Ordering (Multi-dimensional Clustering):

  • How it works: Z-Ordering co-locates related data on disk based on the values in specified columns. This significantly reduces the amount of data that needs to be read for queries that filter on those columns.

  • When to use it: Ideal for queries with filters on multiple columns, especially if those columns are frequently used together in WHERE clauses.

  • Implementation:

    OPTIMIZE your_delta_table ZORDER BY (col1, col2);
    
  • Example: If queries frequently filter on date and customer_id, Z-Ordering by (date, customer_id) would be beneficial.

2. Data Skipping (Min/Max Statistics):

  • How it works: Delta Lake automatically collects min/max statistics for each data file. When a query filters on a column, Delta Lake uses these statistics to skip reading files that don't contain relevant data.
  • Benefit: Reduces I/O by avoiding unnecessary file reads.
  • Automatic: This is enabled by default. No specific configuration is usually needed. However, ensure that the table is optimized regularly to refresh the statistics.

3. File Size Optimization (Compaction):

  • Problem: Many small files can degrade query performance due to increased metadata overhead and I/O.

  • Delta Lake Solution: Use the OPTIMIZE command to compact small files into larger ones:

    OPTIMIZE your_delta_table;
    
  • Auto Compaction: Enable auto compaction for continuous optimization:

    ALTER TABLE your_delta_table SET TBLPROPERTIES (delta.autoOptimize.autoCompact = true);
    

4. Data Skipping with Bloom Filters (Spark 3.0+):

  • How it works: Bloom filters are probabilistic data structures that can quickly determine if a value is not present in a file. This further enhances data skipping.

  • When to use it: Useful for high-cardinality columns with frequent equality filters.

  • Implementation:

    ALTER TABLE your_delta_table SET TBLPROPERTIES (delta.bloomFilter.enabled = true, delta.bloomFilter.columns = "col1,col2");
    

5. Photon Engine (Databricks Runtime 8.0+):

  • How it works: Photon is a vectorized query engine that significantly accelerates query performance, especially for complex queries.
  • Benefit: Often provides substantial performance improvements without requiring code changes.
  • Enablement: Photon is enabled by default on some instance types in Databricks Runtime 8.0 and above. Check the Databricks documentation for supported instance types and how to explicitly enable it if needed.

6. Liquid Clustering (Databricks Runtime 12.2 LTS and above):

  • How it works: Liquid Clustering is a new table organization technique that offers more flexibility and better performance than traditional partitioning and Z-Ordering. It dynamically adjusts the data layout over time to optimize for evolving query patterns.
  • When to use it: Highly recommended for large tables with complex query patterns or frequent data updates.
  • Implementation: Requires creating a new table with liquid clustering enabled.

Investigation Steps for Thanos:

  1. Check for Z-Ordering: If the query filters on specific columns, check if Z-Ordering is applied on those columns. If not, apply it.
  2. Check for Small Files: Look at the number and size of files in the Delta table's storage location. If there are many small files, run OPTIMIZE or enable auto compaction.
  3. Consider Bloom Filters: If there are frequent equality filters on high-cardinality columns, consider enabling Bloom filters.
  4. Verify Photon is Enabled: Ensure that the Photon engine is enabled (if using a compatible Databricks Runtime).
  5. Evaluate Liquid Clustering: For large and frequently queried tables, consider migrating to Liquid Clustering.

By using these Databricks-specific techniques, Thanos can significantly improve query performance and avoid full table scans on their Delta table.

Last updated 17 January 2025