When Does Recompile Stored Procedures
Normally when a user calls a stored procedure, SQL Server does not create a new data access plan to retrieve the information from the database. The queries used by stored procedures are optimized only when they are compiled. As you make changes to the table structure or introduce new indexes which may optimize the data retrieval you should recompile your stored procedures as already compiled stored procedures may lose efficiency. By recompiling stored procedures you can optimize the queries. There are three ways in which you can force SQL Server to recompile your stored procedure:
refrence click:: http://support.microsoft.com/kb/243586
When a user executes a stored procedure, if it is not already available in cache, SQL Server loads the procedure and compiles a query plan. The compiled plan is saved in cache and is reused by subsequent callers of the stored procedure until some action occurs to invalidate the plan and force a recompilation. The following actions may cause recompilation of a stored procedure plan:
•Dropping and recreating the stored procedure
•Restoring the database containing the stored procedure or any object referenced by the stored procedure
•The stored procedures plan dropping from the cache
•Use of a WITH RECOMPILE clause in the CREATE PROCEDURE or EXECUTE statement.
•Schema changes to any of the referenced objects, including adding or dropping constraints, defaults, or rules.
•Running sp_recompile for a table referenced by the procedure.
•Restoring the database containing the procedure or any of the objects the procedure references (if you are performing cross-database operations).
•Sufficient server activity causing the plan to be aged out of cache.
•Rows changed threshold: There are thresholds on the number of row changes that cause stored procedure recompilation, depending on the type of object as follows:
Table type- Permanent
Empty condition -< 500 Rows
Threshold when empty -# of Changes >= 500
Threshold when not empty -# of Changes >= 500 + (20 percent of Cardinality)
Table type- Temporary
Empty condition -< 6 Rows
Threshold when empty -# of Changes >= 6
Threshold when not empty -# of Changes >= 500 + (20 percent of Cardinality)
Table type-Table Variable
Empty condition -No thresholds
Threshold when empty -No thresholds
Threshold when not empty -No thresholds
The above thresholds, recorded by Profiler under the SP:Recompile event, govern the recompilation of permanent and temporary tables. The data column EventSubClass provides the reason for recompilation. When a threshold is crossed, EventSubClass will = 2, indicating that statistics changed. Row changes are recorded in the rowmodctr column of the sysindexes table.
•Use of certain SET options in stored procedures can cause recompilation.
1.Generally, those that affect query behavior or result sets such as:
ANSI_DEFAULTS
ANSI_NULL_DFLT_OFF
ANSI_NULL_DFLT_ON
ANSI_NULLS
ANSI_PADDING
CONCAT_NULL_YIELDS_NULL
FORCEPLAN
2.Other SET options include:
ANSI_WARNINGS
ARITHABORT
LANGUAGE
NUMERIC_ROUNDABORT
QUOTED_IDENTIFIER
All these reasons for recompiling a stored procedure did exist in earlier versions, and caused the plan to recompile before beginning execution of the procedure. In SQL Server 7.0, a new behavior is introduced that may cause a stored procedure to recompile during execution. This new behavior ensures that the optimizer always has the best possible plan for each specific
statement within a procedure. The following events may cause a run-time recompilation of a stored procedure:
•Stored procedure will recompile if there is a sufficient number of rows in a table referenced by the stored procedure has changed. SQL Server will recompile the stored procedure to be sure that the execution plan has the up-to-date statistics for the table.
•The procedure interleaves Data Definition Language (DDL) and Data Manipulation Language (DML) operations.
•Stored procedures will recompile if the developer has place interleaving Data Definition Language operations with Data Manipulation Language operations. This is usually caused when temporary objects are created and referenced throughout the code.
•The procedure performs certain operations on temporary tables.
In some cases, the cost of recompiling the stored procedure is more than the benefit derived from doing so, especially for large procedures. It is very important to note that when a recompilation is triggered, the entire batch or procedure is recompiled. This means that performance degradation is directly proportional to the size of the procedure or batch.
refrence click :: http://msdn.microsoft.com/en-us/library/ms190439.aspx
(a) The sp_recompile system stored procedure forces a recompile of a stored procedure the next time it is executed. For example:
Exec sp_recompile [MySPName]
(b) Create a stored procedure specifying WITH RECOMPILE option.
If WITH RECOMPILE is specified SQL Server does not cache a plan for this stored procedure;
the stored procedure is recompiled each time it is executed. Use the WITH RECOMPILE option when stored procedures take parameters whose values differ widely between executions of the stored procedure, resulting in different execution plans to be created each time. Ideally, you should not use this option because by using this option, you lose most of the advantages
you get by substituting SQL queries with the stored procedures. However, you can use this option during early development stages of the project where database structure is still evolving and stored procedures should be recompiled as a result of these changes.
Here is how you can create a store procedure using RECOMPILE option:
CREATE PROCEDURE usp_MyProcedure WITH RECOMPILE
AS
Select SampleName, SampleDesc From SampleTable
GO
(c) You can force the stored procedure to be recompiled by specifying the WITH RECOMPILE option when you execute the stored procedure. Use this option only if the parameter you are supplying is atypical or if the data has significantly changed since the stored procedure was created.
For Example:
EXEC usp_MyProcedure WITH RECOMPILE
Vedio Link http://www.novicksoftware.com/videorecompile_profiler.htm
Thanks
Yogesh