SQL Like a Pro: Query Tuning #1

If you ever got fed up with queries that run for hours… this one is for you!

Joao Marques @ Data Beyond Ltd
5 min readFeb 14, 2022
Photo by Veri Ivanova on Unsplash

I started tuning SQL queries very early in my career, being that ETL scripts or business user queries. There was something highly rewarding about taking a SQL query runtime from 10 minutes to 30 seconds, and deep diving into how the database worked, understanding its core query planner behaviours and how to adjust the queries for performance was fascinating. As my career went on, tuning was always part of my work, and I can honestly say I must have tuned hundreds of queries, and saved hundreds of hours of compute.

As the data world move towards “pay for compute” models (snowflake, big query, redshift serverless, spark, …), tuning queries can now be measured in dollars and pounds, and that is even more motivating. Just this week, I tuned a Snowflake query from $3,000/year to $180/year, how cool is that!

The challenge with query tuning though is only a very small set of rules actually work everywhere… that made me think it would be more interesting to show you a few real world examples, rather than generic abstract rules…

--

--

Joao Marques @ Data Beyond Ltd

Just a technical Data Architect having too much fun building new data platforms! Want to support my writing? Why not BuyMeACoffee.com/joao.marques.db