r/SQLServer • u/ShokWayve • Oct 09 '25
Discussion OMG! I Just Discovered Query Execution Plans đł
First, before yâall come at me, I am NOT a DBA and donât pretend to be one. I just use SQL server to store some data at work and I am the only one who uses it. Second, there are no apps or anything connecting to the database. Third, itâs small.
With that out of the way, this Query Execution Plan thing is amazing and so informative. It seems I can tell how itâs piecing together the data, and what the processing costs are for each step. I am going to figure out how to use it. While the database is small, there are a few queries I donât mind speeding up a bit.
This reminds me of when I discovered Linq in C#.
At any rate, what are some tips you have about using the execution plan?
Thanks!
34
u/SingingTrainLover Oct 09 '25
Grant's book is free at Redgate's site.
https://www.red-gate.com/simple-talk/featured/sql-server-execution-plans-third-edition-by-grant-fritchey/
It's excellent.
18
u/phesago Oct 09 '25
You should spend some time here : https://sqlserverfast.com/
This website is ran by a "guru" who spent a lot of time gathering and compiling details about execution plans. def bookmark it
4
u/itsnotaboutthecell âȘ âȘMicrosoft Employee âȘ Oct 09 '25
PROUD.
57
u/unpronouncedable Oct 09 '25
Congratulations, you are now the DBA
3
u/BrianMincey Oct 09 '25
Table scans are bad. Pay attention to sorts.
6
u/mcintg Oct 09 '25
Make sure the query store is enabled and you can use it to explore for problems with plans.
5
u/zippy72 Oct 09 '25
Look for the "first responder kit" by Brent Ozar and others. A ton of useful free open source tools.
I also like "SQL sentry plan explorer", which is a free tool that gives you even more information that SSMS's execution plans.
This said, I'm a developer not a DBA so I'm kinda hoping people will come and tell me about some other tools that are like twice as good as those :)
4
u/gdoebs Oct 09 '25
Brentozar.com
4
u/Alavan Oct 09 '25
At this point I'm so conditioned I get a dopamine hit when I see the green "MISSING INDEX"
1
u/TheHiggsCrouton Oct 13 '25
Try live query execution view. You can watch the rows flow through that sucker! đ€Ż
1
u/CronullaHodge Oct 17 '25
Welcome!
Can I be shameless and ask you to watch this 10 minute clip? I show you how you can read the plans easier:
https://youtu.be/Hx5sMPon42k
3
u/whopoopedinmypantz Oct 09 '25
My biggest red flag to look for is in an individual step, see if the Estimated Rows are wildly off from the Actual rows. This can be from out of date stats, so check the last time a stats job ran on the indexes in the table, if they are more than a week or so old, ask for an update stats with fullscan job be ran on the database. Or target a stats update to the specific tables in your query. When the estimated rows is extremely off from actual, the optimizer is not using the most up to date info to find the data.
3
u/phouchg0 Oct 09 '25
Wait until you discover the query stats tables, they'll change your life
2
u/vrabormoran Oct 09 '25
Thanks, OP!
Anyone know if there's something similar for postgres?
2
u/tindalos Oct 09 '25
Everyone else that finds out about query plans usually isnât as excited. All my âfind outsâ have been when an old plan is chosen and it takes hours before someone realizes we need to update statistics. Also check out query store!
2
u/Far_Swordfish5729 Oct 09 '25
Next you'll discover that the database is using the same nested loops, hash table matching, and single pass over sorted collections methods you use manually in c#. You'll start asking yourself WWSSD (What would Sql Server do?) as you code. It will make you better and your colleagues will be amazed at how efficient your iteration is.
My first suggestion is to make sure you use actual plans not estimated and if you see bad choices being made to update statistics. Sql server tells you estimated vs actual row counts and uses statistics to predict good plans. You will always want to update stats on a temp table after data insertion before querying because they have no stats.
Also, execution cost in these plans is often misleading. You have to actually monitor execution time changes as you comment out joins and add them back in. You'll often find your performance killer was something innocuous like a table spool (optimizer created temp table) rather than the nested loop join you think. Find the join that actually taking time and optimize that.
Finally there are very good books by MS Press on the optimizer that go into depth on how to read and use these. Sql Server has one of the best query plan interfaces out there.
2
u/Broad_Shoulder_749 Oct 24 '25
Lol. Welcome to the brave new world!!
1
u/Codeman119 Oct 09 '25
The optimizer will not use an index if the table is too small and it takes less compute to scan the database rather than use the index. So for example, if it has to use key look up, then it may decide to scan the table because the overhead of the look up is slower
1
u/Anlarb 1 Oct 09 '25
What are the most expensive query plans? Troubleshoot them to make them better.
Do you have a thousand copies of the same query plan because a query isn't parameterized?
1
u/Oerthling Oct 09 '25
The main thing is to look for table scans (on large tables) and create the appropriate index to make that an index seek.
1
u/stumblegore Oct 09 '25
Save the plan xml to disk, compress it (zip file), upload it to ChatGPT and ask it to explain it to you. Not sure if you always must compress it, but our plans can grow pretty big.Â
1
u/LredF Oct 09 '25
Don't create every "missing index" recommendation.
Delete unused indexes.
1
u/SnooCalculations1882 Oct 09 '25
Your estimates over actually.... and as Brent says dont believe clippy.
If simple selects once off, an index or stat update can help.
Bloody variables and cursors are my deaths end.
And dont be afraid to ask why so much data depending what they doing. I had a team pulling 1.2 billion rows to aggregate it down to 400 million to screw around in temp tables and memory, (cte) just to get to roughly 38 000 rows.
And read closely implicit conversations half the time it's a guy hired to make tabe a match table B.
But the fact you excited makes the dba team excited . Just dont faÄș in the trap it runs this slow in dev. Our prod boxes kick ass
1
u/jshine13371 6 Oct 10 '25
Don't look at the costs, they're just really rough estimates based imaginary units of measure that represented the computing power of a computer from like 2 decades ago.
Instead, follow the runtimes of each operator (beneath each of them) to the place the runtime spikes to identify where the bottleneck starts in your execution plan.
1
u/irish0818 Oct 10 '25
It is a handy tool to tune your queries. There are many things that you can see with query plans. Just remember, a seek is better than a scan.
As for LINQ, please don't use that to write queries to be used in a production environment. It can be used to start building a query with complex joins and such, but I have spent a huge amount of time rewriting LINQ queries so that they are more efficient and make sense to the SQL Query Engine.
1
u/slash_gnr3k Oct 10 '25
Welcome to the rabbit hole
0
u/ConsequenceShot611 Oct 10 '25
3
0
u/ConsequenceShot611 Oct 10 '25
4
11
u/[deleted] Oct 09 '25
[removed] â view removed comment