r/SQL • • 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

80 comments sorted by

View all comments

Show parent comment

2

u/phunkmunkie Sep 16 '26

Table variables…. Ugggh. Unless you are in a function, do not use. And if you need one in a function, rethink it.

1

u/data_addict Sep 17 '26

Wrong with respect to redshift. u/Eleventhousand is right here. Temp tables make a tremendous difference be CC cause you can redistribute/sort data within the query as needed

2

u/phunkmunkie Sep 17 '26

Temp tables yes. 100%. Table variables, no. I only brought up functions because you can’t use temp tables there.