Reset SQL Server execution plan - sql-server

I've looked all over for this command....what's the command to reset the SQL Server's execution plan?

For clarity..........
Executing sp_recompile will "mark" the given stored procedure for recompilation, which will occur the next time it is executed.
Using the WITH RECOMPILE option will result in a new execution plan being generated each time the given stored procedure is executed.
To clear the entire procedure cache execute
DBCC FREEPROCCACHE

For stored procedures, you use the WITH RECOMPILE option.

If you want to reset QEP for a stored procedure, you shall use sp_recompile

It's not entirely clear from your question what you're after. But in addition to the other suggestions, DBCC FREEPROCCACHE clears all cached execution plans.

sp_recompile will dump the existing query plan and recompile the procedure.
Or you can restart SQL and that will clear the entire execution plan cache.
WITH RECOMPILE is going to generate a new plan EVERY time you execute it.

Related

How to clear sqlserver cache to get correct execution plan

I had a query that was running slow (2.5 mins) on sqlserver.
I got actual execution plan, and there was a suggestion for an index. I created the index and now execution time is < 2 seconds.
Then we had to restart sql server.
Query went back to being slow (2.5 mins), again, I looked at execution plan. This time there was a suggestion for a different index!
It would appear that first execution plan index suggestion was taking into account some sort of cached index maybe?
How can I clear cache (if this is the issue) before looking at execution plan?
The symptoms suggest parameter sniffing, where the query plan was generated for the initially supplied parameter values but the plan is suboptimal for subsequent queries with different values. You can invalidate the currently cached plan for specific query by providing the plan handle to DBCC FREEPROCCACHE:
DBCC FREEPROCCACHE(plan_pandle);
There are a number of ways to avoid parameter sniffing. If the query is not executed frequently, a recompile query hint will provide the optimal plan for the parameter values supplied. Otherwise, you could specify an optimize for unknown hint or use the Query Store (depending on your SQL Server version) to force a specific plan or have SQL Server automatically identify plan regression and select a plan.
Dont clear the cache in PRODUCTION environment. It will lead to serious performance issues.
If you want to generate new plan instead of existing plan, you can go for RECOMPILE option as part of stored procedure execution to see whether new index is being considered in the new plan.
EXEC dbo.Procedure WITH RECOMPILE;
or you can regenerate the execution plan for the procedure, by using the below command. Next time, it will be using the newly generated plan.
EXEC sp_recompile `dbo.procedure`
If you want to measure performance improvement repeatedly in a test environment, you can go with below clearing approaches:
DBCC FREEPROCCACHE -- It will clear the plan cache completely
DBCC DROPCLEANBUFFERS -- It will clear the unchanged data brought from disk to memory.
More elegant approach is to write the dirty pages to disk and then issue the cleaning of unchanged data.
CHECKPOINT;
GO
DBCC DROPCLEANBUFFERS;
GO
DBCC FREEPROCCACHE;
GO

SQL Server Stored Procedures Execution difference

I have SQL Server 2016. There are two absolutely same stored procedures with different names, but they execute differently. First completes in 17 minutes, other in 18 second.
First was created before Index on one of elements was created, but it was recompiled and cleared all plans from the plan cache.
The result does not change.
What problem it can be and how reconfigure execution plan?
To improve performance of stored procedure must be executed script lower. It can help to recreate execution plan.
EXEC sp_recompile N'ProcedureName';
GO
DBCC FREEPROCCACHE WITH NO_INFOMSGS;
GO
EXEC sp_updatestats;
GO

SqlServer - OPTION RECOMPILE parallel execution behavior

I need clarification on how SQLServer behaves when you execute a parameterised procedure with OPTION RECOMPILE in parallel.
Option Recompile:
"You can force SQL Server to recompile the stored procedure each time it is run. The benefit here is that the best query plan will be created each time it is run."
My question is, when executing this procedure in parallel,
will the best query plan be created for each concurrent procedure executed or will it use 1 plan?
The best query plan will be created for each concurrent procedure executed in parallel.
This plan is only used by the session and not added to the plan cache.
It would be a bug if the plans could be used between sessions as OPTION (RECOMPILE) can perform simplifications based on the parameter values that are not generally applicable to all possible parameter values.

Does updating statistics recompile stored procedures in sql server

Does updating statistics recompile stored procedures in sql server or even after updating statistics( Auto or manual) the procedures run with the same execution plan it first compiled with?
MSDN has a lengthy article on that. To sum it up:
Therefore, plan optimality-related reasons have close association with
the statistics.
Looks like it depends on how much the statistics changed. So updating statistics may lead to a recompile but does not have to. To force removal of all cached query plans, you can run:
DBCC FREEPROCCACHE

DROPCLEANBUFFERS does not work in SQL Server

I have a stored procedure which I am trying to optimize. In order to know how much time is spent for an execution I have added at the beginning of the script:
use MyDatabase
go
CHECKPOINT;
GO
DBCC DROPCLEANBUFFERS;
GO
between two execution I can see the time to run the query decreasing. I thought only cache could fasten the execution of my script, are there other mechanism in SQL Server to do the trick?
thanks,
The first time you run a stored procedure, the query plan is calculated and compiled. This typically takes 20ms, can be a bit more for a long procedure.
If you run the query once, before checkpoint; dbcc dropcleanbuffers;, the plan should be cached.

Resources