Article ID: 263889 - Last Review: November 24, 2010 - Revision: 5.0 Description of SQL Server blocking caused by compile locks
This article was previously published under Q263889 On This PageSUMMARYIn Microsoft SQL Server, only one copy of a stored procedure plan is generally in cache at a time. Enforcing this requires serialization of some parts of the compilation process, and this synchronization is accomplished in part by using compile locks. If many connections are concurrently running the same stored procedure and a compile lock must be obtained for that stored procedure every time that it is run, system process IDs (SPIDs) might begin to block one another as they each try to obtain an exclusive compile lock on the object. MORE INFORMATIONStored procedure recompilation is one explanation for compile locks on a stored procedure or trigger. The solution in this case is to reduce or to eliminate the recompiles. For an explanation of the most common reasons that a stored procedure may have to be recompiled and for some useful information on reducing the frequency of recompiles, see the following Microsoft Knowledge Base article: 243586
(http://support.microsoft.com/kb/243586/
)
Troubleshooting stored procedure recompilation
Another scenario in which compile locks occur is when the following conditions are true:
If an existing plan is found, SQL Server reuses the cached plan and does not actually compile the stored procedure. However, the lack of owner-qualification forces SQL Server to perform a second cache lookup and obtain an exclusive compile lock before the program determines that the existing cached execution plan can be reused. Obtaining the lock and performing lookups and other work that is needed to reach this point can introduce a delay for the compile locks that leads to blocking. This is especially true if many users who are not the stored procedure's owner concurrently run the procedure without supplying the owner's name. Be aware that even if you do not see SPIDs waiting for compile locks, lack of owner-qualification can introduce delays in stored procedure execution and cause unnecessarily high CPU utilization. The following sequence of events will be recorded in a SQL Server Profiler trace when this problem occurs. (To trace cache-related events, you must enable advanced events. To do this, click Options on the Tools menu, and then select All event classes.) Collapse this table
SP:CacheMiss occurs when the cache lookup by name fails. The following SP:ExecContextHit indicates that a matching cached plan was ultimately found in cache after the ambiguous object name was resolved to an object ID. Depending on the circumstances, SP:CacheHit may appear instead of SP:ExecContextHit. The solution to this problem of compile locking is to make sure that references to stored procedures are owner-qualified. (Instead of exec mystoredproc, use exec dbo.mystoredproc.) While owner-qualification is important for performance reasons, you do not have to qualify the stored proc with the database name to prevent the additional cache lookup. Blocking that is caused by compile locks can be detected by using blocking scripts such as those that are defined in the following Microsoft Knowledge Base articles: 251004
(http://support.microsoft.com/kb/251004/
)
INF: How to monitor SQL Server 7.0 blocking
271509
(http://support.microsoft.com/kb/271509/
)
INF: How to monitor SQL Server 2000 blocking
The following are some typical characteristics of compile blocking that can be observed in the blocking script output:
spid blocked waittype waittime lastwaittype waitresource
---- ------- -------- -------- ------------ -------------------------
221 29 0x000e 2141 LCK_M_X TAB: 6:834102 [[COMPILE]]
228 29 0x000e 2235 LCK_M_X TAB: 6:834102 [[COMPILE]]
29 214 0x000e 3937 LCK_M_X TAB: 6:834102 [[COMPILE]]
13 214 0x000e 1094 LCK_M_X TAB: 6:834102 [[COMPILE]]
68 214 0x000e 1968 LCK_M_X TAB: 6:834102 [[COMPILE]]
214 0 0x0000 0 LCK_M_X TAB: 6:834102 [[COMPILE]]Notes
Known issuesHere are some known issues that can prevent plan caching:
APPLIES TO
| Other Resources Other Support Sites
CommunityGet Help NowArticle Translations
|





















Back to the top