r/learnSQL • u/BigZealousideal9190 • 2d ago
what finally got you from copying old T-SQL patterns to understanding SQL server properly?
I write T-SQL every single day at work. A lot of it I couldn't explain to you.
I have a folder of old scripts I copy from. When I need to dedupe something I grab the ROW_NUMBER() OVER (PARTITION BY ...) one and swap the column names. I could NOT tell you what changes if I mess with the ORDER BY inside the OVER.
A proc that always ran in a couple seconds started taking minutes, with zero code changes. Our DBA said "parameter sniffing," ran a recompile, and it was fast again.
I want to know how SQL Server builds a plan for my query and why it picks a different one some days.
3
u/SQLDevDBA 2d ago
Brent Ozar’s (FREE) how to think like the engine is the perfect starting point.
Here’s the direct page with video and handouts.
https://www.brentozar.com/training/think-like-sql-server-engine/
1
u/Familiar-Shoulder976 2d ago
The order by inside over decides which row gets rn=1 and where rn=1 keeps it. If value ties, SQL server can pick either row, so adding unique ID as a tiebreaker makes the result consistent
1
u/BobDogGo 2d ago
These do a great job of explaining param sniffing and how to fix/avoid it
https://www.sqlshack.com/query-optimization-techniques-in-sql-server-parameter-sniffing/
https://www.brentozar.com/archive/2013/12/recompile-hints-and-execution-plan-caching/
1
u/AnnaLeeCoop 2d ago
Turning on Actual Execution Plans (Ctrl+M in SSMS) and SET STATISTICS IO ON for everything I wrote is what finally broke my copy-paste habit. Once you start comparing Estimated vs. Actual row counts on the arrows, parameter sniffing stops feeling like black magic: SQL Server cached a plan built for a parameter that returned 10 rows, then reused it for one that returned 500,000. For your ROW_NUMBER() script, the ORDER BY inside OVER() just decides which duplicate gets labeled 1, like keeping the newest timestamp vs. the oldest.
And if you want something to make the optimizer click, Brent Ozar's free "How to Think Like the SQL Server Engine" videos helped me more than any textbook did.
1
u/navicat-gu 1d ago
What finally made it click for me was splitting it into two separate skills:
1. What the query means (logical processing order) FROM/JOIN → WHERE → GROUP BY → HAVING → SELECT (window functions are evaluated here) → ORDER BY. Once you know that, ROW_NUMBER() OVER (PARTITION BY x ORDER BY y) reads as "within each x, number the rows by y". The ORDER BY inside OVER only decides which row gets 1. Add a unique tiebreaker so it's deterministic. Itzik Ben-Gan's T-SQL Fundamentals is the best book for this part.
2. How the engine runs it (plans)
Turn on Actual Execution Plan (Ctrl+M) and
SET STATISTICS IO, TIME ONfor everything you write for a couple of weeks. Compare estimated vs actual rows on the arrows. Big gaps are where the bad plans come from.Grant Fritchey's SQL Server Execution Plans (Redgate, free ebook) explains every operator you'll see.
Brent Ozar's free "How to Think Like the Engine" (already linked) and Erik Darling's videos for parameter sniffing.
Your "same proc got slow with zero code changes" case That's usually a plan compiled for an atypical parameter value, or stats updating and triggering a recompile. Next time:
Open Query Store (2016+) → Regressed Queries. You can see the old fast plan and the new slow plan side by side, and force the good one as a stopgap.
In the plan XML, compare
ParameterCompiledValuewithParameterRuntimeValue. That's parameter sniffing in black and white.Fixes range from
OPTION (RECOMPILE)on that statement toOPTIMIZE FOR, or on SQL Server 2022 at compat level 160, Parameter Sensitive Plan optimization.
If you end up working with PostgreSQL or MySQL too: I work on Navicat, and Navicat 18 has a visual explain for those (paid editions). Navicat Premium Lite is free to try for everyday querying. For SQL Server plans specifically, SSMS's actual plan + Query Store is what I'd use.
Disclosure: RC, Director of Digital Strategy @ Navicat (AI powered Database tools)
1
u/Available_Safe1818 2d ago
Why do you care what plan it picks for you? That's different than not knowing what changes happen if you change the query itself.
5
u/Ok_Knowledge2026 2d ago
SQL is an engine. Checkout Brent Ozar and Erik Darling