

There is sometimes more to optimisation than preferring seeks over scans and so forth, sometimes an index scan is more efficient than many executions of seek operations, and the cost estimates upon which the percent figures you are looking at are calculated are that (estimates) at best (a useful guide but sometimes far from at all accurate). SELECT += 'EXEC sp_refreshview '''++''''+CHAR(10) FROM sys.objects WHERE IN ('V') ĮXEC execute plans show 80+ percent of the run time is on a clustered index seek so I don't think there is much more I can do to optimise the stored procedures. Use the ALTER PROCEDURE statement to recompile a.
#Stored procedure recompile upgrade#
You can similarly mark them as needing to be reassessed to make sure stored plans and other meta-data is not stale with sp_refreshview, by small modifications to either the cursor or ad-hoc SQL methods shown above: DECLARE NVARCHAR(MAX) = '' You must recompile your stored procedures whenever you upgrade or migrate to or across a major release. Most of the times, reusing a plan saves time.


Explicit recompilation eliminates the need for implicit run-time. When you refer to recompiling a stored procedure, you are basically talking about removing this plan from the cache so that SQL Server will be forced to regenerate a new plan. SELECT += 'EXEC sp_recompile '''++''''+CHAR(10) FROM sys.objects WHERE IN ('P', 'FN', 'IF') ĮXEC I find this form sometimes throws people due to looking set-based but building the string up iteratively, and not being a standard SQL pattern)Īnother set of objects that might be a similar concern here is views. Whenever you add new indexes to a table or modify its structure extensively is a good practice to recompile all stored procedures involved with that table, so. Use the ALTER PROCEDURE statement to explicitly recompile a standalone stored procedure.
#Stored procedure recompile code#
Or you could produce ad-hoc SQL and run that via EXEC, takes less code which might be marginally more efficient: DECLARE NVARCHAR(MAX) = '' Regardless of where OPTION (RECOMPILE) is used at the statement level for an ad hoc query or a statement within a stored procedure and when the RECOMPILE option is used at the procedure level during creation or execution the query text, the plan, and the execution statistics still get captured within Query Store. that access the table will be recompiled at the next execution. To migrate the stored procedure and recompile to create. table will be recompiled the next time it is used. Procedure To migrate the stored procedure without recompiling: Copy the CREATE PROCEDURE statement. You can run sp_recompile on everything by using a cursor to produce ad-hoc SQL for each and run it, if you think that will help: DECLARE C CURSOR FOR (SELECT FROM sys.objects WHERE IN ('P', 'FN', 'IF')) Hi Carl, If you apply sprecompile to a given table, any dependent object of tha.
