﻿<?xml version='1.0' encoding='UTF-8'?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>SQLServerCentral / Article Discussions / Article Discussions by Author / Discuss content posted by Steve Jimmo  / Compilation Locks / Latest Posts</title><generator>InstantForum.NET v2.9.0</generator><description>SQLServerCentral</description><link>http://www.sqlservercentral.com/Forums/</link><webMaster>notifications@sqlservercentral.com</webMaster><lastBuildDate>Sun, 19 May 2013 22:21:40 GMT</lastBuildDate><ttl>20</ttl><item><title>RE: Compilation Locks</title><link>http://www.sqlservercentral.com/Forums/Topic919418-2564-1.aspx</link><description>[quote][b]Hugo Kornelis (5/12/2010)[/b][hr]Well, actually hints [b][i]are[/i][/b] law. The term "optimizer hint" is highly misleading; "optimizer directive" would have been better. If you use an index hint, the optimizer WILL use that index for the query. If you specify to use hash joins, you WILL get hash joins. No matter how high the cost and how much cheaper an unhinted plan would have been.  If you have any evidence to the contrary, please let me know - it's quite likely to be a bug.[/quote]Even "optimiser directive" doesn't quite capture it, since not all 'hints' apply to the optimiser.  Some hints (like NOWAIT, REPEATABLEREAD, IGNORE_TRIGGERS) are only used by the execution engine.  Other hints (like KEEPIDENTITY) materially change the effect of the query.I prefer to think of the true optimiser hints as 'implementation rule switches', since they affect which physical implementations are available to the optimiser as it explores the plan space.Given the wide range of effects on various stages of query execution, I think 'hint' is as good a term as any, though undoubtedly a flawed one.</description><pubDate>Fri, 14 May 2010 23:58:47 GMT</pubDate><dc:creator>Paul White</dc:creator></item><item><title>RE: Compilation Locks</title><link>http://www.sqlservercentral.com/Forums/Topic919418-2564-1.aspx</link><description>Well I selected both the answers required to get the green tick, but I am pretty sure the 'answer' is incorrect.Sure, not schema-qualifying the procedure name leads to compilation locks, but not being the owner of the procedure is not sufficient to do so."The user that runs the stored procedure is not the owner of the procedure." should not be a correct answer here.</description><pubDate>Fri, 14 May 2010 23:18:57 GMT</pubDate><dc:creator>Paul White</dc:creator></item><item><title>RE: Compilation Locks</title><link>http://www.sqlservercentral.com/Forums/Topic919418-2564-1.aspx</link><description>[quote][b]Adi Cohn-120898 (5/12/2010)[/b][hr][quote] This is not a bug. Rowlock and pagelock are mutually exclusive, and both state to use either row locks or page locks [i]as long as escalation to page locks is not required[/i]. [/quote]  I think that lock escalation can't escalate to page lock.  If I'm not mistaken until SQL Server 2008 lock escalation could escalate directly to table lock and since SQL Server 2008 if the table is partitioned, it can escalate to lock a partition in the table (or few partitions if needed).Adi[/quote]Oops. Stupid mistake. My mind said table lock, my fingers typed something else.Thanks for pointing it out; I'll see if I can edit the post.</description><pubDate>Wed, 12 May 2010 10:59:09 GMT</pubDate><dc:creator>Hugo Kornelis</dc:creator></item><item><title>RE: Compilation Locks</title><link>http://www.sqlservercentral.com/Forums/Topic919418-2564-1.aspx</link><description>[quote] This is not a bug. Rowlock and pagelock are mutually exclusive, and both state to use either row locks or page locks [i]as long as escalation to page locks is not required[/i]. [/quote]  I think that lock escalation can't escalate to page lock.  If I'm not mistaken until SQL Server 2008 lock escalation could escalate directly to table lock and since SQL Server 2008 if the table is partitioned, it can escalate to lock a partition in the table (or few partitions if needed).Adi</description><pubDate>Wed, 12 May 2010 10:21:05 GMT</pubDate><dc:creator>Adi Cohn-120898</dc:creator></item><item><title>RE: Compilation Locks</title><link>http://www.sqlservercentral.com/Forums/Topic919418-2564-1.aspx</link><description>[quote]This is not a bug. Rowlock and pagelock are mutually exclusive, and both state to use either row locks or page locks as long as escalation to page locks is not required. The pagelock hint tells SQL Server to simply forego the stage of locking individual pages or individual rows and lock the entire table right at once. [/quote]Exactly. As for the index hints, I knew that one. But thanks for the confirmation.</description><pubDate>Wed, 12 May 2010 10:09:06 GMT</pubDate><dc:creator>sjimmo</dc:creator></item><item><title>RE: Compilation Locks</title><link>http://www.sqlservercentral.com/Forums/Topic919418-2564-1.aspx</link><description>[quote][b]sjimmo (5/12/2010)[/b][hr]Hugo,Mine has been more in the rowlock, nolock and tablock hints. I have had cases where the optimizer determined that the tablock was more effective than a rowlock and escalated it as such. Possibly a bug, won't wswear to it as my understanding has been that this is the way that it should be.Thanks[/quote]Ah, that's what you meant.This is not a bug. Rowlock and pagelock are mutually exclusive, and both state to use either row locks or page locks [i]as long as escalation to table locks is not required[/i]. The pagelock hint tells SQL Server to simply forego the stage of locking individual pages or individual rows and lock the entire table right at once.EDIT: Corrected a very stupid eror - thanks, Adi, for pointing it out!</description><pubDate>Wed, 12 May 2010 09:57:26 GMT</pubDate><dc:creator>Hugo Kornelis</dc:creator></item><item><title>RE: Compilation Locks</title><link>http://www.sqlservercentral.com/Forums/Topic919418-2564-1.aspx</link><description>Hugo,Mine has been more in the rowlock, nolock and tablock hints. I have had cases where the optimizer determined that the tablock was more effective than a rowlock and escalated it as such. Possibly a bug, won't wswear to it as my understanding has been that this is the way that it should be.Thanks</description><pubDate>Wed, 12 May 2010 09:18:15 GMT</pubDate><dc:creator>sjimmo</dc:creator></item><item><title>RE: Compilation Locks</title><link>http://www.sqlservercentral.com/Forums/Topic919418-2564-1.aspx</link><description>[quote][b]sjimmo (5/12/2010)[/b][hr]Unfortunately, many think that a hint is law, and have spent many hours debating hints, as well as showing hints that work and then not work because the optimizeer deemed that it knew better.[/quote]Well, actually hints [b][i]are[/i][/b] law. The term "optimizer hint" is highly misleading; "optimizer directive" would have been better. If you use an index hint, the optimizer WILL use that index for the query. If you specify to use hash joins, you WILL get hash joins. No matter how high the cost and how much cheaper an unhinted plan would have been.If you have any evidence to the contrary, please let me know - it's quite likely to be a bug.</description><pubDate>Wed, 12 May 2010 08:38:20 GMT</pubDate><dc:creator>Hugo Kornelis</dc:creator></item><item><title>RE: Compilation Locks</title><link>http://www.sqlservercentral.com/Forums/Topic919418-2564-1.aspx</link><description>[quote]I think you are referring to the OPTIMIZE FOR option, right?[/quote]That is the one. Yes there is a down side to it, as with most hints.Unfortunately, many think that a hint is law, and have spent many hours debating hints, as well as showing hints that work and then not work because the optimizeer deemed that it knew better.I have not worked with this one [b]yet[/b], but think it is coming with a specific issue. Thanks for the info of your experience.</description><pubDate>Wed, 12 May 2010 06:55:10 GMT</pubDate><dc:creator>sjimmo</dc:creator></item><item><title>RE: Compilation Locks</title><link>http://www.sqlservercentral.com/Forums/Topic919418-2564-1.aspx</link><description>[quote][b]sjimmo (5/12/2010)[/b][hr]It slips me right this moment which hint it is, but for the scenario you describe, there is a hint that can be used in order to have the procedure use the queryplan for the longest running parameter.[/quote]I think you are referring to the OPTIMIZE FOR option, right?You can use this to force optimization for the "most often" used vlaue (getting the best performance in most cases, but worse -potentially much worse- in rare cases).Or you can use it to force optimization for a value that will result in a plan that might not be optimal for all values, but will never hit extreme execution times.[funny fact - I am typing this right after identifying a query ona customer site where the optimizer decided on a nested loops join and a table scan, based on an estimate of 1 input row; the actual row count turned out to be over 20,000 so a 25,000+-row table got scanned over 20,000 times... - that would be the extreme execution time]</description><pubDate>Wed, 12 May 2010 06:32:50 GMT</pubDate><dc:creator>Hugo Kornelis</dc:creator></item><item><title>RE: Compilation Locks</title><link>http://www.sqlservercentral.com/Forums/Topic919418-2564-1.aspx</link><description>Thanks for the explanation Hugo. The questio happened to fall while I was on vacation and traveling. In addition to your explanation, each database should have a good maintenance plan. Within that plan should be the updating of statistics on the various indexes as well as rebuilding indexes that need it. I also use a DBCC UPDATEUSAGE as well. After all of my maintenance is completed, I want to ensure that the various stroed procedures are using an optimal QueryPlan. So, the last thing I do is to perform a recompile of all stored procedures and triggers. Ultimately, this will force the procedures to reaquire a new queryplan the first time that they are run.Additionally, within your explanation, you should go a bit further. It slips me right this moment which hint it is, but for the scenario you describe, there is a hint that can be used in order to have the procedure use the queryplan for the longest running parameter. this takes some effort to identify, but works. I found it while trying to quell an attempted developer revolt, in that the developers wanted to recompile procedures each time that they ran. In a 1200 user high transaction database, where some of these procedures are called literally a hundred times an hour, could be a problem. I will lookup the hint and post it later.</description><pubDate>Wed, 12 May 2010 05:41:22 GMT</pubDate><dc:creator>sjimmo</dc:creator></item><item><title>RE: Compilation Locks</title><link>http://www.sqlservercentral.com/Forums/Topic919418-2564-1.aspx</link><description>[quote][b]ziangij (5/11/2010)[/b][hr]plz. suggest... what are the pros &amp; cons of recompile option.[/quote]Parameter sniffing.Basically, SQL Server has three ways to deal with constant values in a query:a) If it's a constant (e.g. WHERE Age = 35), that value is used to optimize. SQL Server will find a plan that is most efficient for that value.b) If it's a variable (e.g. WHERE Age = @Age), the run-time value is not yet knwon when the query is optimized. Statistical information about the average spread of values in the Age column is used to optimze. The plan SQL Server creates will work good for most values, but might not be optimal for atypical values. If, for instance, a large majority of rows will have an age over 65 and only a small minority will have a younger age (as would be the case in a database of dementia patients), the plan will be optimal when @Age is 80, but less so when @Age is 43.c) If it's a parameter in a procedure (e.g. CREATE PROC Something @Age int AS .... WHERE Age = @Age), then SQL Server will use ("sniff") the value passed to the parameter when the plan gets created, and find a plan that is optimal for that value. Since the plan goes into cache for future reuse, subsequent executions of the procedure will use the same plan, even if they are called with a different parameter value. In the above case, if the first execution of the procedure happens to use @Age = 43, then it is possible that all future executions with a higher value for @Age are very slow, because SQL Server picked a plan that is optimal for values of @Age where only a small minority of rows match.In practice, parameter sniffing does more good than it does harm. But in cases where you find it does harm, you can use the RECOMPILE hint to force SQL Server to receompile the plan every time the stored procedure is executed. The benefit is that each execution uses a plan that is optimal for the @Age value passed in (since SQL Server will still sniff the parameter); the down side is that SQL Server has to spend a lot of resources for all those recompilations.</description><pubDate>Wed, 12 May 2010 01:06:51 GMT</pubDate><dc:creator>Hugo Kornelis</dc:creator></item><item><title>RE: Compilation Locks</title><link>http://www.sqlservercentral.com/Forums/Topic919418-2564-1.aspx</link><description>i generally don't use recompile option for my procs... execution plan is created the first time stored proc is executed and then it takes less time to execute...plz. suggest... what are the pros &amp; cons of recompile option.</description><pubDate>Tue, 11 May 2010 22:31:25 GMT</pubDate><dc:creator>ziangij</dc:creator></item><item><title>RE: Compilation Locks</title><link>http://www.sqlservercentral.com/Forums/Topic919418-2564-1.aspx</link><description>thanks..:-) good question</description><pubDate>Tue, 11 May 2010 22:11:13 GMT</pubDate><dc:creator>ziangij</dc:creator></item><item><title>RE: Compilation Locks</title><link>http://www.sqlservercentral.com/Forums/Topic919418-2564-1.aspx</link><description>Great question.  I learned something good today.</description><pubDate>Tue, 11 May 2010 15:16:46 GMT</pubDate><dc:creator>SQLRNNR</dc:creator></item><item><title>RE: Compilation Locks</title><link>http://www.sqlservercentral.com/Forums/Topic919418-2564-1.aspx</link><description>Great question.  I learned something new, which is always great!</description><pubDate>Tue, 11 May 2010 12:45:23 GMT</pubDate><dc:creator>KevinC.</dc:creator></item><item><title>RE: Compilation Locks</title><link>http://www.sqlservercentral.com/Forums/Topic919418-2564-1.aspx</link><description>[quote][b]rVadim (5/11/2010)[/b][hr]I'm a bit confused. Several of you suggested using "fully qualified names". But this usually refers to using all 4 parts: Server.Database.Schema.Object.Is that what you suggest we should use everywhere in code?[/quote]That was a bit unclear of "several of you" (of which I am one).In this context, "fully qualified" should be read as "schema-qualified". My apologies.</description><pubDate>Tue, 11 May 2010 12:20:53 GMT</pubDate><dc:creator>Hugo Kornelis</dc:creator></item><item><title>RE: Compilation Locks</title><link>http://www.sqlservercentral.com/Forums/Topic919418-2564-1.aspx</link><description>I'm a bit confused. Several of you suggested using "fully qualified names". But this usually refers to using all 4 parts: Server.Database.Schema.Object.Is that what you suggest we should use everywhere in code?</description><pubDate>Tue, 11 May 2010 11:16:10 GMT</pubDate><dc:creator>rVadim</dc:creator></item><item><title>RE: Compilation Locks</title><link>http://www.sqlservercentral.com/Forums/Topic919418-2564-1.aspx</link><description>This is a very good question, thank you Steve.Fully qualified objects names is a must, not subjected to any second guessing. This is a very pathetic scenario when due to someone's ignorance the database engine is forced to acquire compile lock thus potentially causing block contention just to determine that recompilation is not necessarily required.Oleg</description><pubDate>Tue, 11 May 2010 11:03:36 GMT</pubDate><dc:creator>Oleg Netchaev</dc:creator></item><item><title>RE: Compilation Locks</title><link>http://www.sqlservercentral.com/Forums/Topic919418-2564-1.aspx</link><description>Explanation is corrected.</description><pubDate>Tue, 11 May 2010 09:49:13 GMT</pubDate><dc:creator>Steve Jones - SSC Editor</dc:creator></item><item><title>RE: Compilation Locks</title><link>http://www.sqlservercentral.com/Forums/Topic919418-2564-1.aspx</link><description>Thank you for the great QOD! I really do appreciate the reminder to fully qualify names and reduce the work required by SQL Server. In this case, the lack of name qualification (laziness) causes SQL Server to place a compile lock on a potentially heavily used stored pocedure and substantially increasing runtimes.</description><pubDate>Tue, 11 May 2010 08:37:41 GMT</pubDate><dc:creator>bradfield_eric</dc:creator></item><item><title>RE: Compilation Locks</title><link>http://www.sqlservercentral.com/Forums/Topic919418-2564-1.aspx</link><description>I did a fair amount of background reading on this question and it led me to several light bulb moments about my current coding practices.  Thanks for a great question and a little bit of education first thing in the AM.</description><pubDate>Tue, 11 May 2010 07:54:27 GMT</pubDate><dc:creator>Daniel Bowlin</dc:creator></item><item><title>RE: Compilation Locks</title><link>http://www.sqlservercentral.com/Forums/Topic919418-2564-1.aspx</link><description>Thanks, Steve. Nice question to remind of us one more reason (hah! as if we'd need one!) to always fully qualify our object names.Small mistake in the explanation - is I read the referenced KB article, the compile lock occurs when both conditions are met, i.e. the SP is run by a non-owner, [b][i]AND[/i][/b] without using the fully qualified name. The explanation usees "or" instead of "and".</description><pubDate>Tue, 11 May 2010 02:07:10 GMT</pubDate><dc:creator>Hugo Kornelis</dc:creator></item><item><title>RE: Compilation Locks</title><link>http://www.sqlservercentral.com/Forums/Topic919418-2564-1.aspx</link><description>While I agree that it is a good question, I think that there is a mistake in the answers.  Since the question states that it is for SQL Server 2005, the fully qualified name of the object has nothing to do with the object’s owner.  Instead it has to do with the object’s schema.Adi</description><pubDate>Tue, 11 May 2010 01:46:24 GMT</pubDate><dc:creator>Adi Cohn-120898</dc:creator></item><item><title>RE: Compilation Locks</title><link>http://www.sqlservercentral.com/Forums/Topic919418-2564-1.aspx</link><description>Good question. Had to read up on it and then even got it right :-)</description><pubDate>Tue, 11 May 2010 01:39:37 GMT</pubDate><dc:creator>Jan Van der Eecken</dc:creator></item><item><title>RE: Compilation Locks</title><link>http://www.sqlservercentral.com/Forums/Topic919418-2564-1.aspx</link><description>Good question, i learned some thing out of it.</description><pubDate>Tue, 11 May 2010 01:26:15 GMT</pubDate><dc:creator>sharath.chalamgari</dc:creator></item><item><title>RE: Compilation Locks</title><link>http://www.sqlservercentral.com/Forums/Topic919418-2564-1.aspx</link><description>Deep-delve points to remember, especially when it comes to applications with large databases.Good question</description><pubDate>Tue, 11 May 2010 00:10:45 GMT</pubDate><dc:creator>Stewart "Arturius" Campbell</dc:creator></item><item><title>Compilation Locks</title><link>http://www.sqlservercentral.com/Forums/Topic919418-2564-1.aspx</link><description>Comments posted to this topic are about the item [B]&lt;A HREF="/questions/Locks/69930/"&gt;Compilation Locks&lt;/A&gt;[/B]</description><pubDate>Mon, 10 May 2010 22:09:14 GMT</pubDate><dc:creator>sjimmo</dc:creator></item></channel></rss>