Skip to the page
Tecyfy

Databricks interview question · easy

Barbara needs to join data from a Delta table with data residing in a cloud storage location. How can this be efficiently accomplished within a Databricks notebook, considering data access patterns and potential performance bottlenecks?

The short answerRead the files through Unity Catalog (a volume, an external location or an external table), give them an explicit schema, and let Spark broadcast whichever side is small.

The full answer

Read the files through Unity Catalog (a volume, an external location or an external table), give them an explicit schema, and let Spark broadcast whichever side is small. If the join runs repeatedly, load the files into a Delta table first and join two tables.

1. Reach the files through Unity Catalog

Unity Catalog governs cloud storage, so the notebook reads paths it has been granted rather than keys in code:

  • Volumes for files: /Volumes/main/landing/partners/prices/.
  • External locations for paths in your own buckets, such as s3://my-bucket/prices/.

2. Read with a schema, then join

from pyspark.sql import functions as F

prices = (spark.read.format("csv")
          .option("header", "true")
          .schema("product_id STRING, price DECIMAL(10,2), valid_from DATE")
          .load("/Volumes/main/landing/partners/prices/"))

orders = spark.read.table("main.sales.orders")
joined = orders.join(prices, "product_id", "left")

In SQL, read_files does the same, and Databricks recommends it for reading CSV from SQL:

SELECT o.*, p.price
FROM main.sales.orders o
LEFT JOIN read_files('/Volumes/main/landing/partners/prices/',
                     format => 'csv', header => true,
                     schema => 'product_id STRING, price DECIMAL(10,2), valid_from DATE') p
USING (product_id);

An explicit schema avoids an extra pass over the files to infer types, and avoids surprises such as IDs read as integers.

3. Make the join cheap

  • Small file side: Spark broadcasts a small table to every executor so the large Delta table is never shuffled. Adaptive query execution does this automatically at run time when it sees the side is small; F.broadcast(prices) or a BROADCAST hint forces it.
  • Filter early: filter the Delta table on its clustering or partition columns and select only the columns you need before the join, so Delta skips files.
  • Large on both sides: a sort-merge join is normal. Watch the Spark UI for skew (a few tasks much slower than the rest); adaptive query execution splits skewed partitions.
  • Avoid row formats for large data: CSV and JSON are slow to parse and cannot skip data. If the file side is large, convert it.

4. If the join is regular, ingest first

Read the files once into a bronze Delta table with Auto Loader (it picks up only new files), clean them into a silver table, and join two Delta tables. In a Lakeflow declarative pipeline the join becomes a materialized view that refreshes when either side changes. This is faster for every later query, and the file data gains history, statistics and access control.

5. Keep compute near the data

Run the workspace in the same cloud region as the bucket: cross-region reads are slower and add transfer charges.

In short: governed access, an explicit schema, a broadcast for the small side, and Delta tables for anything you join more than once.

Last updated 16 October 2026

Asked next