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

4

u/TheAmorphous Sep 16 '26

Creating an md file with my schema is a great idea, thanks. Don't know why I didn't think of that sooner.

5

u/colemaker360 Sep 16 '26

Have it use a Mermaid ERD in your markdown. Many markdown previews can render mermaid tripple backtick fences. That way, you can visualize the output too. You can break the model into smaller subdiagrams if you have too many tables. Having something both you can read and the machine can makes all the difference.

3

u/philbgarner Sep 16 '26

Mermaid is a great tip, I've found it works very well as input/context for a prompt since the logic is baked in to the graph.

2

u/Attackontitanplz Sep 16 '26

My problem is nobody else is on board, and i got hundreds of schemas and alot of them dont have data dictionarys so creating context files that explain joins is a series of phone calls with DBA’s to understand how table relationships work etc etc

2

u/TheGoblinPopper Sep 16 '26

I started with that as a foundation... But the AI needs a LOT of handholding to understand the tables and such. If you aren't crazy about documentation, give the AI your metadata files, then ask it to review and perform a Q&A with you to build out the documentation.

Anyone who disagrees and thinks 'AI is good enough' doesn't work with 1200 tables enterprise systems.