

This is a great adjustment and SQL Server makes every single time when the stored procedure is executed. When the query needs more resources, it takes more resources and when needed fewer resources, it uses appropriate resources. When we see the execution plan of the queries you will notice that this time the execution plan has a correct estimation of the rows and accordingly the query is consuming the resources which are needed to optimize that particular execution plan. Next, let us run the following two stored procedures with two different parameters. Now enable the execution plan for your query window in SQL Server Management Studio (SSMS). CREATE OR ALTER PROC GetCustomerOrders INT) OPTION (RECOMPILE)įirst, let us create a stored procedure that contains the keyword OPTION (RECOMPILE). It is important to know how each feature works and with the same spirit let us see how option recompile can help us in certain situations. In SQL Server there is no right or wrong answer unless while using production we are facing the negative consequence of it. While many want to argue that this may be very bad advice, there are moments where it has been amazing advice as well. There is only one sure resolution of the performance issue if the SQL Server execution plan is building execution plan which is not efficient for the parameter passed and that is the Recompilation of the stored procedure at every single run with the query hint OPTION (RECOMPILE). It is quite possible that even though we use the average value due to statistics the performance problem has yet not resolved. There is no sure solution to overcome the problem if your stored procedure has sniffed the parameter and used that to build the execution plan. However, it is extremely difficult to overcome the performance problem which is introduced due to the parameter sniffing issue. It is extremely easy to overcome the problem of parameter sniffing.
#Sql server recompile view series#
This post summarizes the entire series of parameter sniffing, performance and recompiling query. Performance and Recompiling Query – Summary.The oldest and most traditional technique to not cache the query plans and compile your stored procedure or queries every single time to get optimal performance. SQL SERVER – Parameter Sniffing and OPTION (RECOMPILE).This new database level enhancement was introduced recently which can help you overcome any issue with the parameter sniffing. SQL SERVER – DATABASE SCOPED CONFIGURATION – PARAMETER SNIFFING.You can take advantage of the new query hint of Optimize For Unknown to simulate the local variable in the stored procedure. SQL SERVER – Parameter Sniffing and OPTIMIZE FOR UNKNOWN.It is easy to overcome the parameter sniffing for the stored procedure by declaring the local variable inside the stored procedure. SQL SERVER – Parameter Sniffing and Local Variable in SP.In this blog post, we discuss what actually is parameter sniffing and how we can overcome it with the help of recompiling the stored procedure. SQL SERVER – Parameter Sniffing Simplest Example.

I strongly recommend that you read my earlier blog posts on this topic before you consider the option of recompile suggested in this blog post.īefore continuing this blog post, I strongly suggest that you read the following blog posts to get an understanding of the background of the concept which I am going to discuss today.

Today is the last blog post of the year 2019 and this is also a final blog post in the series of Parameter Sniffing which I have been writing.
