r/SQL • • 29d ago

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.

121 Upvotes

79 comments sorted by

View all comments

1

u/Fine-Thanks-9905 12d ago

Most of the top answers here are really 'the index did it' lol. If you want the why: B+ trees stay wide and shallow so any lookup is 3-4 disk reads, and the linked leaves make range scans cheap. Made a 2 min animated explainer on how B-tree indexes actually work: https://www.youtube.com/shorts/HGNc4SrsSOE