SQL Server and the Plan Guides, An easy way create and utilize!
First of all I must say a big thank you for the first five thousands visualizations of this my Blog. I am really very proud of it! Thank you so much!
I hope you enjoy this post!
Introduction
We have seen that parameter sniffing is not necessary a bad thing.
Today for this first post of february I will show you a way to solve this problem through the creation of a Plan Guide
However, before talking of Plan Guide, we do also a small summary about unevenly distributed data.
Let's start!
Recap about not evenly distribuited data
Suppose we have two tables where we store our orders.
OrdTes represents the header of the documents while OrdRig its rows.
A document can have one or more lines.
Suppose that the data is not distributed evenly.
What does it mean?
It means that if i execute the query below with the parameter @P1 equal to 100 the Query returns 300.000 Row
exec sp_executesql N'select ft.id,SUM(fr.Qta1) from OrdRig fr Join OrdTes ft on fr.IDORDTES = ft.id where fr.QTA1 = @P1 group by ft.id
',N'@P1 int', 100
exec sp_executesql N' select ft.id,SUM(fr.Qta1) from OrdRig fr Join OrdTes ft on fr.IDORDTES = ft.id where fr.QTA1 = @P1 group by ft.id
',N'@P1 int', 10
But why?
Because the execution plan of the first query executed will be stored in the plan cache and this execution plan will be reused for all the subsequent queries.
However note that two Queries have different execution plans.
The Plan Guide
What is a Plan Guide?A Plan Guide is essentially a way to modify the execution plan of a Query.
Through a plan guide we can apply to our query a series of hint.
Let's see how to create a Plan Guide.
First of all capture your Query execution through the SQL Server profiler.
Remember to specify these options:
Start the SQL Server Profiler and execute your Query from the management studio:
Exec sp_executesql N'select ft.id,SUM(fr.Qta1) from OrdRig fr Join OrdTes ft on fr.IDORDTES = ft.id where fr.QTA1 = @P1 group by ft.id
',N'@P1 int', 100
In this case (in blu) i have specified the recompile option.
EXEC sp_create_plan_guide
@name = N'RICALCOLO',
@stmt = N'select ft.id,SUM(fr.Qta1) from OrdRig fr Join OrdTes ft on fr.IDORDTES = ft.id where fr.QTA1 = @P1 group by ft.id
',
@type = N'SQL',
@module_or_batch = NULL,
@params = N'@P1 int',
@hints = N'OPTION (RECOMPILE)'
GO
Then execute it!
But now, how to verify that your queries are using the plan guide?
Again using the SQL Server Profiler.
Just runing your query again.
You must see, inside the profiler, a row with the name of your plan guide.

Yes our plan guide is effectivery used.
Now using the RECOMPILE OPTION your Queries will be always recompiled each time it will be executed.
As usually each solution has its own PROs and CONs.
The PRO is that every time you will execute the Query you will have the right execution plan.
The CONS is that generating the execution plan whenever is expensive and time consuming.
Another solution..
Through a plan guide we could force other behaviors for example using hints such as ForceSeek.
Finally, for the sake of the completeness, you can view your plan guides in the menu Programmability / Plan Guide:
Luca Biondi @ SQLServerPerformance blog!
Previous post: SQL Server, How to find deadlocks: the easy way DBCC TRACEON (1204, 1222,-1)
A song with the mood of the day: U Can't Touch This





nice post.
ReplyDeleteData Science training
linux training
mulesoft training