r/SQL • u/OnepieceKing2222 • Sep 16 '26
Discussion What SQL query optimization trick made the biggest difference for you?
I've been working with SQL for a while and recently started paying more attention to query performance rather than just getting the correct result.
What's one SQL optimization technique that made a noticeable difference for you?
For example:
- Indexing
- Avoiding unnecessary joins
- Window functions
- EXISTS vs IN
- Reading execution plans
- CTEs/subqueries
- Partitioning
Would be interested in hearing some real-world examples where a query went from slow to significantly faster.
119
Upvotes
View all comments
Show parent comment
2
u/TokenSDragon Sep 16 '26
This is curious to me, what model? I tried GPT and it was a donkey butt. It optimized the ‘text’ which made the actual query garbage. But Claude (Sonnet 5) dang, it seemed to actually understand the intricacies of the optimizer in MSSSL 2019+. I threw a monster at it that I had worked on multiple times over the years that involves a nasty, horrible table schema we do not have control of (even to reindex) and my god…. It cut latency by about half. Shocked I tell you! This is a query that has to aggregate 200,000+ records across two tables not well suited for joins against their indices. I consider myself a SQL-whisperer and this blew my mind.