<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>The Pythian Blog &#187; Warner Chaves</title>
	<atom:link href="http://www.pythian.com/news/author/chaves/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.pythian.com/news</link>
	<description>News and views from Pythian DBAs</description>
	<lastBuildDate>Fri, 19 Mar 2010 02:09:24 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Log Buffer #120: a Carnival of the Vanities for DBAs</title>
		<link>http://www.pythian.com/news/1307/log-buffer-120-a-carnival-of-the-vanities-for-dbas/</link>
		<comments>http://www.pythian.com/news/1307/log-buffer-120-a-carnival-of-the-vanities-for-dbas/#comments</comments>
		<pubDate>Fri, 24 Oct 2008 16:24:39 +0000</pubDate>
		<dc:creator>Warner Chaves</dc:creator>
				<category><![CDATA[Log Buffer]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[Non-Tech Articles]]></category>
		<category><![CDATA[Oracle]]></category>
		<category><![CDATA[PostgreSQL]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[DB2]]></category>
		<category><![CDATA[dba]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[mac]]></category>
		<category><![CDATA[Windows]]></category>

		<guid isPermaLink="false">http://www.pythian.com/blogs/1307/log-buffer-120-a-carnival-of-the-vanities-for-dbas</guid>
		<description><![CDATA[Previously on Log Buffer: Log Buffer #119.
And now. 
Welcome to Log Buffer #120. My name is Warner, and I&#8217;m a SQL Server DBA at The Pythian Group. This is my first time on Log Buffer duties ever, so here&#8217;s hoping I can give everyone a fair and unbiased look at this week in the database [...]]]></description>
			<content:encoded><![CDATA[<p>Previously on <em>Log Buffer</em>: <a href="http://www.pythian.com/blogs/1304/log-buffer-119-a-carnival-of-the-vanities-for-dbas"><em>Log Buffer #119</em></a>.</p>
<p>And now. </p>
<p>Welcome to <em>Log Buffer #120</em>. My name is Warner, and I&#8217;m a SQL Server DBA at <a href="http://www.pythian.com">The Pythian Group</a>. This is my first time on <em>Log Buffer</em> duties ever, so here&#8217;s hoping I can give everyone a fair and unbiased look at this week in the database blogging world (and related).</p>
<p>I admit I had no idea of the community or state of the <strong>PostgreSQL</strong> RDBMS, and so I definitely learned some new stuff this week. First off, over on <a href="http://scale-out-blog.blogspot.com/">&#8220;The Scale-out Blog&#8221;</a> <strong>Robert Hodges</strong> invites us all to get our <a href="http://gizmodo.com/gadgets/gadgets/shoephone-has-made-its-triumphant-return-241728.php">shoephone</a> and <a href="http://scale-out-blog.blogspot.com/2008/10/getting-smart-about-new-world-of.html">get smart about the new world of PostgreSQL replication</a>. </p>
<p>Moving over to <a href="http://andreas.scherbaum.la/blog/">&#8220;ad&#8217;s corner&#8221;</a>, <strong>Andreas Scherbaum</strong> gives us a glimpse of the glitz and glam of <a href="http://andreas.scherbaum.la/blog/archives/465-PGDay.org-first-day.html">PGDay opening</a>. Then he entices us with the title, <a href="http://andreas.scherbaum.la/blog/archives/466-Party-in-the-evening.html">&#8220;Party in the evening&#8221;</a> just to horrify us by revealing that&#8212;you&#8217;d better sit down&#8212;by the end of the event half of the beer was still untouched. Next time Andreas, <a href="http://www.pythian.com/contact/">who you gonna call?</a></p>
<p>&#8220;Everything is a DNS problem,&#8221; is my new voicemail message, and also the title for <a href="http://www.krisbuytaert.be/blog/"><strong>Kris Buytaert&#8217;s</strong> blog</a>, where we go deep into uncovering once again the 10 month-old enigma of <a href="http://www.krisbuytaert.be/blog/node/745">why did Sun buy <strong>MySQL</strong></a>.</p>
<p>Next up, I will speak to you, Mr. (or Ms.) Cross-Platform DBA. You think you know all of Oracle&#8217;s exp command-line switches? All of SQL Server&#8217;s bcp formats? And all of the things you can do with&nbsp;.&nbsp;.&nbsp;.&nbsp; whatever you use with DB2? <span id="more-1307"></span> Well, I bet you can still learn something from <a href="http://provenscaling.com/blog/"><strong>Eric Bergen</strong></a> and the <a href="http://provenscaling.com/blog/2008/10/19/how-mysqldump-locks-tables-with-different-options/">different locking behaviors you can get out of mysqldump</a>.</p>
<p>I take my hat off to <a href="http://blog.shlomoid.com/"><strong>Schlomo Priymak</strong></a> for contributing to our dream of cross-platform love by helping us <a href="http://blog.shlomoid.com/2008/10/problems-uninstalling-mysql-connector.html">uninstall before upgrading the MySQL Connector for .NET</a>. Because both platforms can benefit from each other and people like him keep our dream strong.</p>
<p>On Sunday, I was <a href="http://datacharmer.blogspot.com/">data charmed</a> by <strong>Giuseppe Maxia</strong> with this warning post about <a href="http://datacharmer.blogspot.com/2008/10/odd-spot-in-relay-slaves.html">handling MySQL relay slaves when changing the replication format</a>. After reading that I had to put on my 3-D glasses, having discovered <a href="http://www.flickr.com/photos/juliancash/sets/72157604716923223/">Julian Cash&#8217;s Photo Gallery of MySQL DBAs</a> including some very psychedelic shots of Pythian&#8217;s very own <a href="http://www.pythian.com/blogs/author/sheeri">Sheeri K. Cabral</a>. </p>
<p>Let&#8217;s jump right over to <strong>Oracle</strong>. <a href="http://www.petefinnigan.com/weblog/entries/">Pete Finnigan&#8217;s Oracle security weblog</a> pointed out a new paper going by the incendiary title <a href="http://www.petefinnigan.com/weblog/archives/00001214.htm">&#8220;Assault on Oracle PL/SQL &#8211; Injection&#8221; </a> by a hacker called <a href="http://www.milw0rm.com/author/880" rel="nofollow">AELPHAEIS MANGARAE</a>. N.B.: If your kids are the kind  that like to read DB security documentation, be aware that there are some four letter words on the document.</p>
<p>If you are a <a href="http://www.oraclecommunity.net/group/tomkytefans"><strong>Tom Kyte</strong> Fan</a> (look who I found on the front page, none other than <a href="http://www.pythian.com/blogs/author/seiler">Mr. Don Seiler</a>) then you have to head over right now to the <a href="http://jonathanlewis.wordpress.com/">Oracle Scratchpad</a> where <strong>Jonathan Lewis</strong> has posted the dates for <a href="http://jonathanlewis.wordpress.com/2008/10/21/advert-tom-kyte/">Tom&#8217;s events in Europe</a>.</p>
<p>Moving to current world trends, <a href="http://gplivna.blogspot.com/"><strong>Gints Plivna</strong></a> shows us how to <a href="http://gplivna.blogspot.com/2008/10/mortgage-calculator-using-sql-model.html">calculate our mortgages using the Oracle MODEL clause</a>.  Time is running out, in case you haven&#8217;t noticed, and there are <a href="http://only4left.jpiwowar.com/">Only Four Left&#8230;</a>. Or so says <strong>John Piwowar</strong>, showing us how to <a href="http://only4left.jpiwowar.com/2008/10/running-oidadmin-on-mac-os-x/">run oidadmin on Mac</a>.</p>
<p>Now, join me where the air is warm and the food is plentiful. </p>
<p>I call all my fellow <strong>SQL Server</strong> DBAs to gather for a road trip to Redmond, Boston, Seattle, and Montreal, as we follow <a href="http://sqlblog.com/blogs/adam_machanic/default.aspx"><strong>Adam Machanic&#8217;s</strong></a> trail through the world of <a href="http://sqlblog.com/blogs/adam_machanic/archive/2008/10/21/speaking-season-about-to-begin-mcm-roadshow-pass-and-sqlclr-precon-at-devteach.aspx">SQL Server conferencing</a>. Adam, how about we pitch the reality show to Microsoft? (Suggestions for the show&#8217;s title on the Comments section, thank you).</p>
<p>I&#8217;m bringing myself up-to-date on SQL 2008 and <a href="http://sqlblog.com/blogs/tibor_karaszi/default.aspx"><strong>Tibor Karaszi</strong></a> gave me a nice heads-up on what to expect with <a href="http://sqlblog.com/blogs/tibor_karaszi/archive/2008/10/22/does-the-resource-governor-govern.aspx">resource governor behavior on multiple CPUs</a>. And just so we don&#8217;t forget to keep working on the fundamentals, <a href="http://sqlblogcasts.com/blogs/acalvett/default.aspx"><strong>Andrew Calvett</strong></a> gives us a clear look at the <a href="http://sqlblogcasts.com/blogs/acalvett/archive/2008/10/20/the-overhead-of-a-non-unique-clustered-index.aspx">overhead of non-unique clustered indexes</a> with the help of <strong>Danny Gould&#8217;s</strong> really cool <a href="http://www.codeplex.com/InternalsViewer">Internals Viewer for SQL Server</a>.</p>
<p>SQL injection is running rampant on all platforms, so over on <a href="http://sqlserver-qa.net/blogs/perftune/default.aspx">SSQA.net</a>, SQL Master (yes, that&#8217;s his real name) recommends us some <a href="http://sqlserver-qa.net/blogs/perftune/archive/2008/10/21/5001.aspx"> tools to help secure your SQL Server environment</a> to plug all those holes before it&#8217;s too late.</p>
<p>There are few things more embarrassing for a DBA than getting locked out of their own databases. If that ever happens to you though, don&#8217;t say you were not warned by <a href="http://www.pythian.com/blogs/author/mawla">Mohammed Mawla</a> as he <a href="http://www.pythian.com/blogs/1310/sql-server-troubleshooting-logon-triggers">shows us how to properly use logon triggers or save some face with the SQL 2005 DAC connection</a>.</p>
<p>And in case you are on the fence about upgrading to SQL Server 2008, <a href="http://weblogs.sqlteam.com/chrism/Default.aspx"><strong>Chris Miller</strong></a> will convince you with the reasons why he is <a href="http://weblogs.sqlteam.com/chrism/archive/2008/10/22/SQL-Management-Studio-2008.aspx">loving the new SQL Server 2008 Management Studio</a>: &#8220;&nbsp;.&nbsp;.&nbsp;.&nbsp;it actually does suck 85% less than SQL Server Management Studio 2005&#8243;. Nuff said!</p>
<p>Okay, I&#8217;m winding down now, but I cannot go  before letting you know that you might want to break the piggy bank for the $1.4 million <a href="http://freedb2.com"><strong>Leon Katsnelson</strong></a> tells us you will need to recreate <a href="http://freedb2.com/2008/10/22/can-db2-express-c-be-that-fast/">IBM&#8217;s DB2 TPC-C Benchmark on an x86-64 PC server</a>.</p>
<p>And last but not least, I would like to close it out with two database-agnostic posts. </p>
<p>First <strong>Ken Downs</strong>, <a href="http://database-programmer.blogspot.com/">the Database Programmer</a>, goes into our everlasting philosophical dilemma of <a href="http://database-programmer.blogspot.com/2008/10/argument-for-denormalization.html">normalization and denormalization</a>. Second, <a href="http://www.db2portal.com/blog.html"><strong>Craig Mullins</strong></a> gives some food for thought in the form if his <a href="http://www.db2portal.com/2008/10/dba-rules-of-thumb.html">DBA rules of thumb</a>. My personal favorite is #4: <a href="http://www.neonesoft.com/blog/blogs/cmullins/archive/2008/10/01/DBA-Rules-of-Thumb_2C00_-Part-4-_2D00_-Don_2700_t-Panic.aspx">&#8220;Don&#8217;t Panic&#8221;</a>.</p>
<p>I hope you enjoyed this 120th episode (that&#8217;s as long as The Muppet Show&#8217;s original run <em>and</em> the 1960&#8217;s Batman!). Until next time.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.pythian.com/news/1307/log-buffer-120-a-carnival-of-the-vanities-for-dbas/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Browsing SQL Server 2008&#8217;s New DMVs</title>
		<link>http://www.pythian.com/news/1305/browsing-sql-server-2008s-new-dmvs/</link>
		<comments>http://www.pythian.com/news/1305/browsing-sql-server-2008s-new-dmvs/#comments</comments>
		<pubDate>Mon, 20 Oct 2008 17:16:17 +0000</pubDate>
		<dc:creator>Warner Chaves</dc:creator>
				<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[DMV]]></category>
		<category><![CDATA[sql server 2008]]></category>

		<guid isPermaLink="false">http://www.pythian.com/blogs/1305/browsing-sql-server-2008s-new-dmvs</guid>
		<description><![CDATA[SQL Server 2008 is out of the bag and&#8212;luckily for my team&#8212;at Pythian we are already seeing customer interest in upgrading, even from SQL Server 2000 and 2005. There are many new features and there will definitely be more blog posts coming from the team regarding them, but for now, I was just browsing around [...]]]></description>
			<content:encoded><![CDATA[<p>SQL Server 2008 is out of the bag and&#8212;luckily for my team&#8212;at Pythian we are already seeing customer interest in upgrading, even from SQL Server 2000 and 2005. There are many new features and there will definitely be more blog posts coming from the team regarding them, but for now, I was just browsing around the new 2008 Dynamic Management Views (DMVs), and did a quick overview of some that spiked my attention:</p>
<pre>
select * from sys.dm_db_mirroring_auto_page_repair
</pre>
<p>It looks like grouping by db or file id would be useful to zoom in on possible disk issues causing the page errors. Beware&#8212;only 100 rows are kept for any database, so if you want to keep a full history you should move those records on a scheduled basis.</p>
<pre>
select * from sys.dm_db_mirroring_past_actions
</pre>
<p>This one could be useful for monitoring when there is no witness on a mirroring setup; or for creating a mirroring history report.</p>
<pre>
select * from sys.dm_db_persisted_sku_features
</pre>
<p>This one should be used as a warning sign before moving databases between editions, specifically between Enterprise and Standard. Paul Randal has a great post on it: <a href="http://www.sqlskills.com/blogs/paul/post/SQL-Server-2008-Does-my-database-contain-Enterprise-only-features.aspx"><em>SQL Server 2008: Does my database contain Enterprise-only features?</em></a>.</p>
<p><span id="more-1305"></span></p>
<pre>
select * from sys.dm_db_script_level
</pre>
<p>The only thing I could find on this was this KB article: <a href="http://support.microsoft.com/kb/955826"><em>Error message when you try to run a query in SQL Server 2008: &#8220;Msg 916, Level 14, State 1, Line 1&#8243;</em></a>, but no Books Online official page. It looks like some change-tracking internal view for hotfixing and service packing purposes. Oh well, it&#8217;s good to know what&#8217;s in your SQL anyway.</p>
<pre>
select * from sys.dm_exec_procedure_stats
</pre>
<p>This one is very similar to <code>dm_exec_query_stats</code> but on the Stored Procedure level instead of the statement level. It&#8217;s useful if you are interested only in stored procedures as a whole, or if you  want to filter ad hoc SQL batches from your analysis.</p>
<pre>
select * from sys.dm_exec_trigger_stats
</pre>
<p>This is very nice, as trigger-related issues are a somewhat common cause of performance hiccups on client environments.</p>
<pre>
select * from sys.dm_os_memory_brokers
</pre>
<p>This is the DMV form of information that was returned in previous versions by <code>DBCC MEMORYSTATUS</code>. It is useful for identifying and diagnosing possible memory pressure issues with regular T-SQL querying. It was a real pain to get it with the <code>DBCC MEMORYSTATUS</code> output.</p>
<pre>
select * from sys.dm_os_memory_node_access_stats
</pre>
<p>This one is not documented in Books Online. It looks like read and write stats for different page types, aggregated by memory nodes. I wonder if internal memory pressure can be analyzed or diagnosed with some help from this view.</p>
<pre>
select * from sys.dm_os_memory_nodes
</pre>
<p>Books Online says, &#8220;Nodes are created per physical NUMA memory nodes.&#8221; Again, this is taken from the <code>DBCC MEMORYSTATUS</code> output.</p>
<pre>
select * from sys.dm_os_nodes
</pre>
<p>Tied to the SQL OS architecture<a href="#footnote" name="footnote-source">*</a>, this view has some interesting stats like <code>active_worker_count</code> and <code>avg_load_balance</code>. I will definitely be running some tests and checking the output of this.</p>
<pre>
select * from sys.dm_os_process_memory
</pre>
<p>General memory stats from the server, useful for SQL-based monitoring on columns such as <code>process_physical_memory_low</code> that could alert after N minutes have passed on low memory warning. Also for baselining with the <code>page_fault_count</code>. This is an accumulated value though, so the differences should be measured and stored on a separate monitoring table.</p>
<pre>
select * from sys.dm_os_sys_memory
</pre>
<p>These are system-wide memory statistics, including columns that show if the system is on high- or low-memory overall. Definitely useful for monitoring and correlating with SQL memory statistics on non-SQL Server exclusive machines (Which should not exist. But the ideal world and the real one are so different, aren&#8217;t they?)</p>
<pre>
select * from sys.dm_sql_referenced_entities
select * from sys.dm_sql_referencing_entities
</pre>
<p>These are both tools for tracking dependencies, covered in depth by Aaron Bertrand on <a href="http://sqlblog.com/blogs/aaron_bertrand/archive/2008/09/09/keeping-sysdepends-up-to-date-in-sql-server-2008.aspx"><em>Keeping sysdepends up to date in SQL Server 2008</em></a>.</p>
<p>Now, I&#8217;m obviously just scratching the surface. There are many other new DMVs I haven&#8217;t had the chance to look at, many of them related to specific features like Full Text Search, Change Data Capture, FILESTREAM, Auditing, Extended Events (definitely a topic for upcoming posts) and Resource Governor.</p>
<p>If any of those interest you, take the time to browse around Books Online and explore them on your server (a trial version is out for <a href="http://www.microsoft.com/sqlserver/2008/en/us/trial-software.aspx">free download!</a>). Denis Gobo has <a href="http://sqlblog.com/blogs/denis_gobo/archive/2008/02/21/5188.aspx">a handy query to get all the names of the new DMVs</a>. Start looking around and get ready for the upgrades to 2008 coming your way.</p>
<p><u>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</u></p>
<p><a href="#footnote-source" name="footnote">*</a> Required reading for <em>all</em> SQL Server DBAs: <a href="http://blogs.msdn.com/slavao/articles/441058.aspx"><em>A new platform layer in SQL Server 2005 to exploit new hardware capabilities and their trends</em></a>. <a href="#footnote-source"><small>back</small></a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.pythian.com/news/1305/browsing-sql-server-2008s-new-dmvs/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>SQL Server COMPILE Locking and Encryption Keys</title>
		<link>http://www.pythian.com/news/1231/sql-server-compile-locking-and-encryption-keys/</link>
		<comments>http://www.pythian.com/news/1231/sql-server-compile-locking-and-encryption-keys/#comments</comments>
		<pubDate>Fri, 19 Sep 2008 18:53:44 +0000</pubDate>
		<dc:creator>Warner Chaves</dc:creator>
				<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[compile locking]]></category>
		<category><![CDATA[plan compilation]]></category>
		<category><![CDATA[sql server encryption]]></category>

		<guid isPermaLink="false">http://www.pythian.com/blogs/1231/from-the-trenches-sql-server-compile-locking-and-encryption-keys</guid>
		<description><![CDATA[The morning begins with this page: &#8220;a large number of sessions are blocked on one of your managed SQL Server 2005.&#8221; So you go and check out the Activity Monitor, and you can tell something unusual is going on:

Blocking chain of 200 procedures, your server is crawling with high CPU and requests are coming out [...]]]></description>
			<content:encoded><![CDATA[<p>The morning begins with this page: &#8220;a large number of sessions are blocked on one of your managed SQL Server 2005.&#8221; So you go and check out the Activity Monitor, and you can tell something unusual is going on:</p>
<p><a href='http://www.pythian.com/blogs/wp-content/uploads/compilelockactivitymonitor.jpg' title='compilelockactivitymonitor.jpg'><img src='http://www.pythian.com/blogs/wp-content/uploads/compilelockactivitymonitorthumbnail.jpg' alt='compilelockactivitymonitorthumbnail.jpg' /></a></p>
<p>Blocking chain of 200 procedures, your server is crawling with high CPU and requests are coming out a funnel. Now, don&#8217;t worry, the <strong>waitresource</strong> column provides us with the information to start zooming in on our problem. In this case, we have value &#8220;TAB: 7:357576312 [[COMPILE]]&#8221;. Disregarding the fact that the resource description says TAB, run the following query with the resource information to get the database and procedure being locked:</p>
<pre>
select db_name(7)

Use [PerformanceReports] -- the database name we got from the select above
GO

select object_name(357576312)
</pre>
<p>Knowing the procedure, it&#8217;s  then a matter of discarding  possibilities. The common causes of COMPILE locking are well documented on the Microsoft KB, <a href="http://support.microsoft.com/kb/263889">&#8220;Description of SQL Server blocking caused by compile locks&#8221;</a>, so if you have a chance, go on and read that. If you don&#8217;t, then this is the gist of it:</p>
<p><span id="more-1231"></span></p>
<ol>
<li>Ensure all references to stored procedures in your code are owner-qualified. Do not call &#8216;exec mysp&#8217;, call &#8216;exec john.mysp&#8217;. If you don&#8217;t, then you can get COMPILE locking and a Cache Miss.</li>
<li>Do not begin your stored procedures&#8217; names with &#8217;sp_&#8217;. This makes the engine go looking into the master database for the stored procedure, and will get you a Cache Miss (or godforbid, someone created a procedure in master with the same name and you are actually executing that). There are many options: &#8216;usp_&#8217;, &#8216;appsp_&#8217;, etc. Whatever works for you.</li>
<li>If you do owner-qualify your call, but you execute the procedure with a different case than what it was created as, then you could get a Cache Miss or a COMPILE lock. I tested this and got Cache Hits, but why take the chance? Use the same case.
<p>The last one is not included in the KB; I found out about it when working this specific case: </li>
<li>Do not open and close an encryption key inside a stored procedure in order to use encryption routines that will be called concurrently in heavy volume. This will get you Cache Miss and COMPILE locks too.</li>
</ol>
<p>I am talking about something like this:</p>
<pre>
CREATE PROCEDURE dbo.LookupByID @ValueID int
AS

SET NOCOUNT ON;

OPEN SYMMETRIC KEY PythianSymmetricKey DECRYPTION BY ASYMMETRIC KEY PythianAssymKey

SELECT CAST(DecryptByKey(encryptedField) AS VARCHAR(30)) FROM EncryptedValues
where ID = @ValueID

CLOSE SYMMETRIC KEY PythianSymmetricKey
</pre>
<p>If you don&#8217;t follow the points above, will you get the blocking chain I showed before? Not necessarily &#8212; if the server is fast enough, it will acquire and release those compile locks before you notice anything. As your workload increases, however, the issue will start coming to the surface until it materializes as a full-blown blocking chain. </p>
<p>Here is a detailed look through Profiler. I traced Lock, Batch, and Cache Hit events, and removed from the output the events that were not relevant to this problem. This is the output from the call with no owner specified and our encryption OPEN and CLOSE statements inside the procedure:</p>
<table cellpadding="2" cellspacing="2" summary="" border="1">
<tr>
<td>EventClass</td>
<td>TextData</td>
</tr>
<tr>
<td>SP:CacheMiss</td>
<td>exec lookupByID @valueid=7</td>
</tr>
<tr>
<td>SQL:BatchStarting</td>
<td>exec lookupByID @valueid=7</td>
</tr>
<tr>
<td>Lock:Acquired</td>
<td>[COMPILE]</td>
</tr>
<tr>
<td>Lock:Acquired</td>
<td>object_id = 309576141</td>
</tr>
<tr>
<td>Lock:Released</td>
<td>object_id = 309576141</td>
</tr>
<tr>
<td>&#8230;.</td>
<td>&#8230;.</td>
</tr>
<tr>
<td>Lock:Released</td>
<td>[COMPILE]</td>
</tr>
<tr>
<td>Lock:Acquired</td>
<td>symmetric_key_id</td>
</tr>
<tr>
<td>Lock:Acquired</td>
<td>asymmetric_key_id</td>
</tr>
<tr>
<td>Lock:Released</td>
<td>asymmetric_key_id</td>
</tr>
<tr>
<td>Lock:Released</td>
<td>symmetric_key_id</td>
</tr>
<tr>
<td>&#8230;.</td>
<td>&#8230;.</td>
</tr>
<tr>
<td>SQL:BatchCompleted</td>
<td>exec lookupByID @valueid=7</td>
</tr>
</table>
<p>As you can see, we are getting an initial Cache Miss that corresponds to just the actual call &#8216;exec LookupByID 7&#8242;. After that, we get the real Cache Miss from the procedure, the Lock Acquired event with COMPILE type and locking for both encryption keys.</p>
<p>Now, let&#8217;s qualify the owner, take out the OPEN and CLOSE SYMMETRIC KEY instructions from the procedure, and just leave the call to DecryptByKey inside:</p>
<table cellpadding="2" cellspacing="2" summary="" border="1">
<tr>
<td>EventClass</td>
<td>TextData</td>
</tr>
<tr>
<td>SP:CacheMiss</td>
<td>exec lookupByID @valueid=7</td>
</tr>
<tr>
<td>SQL:BatchStarting</td>
<td>exec lookupByID @valueid=7</td>
</tr>
<tr>
<td>Lock:Acquired</td>
<td>[COMPILE]</td>
</tr>
<tr>
<td>Lock:Acquired</td>
<td>object_id = 309576141</td>
</tr>
<tr>
<td>Lock:Released</td>
<td>object_id = 309576141</td>
</tr>
<tr>
<td>&#8230;.</td>
<td>&#8230;.</td>
</tr>
<tr>
<td>SP:CacheInsert</td>
<td>LookupByID</td>
</tr>
<tr>
<td>Lock:Released</td>
<td>[COMPILE]</td>
</tr>
<tr>
<td>&#8230;.</td>
<td>&#8230;.</td>
</tr>
<tr>
<td>SQL:BatchCompleted</td>
<td>exec lookupByID @valueid=7</td>
</tr>
</table>
<p>At first glance, it looks like the same thing. Sure, there is no more encryption key locking, but compile locks are being acquired, and our plan is not getting a Cache Hit. But now we got the Cache Insert fired. And that means that our next run of the procedure looks like this:</p>
<table cellpadding="2" cellspacing="2" summary="" border="1">
<tr>
<td>EventClass</td>
<td>TextData</td>
</tr>
<tr>
<td>SP:CacheMiss</td>
<td>exec dbo.LookupByID @ValueID=7</td>
</tr>
<tr>
<td>SQL:BatchStarting</td>
<td>exec dbo.LookupByID @ValueID=7</td>
</tr>
<tr>
<td>SP:Cache Hit</td>
<td></td>
</tr>
<tr>
<td>Lock:Acquired</td>
<td>1:606904</td>
</tr>
<tr>
<td>Lock:Released</td>
<td>1:606904</td>
</tr>
<tr>
<td>SQL:BatchCompleted</td>
<td>exec dbo.LookupByID @ValueID=7</td>
</tr>
</table>
<p>And that is ideally how it should look  all the time: no compilation locking, cache hit and only the shared lock to the table we are querying. </p>
<p>So remember, follow the known Microsoft recommendations, OPEN and CLOSE the key once and you can execute as many times as you want the stored procedure calling DecryptByKey (one approach for this can be found in <a href="http://www.sqlservercentral.com/articles/SQL+Server+2005+-+Security/3058/"><em>A Simple Approach to SQL Server 2005 Encryption</em></a> by Mike Good). Not only will you avoid the COMPILE locks, but also CPU overhead. Back to work.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.pythian.com/news/1231/sql-server-compile-locking-and-encryption-keys/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>
