SQL Server and the Plan Guides, An easy way create and utilize!
Hi Guys!
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!
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!
While, if i execute the same query with the parameter @1 equal to 10 it returns just 1000 rows.
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.
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:
Then take a look to the SQL Server profiler :
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.
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!
Today i will show you how to create a Plan Guide in order to resolve, for example, parameter sniffing issues.
I hope you enjoy this post!
Introduction
It was November when we spoke about parameter sniffing (read here. )
We have seen that parameter sniffing is not necessary a bad thing.
Parametrization sniffing, instead, could be a problem if your data is distribuited unevenly! 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
With unevenly distribuited data we could have sniffing problems.
But why?
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.
So how to resolve this problem if you can’t change your Query?
Suppose infact that your Query is inside an application that you can’t modify..
The solution in this case is to create a plan guide in order to force the Query optimizer to recompile your Query at each execution.
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
Now Copy exactly (spaces included) the text as returned by the profiler and paste it in the @stmt and @params parameters.
In this moment you can specify your options in the @hint parameter.
In this case (in blu) i have specified the recompile option.
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!
Compliments...your first plan guide is now created!
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:
That's all for today!
I hope you enjoyed this Post
If so remember to subscribe to the blog please!
Luca Biondi @ SQLServerPerformance blog!
Next Post: SQL Server, Sub-Queries vs. Cross Apply
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
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