Databricks

Databricks Performance Optimization: How a More Expensive Cluster Cut Our Cost Per Run by 47%

Summary

Databricks performance optimization starts with cost per run, not cost per hour. In Databricks you pay for the hourly rate multiplied by the minutes the job runs. On a recent client pipeline we moved to a node type that costs 33% more per hour, and each run got 47% cheaper because it finished 2.5 times faster. That only worked because we fixed the real bottleneck first: a sort that was spilling around 219 GB to disk.

Last Updated

23 Nov 2026

Published

23 Nov 2026
A long road winding across a valley floor beneath a straight modern viaduct at sunset, a metaphor for a faster route that costs less per run.

Short answer: In Databricks you pay for the hourly rate multiplied by the minutes the job runs. On a recent client pipeline we moved to a node type that costs 33% more per hour, and each run got 47% cheaper because it finished 2.5 times faster. That only worked because we fixed the real bottleneck first: a sort that was spilling around 219 GB to disk.

TL;DR

  • The pipeline took 2 hours 45 minutes. After two changes it runs in about 30 minutes.
  • Fix one, the query: the slowest view spent most of its 2.24 hours in a sort that spilled ~219 GB to disk. Replacing a DENSE_RANK over a wide table with a straight max comparison took that view to 23 minutes, and the pipeline to 1 hour 15 minutes.
  • The diagnosis that came next: the event log showed all eight task slots at 100% for nearly the whole run. Views that looked hung were queuing for a free slot.
  • Fix two, the hardware: same number of workers, one node generation newer, with local NVMe disk. The hourly rate went from 2.25 to 3 DBU/h (+33%).
  • The result: the run went from 1h15 to 30 minutes, and its cost from 2.8 to 1.5 DBU (−47%), about $1.52 to $0.81.
  • The lesson: choosing compute on the hourly rate alone optimises the wrong number. Measure the cost of one run.

Why was the pipeline slow when the code looked fine?

This was a client pipeline that turns raw ingested data into cleaned tables. Nothing was broken. It just took 2 hours and 45 minutes, every run, and the team had stopped questioning it.

The usual reflex is to add workers. We didn't. Before changing any compute, we wanted to know where the time actually went, because adding nodes to a job that is stuck on one stage mostly buys you idle nodes.

So we opened the Spark UI and sorted the stages by duration. One view stood out: 2.24 hours on its own. And almost all of that time sat in a single sort.

How we found and fixed the spill to disk

Spill happens when Spark runs out of execution memory during a shuffle, join, sort or aggregation and writes the overflow to disk. Databricks shows it at the top of the stage page in the Spark UI. In our case the number was hard to miss: around 219 GB spilled in that one sort.

The sort came from a DENSE_RANK window function over a wide table. The query ranked every row so that it could keep the latest one. To rank, Spark has to sort the whole partition, with every column of the wide table attached to every row. That is a lot of data to hold in memory, and when it doesn't fit, it goes to disk.

But the logic never needed a full ranking. It needed the maximum value. So we replaced the rank with a straight max comparison: work out the maximum per key, then keep the rows that match it. Same result, and no full sort of the wide rows.

  • The heaviest view went from 2.24 hours to 23 minutes.
  • The full pipeline went from 2h45 to 1h15.

This is the first thing we check on any slow Databricks job now. If a window function ranks rows only to keep the top one, there is usually a cheaper way to say the same thing.

What did the Databricks event log show?

1h15 was better. It still felt slow, so we looked at the pipeline's event log rather than at individual queries.

The cluster had eight task slots, and for nearly the whole run all eight sat at 100%.

That changed how we read the run. Several views had looked like they were hanging. They weren't. Their work was done, and they were waiting in a queue for a free slot just to be marked complete. Nothing in the code was wrong. The cluster simply had no headroom.

At that point there were two obvious choices. Add more workers, or make each slot faster. Spill was still part of the workload, and spill is a disk problem, so we went for faster nodes with faster local disk.

Why a more expensive Databricks node type made each run cheaper

We changed the node type and nothing else in the cluster configuration:

  • Same number of workers. We did not touch that.
  • One generation newer. A newer processor, and local NVMe storage, because spill to disk was the bottleneck we still had.
  • A higher hourly rate. The cluster went from 2.25 to 3 DBU per hour. That is 33% more expensive per hour.

On paper we had just made the pipeline more expensive. Then we looked at what one run actually cost.

Cost of one run, before and after the node swap
Before node swapAfter node swap
Run time1h 15m30m
Cluster rate2.25 DBU/h3 DBU/h (+33%)
DBUs per run2.81.5 (−47%)
Cost per run at $0.54/DBU~$1.52~$0.81

The run became 2.5 times shorter. A rate 33% higher, multiplied by a run 60% shorter, is a run that costs about half as much.

What you pay for is the rate multiplied by the minutes, and the minutes are the part nobody puts on the slide. Teams compare node types on the price list, which only shows the rate. The run time only shows up when you test.

These figures cover the Databricks charge only. The cloud provider bills the virtual machines separately, and that charge also moves with run time. For how the two fit together, see how Databricks pricing works.

What does this mean for Databricks performance tuning?

Two things, and the order matters.

Fix the work before you buy faster hardware. The node swap saved 45 minutes. The query fix saved 90. If we had swapped nodes first, the newer hardware would have spent its life spilling a 219 GB sort to faster disk, and we would have called it a hardware win. Spill, skew and unnecessary sorts are cheaper to remove than to accelerate.

Then choose compute on cost per run, not cost per hour. Once the query was right, the hourly rate was the wrong number to optimise. For a batch pipeline that is saturating its slots and still touching disk, a newer, more expensive node can be the cheaper choice. The only way to know is a test run: price one run on each node type and compare.

A short checklist we now use:

  • Sort stages by duration in the Spark UI, and look at the longest one first.
  • Check spill on that stage. If it is large, look for sorts and window functions you don't need.
  • Read the event log for slot utilisation before you add workers.
  • Price a test run on the candidate node type: DBU/h × run time = DBUs per run.
  • Compare DBUs per run, never DBU/h alone.

For the wider picture, we have written about Databricks cost optimization without slowing teams and about Databricks performance tuning at scale.

Free resource: the Databricks Cost-Optimisation Checklist. The checks we run before recommending any compute change, on one page. Download it

The Cosmos Thrace perspective

What surprised us was not the node swap. It was how long a 2h45 pipeline had been accepted as normal. Nobody had asked what a single run cost, because the bill arrives monthly and per workspace, not per pipeline.

Across the platforms we work on, the pattern repeats. Cost reviews start from the invoice and end with a list of clusters ranked by spend. That list tells you where money goes. It doesn't tell you which jobs are slow for a reason you can remove. The two biggest savings here came from reading the Spark UI and the event log, not the bill.

We also changed our own advice. We used to treat a higher hourly rate as something to justify. Now we treat it as a hypothesis to test: run it once, measure the run, and let the number decide.

If you want a second pair of eyes on a slow or expensive pipeline, our cost-optimisation work starts exactly where this one did: one pipeline, its slowest stage, and the cost of a single run.

Sources

How do I fix spill to disk in Databricks?
Is a more expensive Databricks cluster ever cheaper?
How do I calculate the cost of a single Databricks job run?
Should I add more workers or use a bigger node type?
Why is DENSE_RANK slow on wide tables in Spark?
What is Databricks performance optimization in practice?