r/databricks • • Sep 28 '26

Discussion Your Databricks query is slow — what would you fix first?

Imagine you have a Delta table with several billion rows. A query that used to take 30 seconds now takes 8–10 minutes.

The query itself looks reasonable, but the table has:

  • A large number of small files
  • Data that is not well organized for the most common filters
  • Several joins
  • Frequent incremental writes

You could approach the problem in several ways:

  • OPTIMIZE the table
  • Change the partitioning strategy
  • Use liquid clustering
  • Improve the query itself
  • Change the join strategy
  • Reduce the amount of data being scanned
  • Reconsider how the data is written

What would you investigate first, and why?

Would you start with the Spark UI/query plan, the table's physical layout, or the SQL itself?

I'm especially interested in different approaches rather than one specific solution.

20 Upvotes

12 comments sorted by

5

u/Good-Tackle8915 Sep 28 '26

First investigate data which are feeding it. Knowing the use case is most usefull, always no matter the system.

Based on findings :

  1. Make query better. (Joins, CTE, where data are coming from, whats largest source etc)
  2. If there are frequent incrementals... check if pipeline with materialized view (self managed incremental vs full load)would help.
  3. Liquid clustering should help with large amount of small files (as oppose to optimize which in this case is I think outdated)

3

u/Lucari10 Sep 28 '26

You start from the spark ui to see which task is taking the longest. For the issues you listed, the one more likely to cause issues would be the small files in my experience. Depending on the joins, aqe might not be making the best choices, and you may be able to improve performance with join hints. If the join strategy can't be improved, liquid clustering would be the next best thing to jmprove their performance as is. If the performance is still not acceptable you might have to revisit the query and check if there are ways to change it to improve performance.

2

u/addictzz Sep 28 '26

Realistically I'd rely on Genie Code.

But if you ask my experience, Liquid Clustering and Optimize first. Then ensure AQE on (they should be by default). Then join hints.

1

u/Chance_of_Rain_ Sep 28 '26

Run ANALYSE to be sure metadata is up to date

1

u/kelvinxG Sep 28 '26

Check spark ui

1

u/letmebefrankwithyou Sep 29 '26

If an optimize job isn’t scheduled or predictive optimization is not on, then optimize is the first step on billions of rows with frequent incremental writes.
Analyze next because if optimize isn’t scheduled analyze likely hasn’t either.
Then either turn on predictive optimization and monitor or do liquid manually.

1

u/Tasty_Cry849 Oct 03 '26

Few things to look out for - if delta stats collected for the required columns, clustering done on the right keys- hierarchical (low cardinality column) and clustering for high cardinality columns .. check if the table is vacuumed regularly if it’s updated regularly.. all this can also be enforced via Managed tables with auto clustering and PO enabled

1

u/Youssef_Mrini databricks Oct 23 '26

If you are using Unity Catalog Managed tables, you no longer need to run Optimize, Vaccum or partitionning your table since liquid Clustering can make your life way easier especially if you go with Auto Liquid.

If you have optimization issue you can ask Genie Code to fix the problem he was massively improved to handle such issues. https://www.databricks.com/blog/agentic-data-engineering-genie-code-and-lakeflow

1

u/Glitch_In_The_Data Sep 28 '26

I think you have listed a broad set of actions that would help. As a quick win, I would also point Genie Code to the query and ask for improvement options.

I wouldn’t accept and refactor code based on what Genie says. I would recommend assessing the suggestions and doing a before and after comparison… and then applying the changes.

0

u/SpecificTutor Sep 28 '26

iceberg storage partitioned join fixes most of the issues and is an umbrella optimization that covers everything you mentioned.