<?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>SQL Internals Archives | SQL Solutions Group</title>
	<atom:link href="https://sqlsolutionsgroup.com/tag/sql-internals/feed/" rel="self" type="application/rss+xml" />
	<link>https://sqlsolutionsgroup.com/tag/sql-internals/</link>
	<description></description>
	<lastBuildDate>Thu, 24 Sep 2026 18:50:13 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=7.0.5</generator>

<image>
	<url>https://sqlsolutionsgroup.com/wp-content/uploads/2021/01/cropped-SSG_FAVICON0002-32x32.png</url>
	<title>SQL Internals Archives | SQL Solutions Group</title>
	<link>https://sqlsolutionsgroup.com/tag/sql-internals/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>Indexed Views &#8211; Performance Panacea or Plight</title>
		<link>https://sqlsolutionsgroup.com/indexed-view/</link>
					<comments>https://sqlsolutionsgroup.com/indexed-view/#comments</comments>
		
		<dc:creator><![CDATA[Jason Brimhall]]></dc:creator>
		<pubDate>Thu, 28 Jan 2016 13:50:48 +0000</pubDate>
				<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Group]]></category>
		<category><![CDATA[#microsftcertifedmaster]]></category>
		<category><![CDATA[#microsftpartner]]></category>
		<category><![CDATA[#SQLAB]]></category>
		<category><![CDATA[#SQlatino]]></category>
		<category><![CDATA[#SQlatinoamerica]]></category>
		<category><![CDATA[#sqldatabase]]></category>
		<category><![CDATA[#sqldeveloper]]></category>
		<category><![CDATA[#SQLgroupie]]></category>
		<category><![CDATA[#sqlimer]]></category>
		<category><![CDATA[#sqlimerbymay]]></category>
		<category><![CDATA[#sqlinjection]]></category>
		<category><![CDATA[#sqlinternals]]></category>
		<category><![CDATA[#sqlite]]></category>
		<category><![CDATA[#sqlite3]]></category>
		<category><![CDATA[#SQLLearning]]></category>
		<category><![CDATA[#SQLMagazine]]></category>
		<category><![CDATA[#sqlmanagementstudio]]></category>
		<category><![CDATA[#sqlmanager]]></category>
		<category><![CDATA[#Sqlmap]]></category>
		<category><![CDATA[#sqlrun]]></category>
		<category><![CDATA[#sqlsaturday2017]]></category>
		<category><![CDATA[#sqlsatvienna]]></category>
		<category><![CDATA[#sqlserver]]></category>
		<category><![CDATA[#SQLserver2012]]></category>
		<category><![CDATA[#sqlserver2014]]></category>
		<category><![CDATA[#sqlserver2017]]></category>
		<category><![CDATA[#sqlserver2022]]></category>
		<category><![CDATA[#SQLServeronLinux]]></category>
		<category><![CDATA[#SQLsolutionsgroup]]></category>
		<category><![CDATA[#SQLTraining]]></category>
		<category><![CDATA[#SQLYog]]></category>
		<category><![CDATA[Database Administration]]></category>
		<category><![CDATA[Index]]></category>
		<category><![CDATA[SQL Administration]]></category>
		<category><![CDATA[SQL Internals]]></category>
		<category><![CDATA[SQLPASS]]></category>
		<category><![CDATA[SQLSaturday]]></category>
		<category><![CDATA[SSG]]></category>
		<guid isPermaLink="false">http://sqlsolutionsgroup.com/?p=2803</guid>

					<description><![CDATA[<p>Indexed View(s) An indexed view is a view where the result set from the query (the view definition) becomes materialized in lieu of the virtual table result set of a standard (non-indexed) view. Many times we see that an indexed view would be created to help improve performance. Far too often, an indexed view is created without consideration for [&#8230;]</p>
<p>The post <a href="https://sqlsolutionsgroup.com/indexed-view/">Indexed Views &#8211; Performance Panacea or Plight</a> appeared first on <a href="https://sqlsolutionsgroup.com">SQL Solutions Group</a>.</p>
]]></description>
										<content:encoded><![CDATA[<h2><img decoding="async" class="alignleft wp-image-2805" src="https://sqlsolutionsgroup.com/wp-content/uploads/2016/01/binocular_night-1024x500.jpg" alt="indexed view" width="307" height="150" srcset="https://sqlsolutionsgroup.com/wp-content/uploads/2016/01/binocular_night-1024x500.jpg 1024w, https://sqlsolutionsgroup.com/wp-content/uploads/2016/01/binocular_night-300x147.jpg 300w, https://sqlsolutionsgroup.com/wp-content/uploads/2016/01/binocular_night-768x375.jpg 768w, https://sqlsolutionsgroup.com/wp-content/uploads/2016/01/binocular_night-1536x751.jpg 1536w, https://sqlsolutionsgroup.com/wp-content/uploads/2016/01/binocular_night.jpg 1588w" sizes="(max-width: 307px) 100vw, 307px" />Indexed View(s)</h2>
<p>An indexed view is a view where the result set from the query (the view definition) becomes materialized in lieu of the virtual table result set of a standard (non-indexed) view. Many times we see that an indexed view would be created to help improve performance. Far too often, an indexed view is created without consideration for the costs of the indexed view. In this article, I hope to cover some of the more important costs that are frequently overlooked when considering an indexed view as a performance panacea.</p>
<h3>The Setup</h3>
<p>To demonstrate the overlooked costs of indexed views, I have decided to use the AdventureWorks2014 database (if you don&#8217;t have this sample database, you can get your copy <a href="https://bit.ly/Aworksdb">here</a>). I will create a view in that database and then I will add a few indexes to that view. Prior to sharing the entire setup, and like all good DBAs, I need to first grab a baseline. For this baseline, I am doing nothing terribly complex. I will just grab table sizes and storage information for the entire database.</p>
<p>Here are my results for the size of the objects within the AdventureWorks2014 database:</p>
<p><img fetchpriority="high" decoding="async" class="alignnone wp-image-2807 " src="https://sqlsolutionsgroup.com/wp-content/uploads/2016/01/Tablesize_start.jpg" alt="Tablesize_start" width="907" height="143" srcset="https://sqlsolutionsgroup.com/wp-content/uploads/2016/01/Tablesize_start.jpg 1927w, https://sqlsolutionsgroup.com/wp-content/uploads/2016/01/Tablesize_start-300x47.jpg 300w, https://sqlsolutionsgroup.com/wp-content/uploads/2016/01/Tablesize_start-1024x162.jpg 1024w, https://sqlsolutionsgroup.com/wp-content/uploads/2016/01/Tablesize_start-768x121.jpg 768w, https://sqlsolutionsgroup.com/wp-content/uploads/2016/01/Tablesize_start-1536x242.jpg 1536w" sizes="(max-width: 907px) 100vw, 907px" /></p>
<p>These results show that the largest object in the database happens to be the Person.Person table at just about 30MB. Not terribly large, but the database is not terribly large as a whole. Let&#8217;s see what happens when I add a materialized view, based on the Person.Person table, to the mix. Here is the definition of that view along with the indexes that I will add to help the performance of some essential production related queries.</p>
<div id="wpshdo_1" class="wp-synhighlighter-outer"><div id="wpshdt_1" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_1"></a><a id="wpshat_1" class="wp-synhighlighter-title" href="#codesyntax_1"  onClick="javascript:wpsh_toggleBlock(1)" title="Click to show/hide code block">Source code</a></td><td align="right"><a href="#codesyntax_1" onClick="javascript:wpsh_code(1)" title="Show code only"><img decoding="async" border="0" style="border: 0 none" src="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_1" onClick="javascript:wpsh_print(1)" title="Print code"><img decoding="async" border="0" style="border: 0 none" src="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img decoding="async" border="0" style="border: 0 none" src="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_1" class="wp-synhighlighter-inner" style="display: block;"><pre class="tsql" style="font-family:monospace;"><span class="kw1">USE</span> <span class="br0">[</span>AdventureWorks2014<span class="br0">]</span>;
GO
&nbsp;
<span class="kw1">CREATE</span> <span class="kw1">VIEW</span> <span class="br0">[</span>Person<span class="br0">]</span>.<span class="br0">[</span>vPerson<span class="br0">]</span>
<span class="kw1">WITH</span> SCHEMAB<span class="sy0">IN</span>D<span class="sy0">IN</span>G
<span class="kw1">AS</span>
	<span class="kw1">SELECT</span> pp.<span class="me1">BusinessEntityID</span>
			, pp.<span class="me1">FirstName</span>
			, pp.<span class="me1">LastName</span>
			, sp.<span class="br0">[</span>StateProvinceID<span class="br0">]</span>
			, sp.<span class="br0">[</span>StateProvinceCode<span class="br0">]</span>
			, sp.<span class="br0">[</span>IsOnlyStateProvinceFlag<span class="br0">]</span>
			, sp.<span class="br0">[</span>Name<span class="br0">]</span> <span class="kw1">AS</span> <span class="br0">[</span>StateProvinceName<span class="br0">]</span>
			, sp.<span class="br0">[</span>TerritoryID<span class="br0">]</span>
			, cr.<span class="br0">[</span>CountryRegionCode<span class="br0">]</span>
			, cr.<span class="br0">[</span>Name<span class="br0">]</span> <span class="kw1">AS</span> <span class="br0">[</span>CountryRegionName<span class="br0">]</span>
			, pbe.<span class="me1">AddressTypeID</span>
			, pp.<span class="me1">AdditionalContactInfo</span>
			, pp.<span class="me1">Demographics</span>
		<span class="kw1">FROM</span> <span class="br0">[</span>Person<span class="br0">]</span>.<span class="br0">[</span>StateProvince<span class="br0">]</span> sp
			<span class="kw1">INNER</span> <span class="sy0">JOIN</span> <span class="br0">[</span>Person<span class="br0">]</span>.<span class="br0">[</span>CountryRegion<span class="br0">]</span> cr
				<span class="kw1">ON</span> sp.<span class="br0">[</span>CountryRegionCode<span class="br0">]</span> <span class="sy0">=</span> cr.<span class="br0">[</span>CountryRegionCode<span class="br0">]</span>
			<span class="kw1">INNER</span> <span class="sy0">JOIN</span> Person.<span class="me1">Address</span> pa
				<span class="kw1">ON</span> sp.<span class="me1">StateProvinceID</span> <span class="sy0">=</span> pa.<span class="me1">StateProvinceID</span>
			<span class="kw1">INNER</span> <span class="sy0">JOIN</span> Person.<span class="me1">BusinessEntityAddress</span> pbe
				<span class="kw1">ON</span> pbe.<span class="me1">AddressID</span> <span class="sy0">=</span> pa.<span class="me1">AddressID</span>
			<span class="kw1">INNER</span> <span class="sy0">JOIN</span> Person.<span class="me1">Person</span> pp
				<span class="kw1">ON</span> pp.<span class="me1">BusinessEntityID</span> <span class="sy0">=</span> pbe.<span class="me1">BusinessEntityID</span>
		<span class="kw1">WHERE</span> pbe.<span class="me1">AddressTypeID</span> <span class="sy0">=</span> <span class="nu0">2</span>;
&nbsp;
GO
&nbsp;
&nbsp;
<span class="kw1">CREATE</span> <span class="kw1">UNIQUE</span> <span class="kw1">CLUSTERED</span> <span class="kw1">INDEX</span> CI_vPersonID <span class="kw1">ON</span> Person.<span class="me1">vPerson</span><span class="br0">&#40;</span>BusinessEntityID<span class="br0">&#41;</span>;
&nbsp;
<span class="kw1">CREATE</span> <span class="kw1">INDEX</span> IX_vPersonName <span class="kw1">ON</span> Person.<span class="me1">vPerson</span><span class="br0">&#40;</span>FirstName,LastName<span class="br0">&#41;</span>;
<span class="kw1">CREATE</span> <span class="kw1">INDEX</span> IX_vPersonState <span class="kw1">ON</span> Person.<span class="me1">vPerson</span><span class="br0">&#40;</span>FirstName,LastName,StateProvinceID,StateProvinceCode,StateProvinceName<span class="br0">&#41;</span>;</pre></div></div>
<p>After executing all of that code to create this new view with its indexes, I have the following size results:</p>
<p><img decoding="async" class="alignnone wp-image-2809" src="https://sqlsolutionsgroup.com/wp-content/uploads/2016/01/Tablesize_post.jpg" alt="Tablesize_post" width="908" height="144" srcset="https://sqlsolutionsgroup.com/wp-content/uploads/2016/01/Tablesize_post.jpg 1923w, https://sqlsolutionsgroup.com/wp-content/uploads/2016/01/Tablesize_post-300x48.jpg 300w, https://sqlsolutionsgroup.com/wp-content/uploads/2016/01/Tablesize_post-1024x162.jpg 1024w, https://sqlsolutionsgroup.com/wp-content/uploads/2016/01/Tablesize_post-768x122.jpg 768w, https://sqlsolutionsgroup.com/wp-content/uploads/2016/01/Tablesize_post-1536x244.jpg 1536w" sizes="(max-width: 908px) 100vw, 908px" /></p>
<p>The creation of this view has chewed up a bunch of storage. It has jumped right up to the number two spot on the biggest objects list within this database. You can see that differences by comparing the highlighted rows to the previous image. The vPerson view is highlighted in red in this second image to help point it out quickly.</p>
<p>Surely this must be a contrived example and people don&#8217;t really do this in the real world, right? The answer to that is simply: NO! It DOES happen. I see situations like this all too often. Far too often, large text fields are added to an indexed view to make retrieval faster. I have mimicked that by adding in two XML columns from the Person.Person table. This is definitely overkill because a simple join back to the table based on the BusinessEntityID would get me those two columns. All I have effectively done is duplicated data being stored and I have achieved that at the low low cost of increased storage of 25% for this small database. If you are curious, the column count between the Person.Person table and this new view is 13 columns each.</p>
<p>I call the 25% increase storage cost a significant increase. An increase of 25% in storage for a single materialized view is not an uncommon occurrence. I have seen multi-terabyte databases with 25% of the total data storage being caused by a single view. If you are tight on storage, any unexpected data volume increase can cause a non-linear growth in data requirements to occur just because of the presence of indexed views. Take that into consideration when presented with an option to either performance tune the code or to take a short-cut and create an indexed view.</p>
<h3>Band-aid Performance</h3>
<p>I alluded to the use of indexed views as a cog to help improve performance. This is done by taking the query that sucks and turning it into a view. The thought is that it will run faster because an index is created specifically for that data set. Generally the performance gains from using an indexed view, to camouflage the bad query, are seen when the query is complex. Unfortunately, the query I provided for this article is not terribly complex. Due to the simplicity of the first example, let&#8217;s look first at the execution plan for another query that runs terribly slow but is also not terribly complex:</p>
<p><img loading="lazy" decoding="async" class="alignnone size-large wp-image-2836" src="https://sqlsolutionsgroup.com/wp-content/uploads/2016/01/view2idxnewplan-1024x801.jpg" alt="view2idxnewplan" width="669" height="523" srcset="https://sqlsolutionsgroup.com/wp-content/uploads/2016/01/view2idxnewplan-1024x801.jpg 1024w, https://sqlsolutionsgroup.com/wp-content/uploads/2016/01/view2idxnewplan-300x235.jpg 300w, https://sqlsolutionsgroup.com/wp-content/uploads/2016/01/view2idxnewplan-768x601.jpg 768w, https://sqlsolutionsgroup.com/wp-content/uploads/2016/01/view2idxnewplan.jpg 1382w" sizes="(max-width: 669px) 100vw, 669px" /></p>
<p>There are a few table spools and index scans. The most costly operator of the bunch appears to be a merge join with a many-to-many join operation. The indexed view in this case does give me a very significant improvement on this query (look at that estimated cost and then the estimated number of rows). What if I could tune the query a little and avoid the all of storage cost? Let&#8217;s see!</p>
<p>Consider the base query for this new view:</p>
<div id="wpshdo_2" class="wp-synhighlighter-outer"><div id="wpshdt_2" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_2"></a><a id="wpshat_2" class="wp-synhighlighter-title" href="#codesyntax_2"  onClick="javascript:wpsh_toggleBlock(2)" title="Click to show/hide code block">Source code</a></td><td align="right"><a href="#codesyntax_2" onClick="javascript:wpsh_code(2)" title="Show code only"><img decoding="async" border="0" style="border: 0 none" src="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_2" onClick="javascript:wpsh_print(2)" title="Print code"><img decoding="async" border="0" style="border: 0 none" src="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img decoding="async" border="0" style="border: 0 none" src="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_2" class="wp-synhighlighter-inner" style="display: block;"><pre class="tsql" style="font-family:monospace;"><span class="kw1">SELECT</span> pp.<span class="me1">BusinessEntityID</span>
			, pp.<span class="me1">FirstName</span>
			, pp.<span class="me1">LastName</span>
			, sp.<span class="br0">[</span>StateProvinceID<span class="br0">]</span>
			, sp.<span class="br0">[</span>StateProvinceCode<span class="br0">]</span>
			, sp.<span class="br0">[</span>IsOnlyStateProvinceFlag<span class="br0">]</span>
			, sp.<span class="br0">[</span>Name<span class="br0">]</span> <span class="kw1">AS</span> <span class="br0">[</span>StateProvinceName<span class="br0">]</span>
			, sp.<span class="br0">[</span>TerritoryID<span class="br0">]</span>
			, cr.<span class="br0">[</span>CountryRegionCode<span class="br0">]</span>
			, cr.<span class="br0">[</span>Name<span class="br0">]</span> <span class="kw1">AS</span> <span class="br0">[</span>CountryRegionName<span class="br0">]</span>
			, pbe.<span class="me1">AddressTypeID</span>
			, pp.<span class="me1">AdditionalContactInfo</span>
			, pp.<span class="me1">Demographics</span>
		<span class="kw1">FROM</span> <span class="br0">[</span>Person<span class="br0">]</span>.<span class="br0">[</span>StateProvince<span class="br0">]</span> sp 
			<span class="kw1">INNER</span> <span class="sy0">JOIN</span> <span class="br0">[</span>Person<span class="br0">]</span>.<span class="br0">[</span>CountryRegion<span class="br0">]</span> cr
				<span class="kw1">ON</span> <span class="kw2">RTRIM</span><span class="br0">&#40;</span><span class="kw2">LTRIM</span><span class="br0">&#40;</span>sp.<span class="br0">[</span>CountryRegionCode<span class="br0">]</span><span class="br0">&#41;</span><span class="br0">&#41;</span> <span class="sy0">=</span> <span class="kw2">RTRIM</span><span class="br0">&#40;</span><span class="kw2">LTRIM</span><span class="br0">&#40;</span>cr.<span class="br0">[</span>CountryRegionCode<span class="br0">]</span><span class="br0">&#41;</span><span class="br0">&#41;</span>
			<span class="kw1">INNER</span> <span class="sy0">JOIN</span> Person.<span class="me1">Address</span> pa
				<span class="kw1">ON</span> <span class="kw2">RTRIM</span><span class="br0">&#40;</span><span class="kw2">LTRIM</span><span class="br0">&#40;</span>sp.<span class="me1">StateProvinceID</span><span class="br0">&#41;</span><span class="br0">&#41;</span> <span class="sy0">=</span> <span class="kw2">RTRIM</span><span class="br0">&#40;</span><span class="kw2">LTRIM</span><span class="br0">&#40;</span>pa.<span class="me1">StateProvinceID</span><span class="br0">&#41;</span><span class="br0">&#41;</span>
			<span class="kw1">INNER</span> <span class="sy0">JOIN</span> Person.<span class="me1">BusinessEntityAddress</span> pbe
				<span class="kw1">ON</span> <span class="kw2">RTRIM</span><span class="br0">&#40;</span><span class="kw2">LTRIM</span><span class="br0">&#40;</span>pbe.<span class="me1">AddressID</span><span class="br0">&#41;</span><span class="br0">&#41;</span> <span class="sy0">=</span> <span class="kw2">RTRIM</span><span class="br0">&#40;</span><span class="kw2">LTRIM</span><span class="br0">&#40;</span>pa.<span class="me1">AddressID</span><span class="br0">&#41;</span><span class="br0">&#41;</span>
			<span class="kw1">INNER</span> <span class="sy0">JOIN</span> Person.<span class="me1">Person</span> pp
				<span class="kw1">ON</span> <span class="kw2">RTRIM</span><span class="br0">&#40;</span><span class="kw2">LTRIM</span><span class="br0">&#40;</span>pp.<span class="me1">BusinessEntityID</span><span class="br0">&#41;</span><span class="br0">&#41;</span> <span class="sy0">=</span> <span class="kw2">RTRIM</span><span class="br0">&#40;</span><span class="kw2">LTRIM</span><span class="br0">&#40;</span>pbe.<span class="me1">BusinessEntityID</span><span class="br0">&#41;</span><span class="br0">&#41;</span>
		<span class="kw1">WHERE</span> <span class="kw2">RTRIM</span><span class="br0">&#40;</span><span class="kw2">LTRIM</span><span class="br0">&#40;</span>pbe.<span class="me1">AddressTypeID</span><span class="br0">&#41;</span><span class="br0">&#41;</span> <span class="sy0">=</span> <span class="nu0">2</span>;</pre></div></div>
<p>Looking at the query, you may be asking yourself why use so many trim functions on each of the joins? This is an extreme that can be easily fixed. None of the fields in the joins should need to be trimmed since they are all numeric in this case. Starting there and removing all of the function use in the joins should provide significant improvements in query speed and without the use of a view. Testing it out would yield similar performance results to the first view in this article. This small change would cause the query to complete in 1 second compared to about 90 seconds. That is a huge gain, and without the added cost of increased storage use.</p>
<p>Do I really see stuff like this in the field? Yes! Spending a little time to fix the root of the problem rather than looking for a quick fix and performance can be spectacular. There are times, though, that an indexed view may be an absolute requirement. That would be perfectly fine. There are times when it is warranted and there is no way around it. If an indexed view is an absolute must, then there are a couple more considerations to be taken into account.</p>
<h3>Disappearing Act</h3>
<p>I showed previously that the indexed view requires storage space and showed that the new view created quickly claimed its place as the second largest object within the database. Now, to stage the next segment, I will show once again that the indexes are present and consuming disk space. This can be shown via the following query:</p>
<div id="wpshdo_3" class="wp-synhighlighter-outer"><div id="wpshdt_3" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_3"></a><a id="wpshat_3" class="wp-synhighlighter-title" href="#codesyntax_3"  onClick="javascript:wpsh_toggleBlock(3)" title="Click to show/hide code block">Source code</a></td><td align="right"><a href="#codesyntax_3" onClick="javascript:wpsh_code(3)" title="Show code only"><img decoding="async" border="0" style="border: 0 none" src="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_3" onClick="javascript:wpsh_print(3)" title="Print code"><img decoding="async" border="0" style="border: 0 none" src="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img decoding="async" border="0" style="border: 0 none" src="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_3" class="wp-synhighlighter-inner" style="display: block;"><pre class="tsql" style="font-family:monospace;"><span class="kw1">SELECT</span> o.<span class="me1">name</span> <span class="kw1">AS</span> ObjName
		, i.<span class="me1">name</span> <span class="kw1">AS</span> IdxName
		, <span class="kw2">SUM</span><span class="br0">&#40;</span>ps.<span class="me1">page_count</span><span class="br0">&#41;</span> <span class="kw1">AS</span> page_count
		, <span class="kw2">SUM</span><span class="br0">&#40;</span>ps.<span class="me1">record_count</span><span class="br0">&#41;</span> <span class="kw1">AS</span> record_count
		, <span class="kw2">SUM</span><span class="br0">&#40;</span>ps.<span class="me1">page_count</span><span class="br0">&#41;</span> <span class="sy0">/</span> 128.0 <span class="kw1">AS</span> SizeMB
	<span class="kw1">FROM</span> sys.<span class="me1">indexes</span> i
		<span class="kw1">INNER</span> <span class="sy0">JOIN</span> sys.<span class="me1">objects</span> o
			<span class="kw1">ON</span> i.<span class="kw2">OBJECT_ID</span> <span class="sy0">=</span> o.<span class="kw2">OBJECT_ID</span>
		<span class="kw1">INNER</span> <span class="sy0">JOIN</span> sys.<span class="me1">dm_db_index_physical_stats</span><span class="br0">&#40;</span><span class="kw2">DB_ID</span><span class="br0">&#40;</span><span class="br0">&#41;</span>, <span class="sy0">NULL</span>, <span class="sy0">NULL</span>, <span class="sy0">NULL</span>,
													<span class="st0">'detailed'</span><span class="br0">&#41;</span> ps
			<span class="kw1">ON</span> ps.<span class="kw2">OBJECT_ID</span> <span class="sy0">=</span> o.<span class="kw2">OBJECT_ID</span>
	<span class="kw1">WHERE</span> o.<span class="me1">name</span> <span class="sy0">LIKE</span> <span class="st0">'vperson%'</span>
	<span class="kw1">GROUP</span> <span class="kw1">BY</span> o.<span class="me1">name</span>
		, i.<span class="me1">name</span>;</pre></div></div>
<p>And the results of that query should look something like the following (quick reminder that I created a new indexed view for the second demo and these results will be based on that second view):</p>
<p><img loading="lazy" decoding="async" class="alignnone size-large wp-image-2833" src="https://sqlsolutionsgroup.com/wp-content/uploads/2016/01/vperson2-1024x231.jpg" alt="vperson2" width="669" height="151" srcset="https://sqlsolutionsgroup.com/wp-content/uploads/2016/01/vperson2-1024x231.jpg 1024w, https://sqlsolutionsgroup.com/wp-content/uploads/2016/01/vperson2-300x68.jpg 300w, https://sqlsolutionsgroup.com/wp-content/uploads/2016/01/vperson2-768x173.jpg 768w, https://sqlsolutionsgroup.com/wp-content/uploads/2016/01/vperson2.jpg 1052w" sizes="(max-width: 669px) 100vw, 669px" /></p>
<p>Now that we know the index is present and consuming space, time to make a change to the view and see what happens. I am proceeding under the premise that I have determined that including the two XML columns in the view are completely unnecessary and are the cause of too much space consumption. I can reap the benefits of the view at a fraction of the cost if I ALTER the view and remove those columns. So I will proceed by issuing the following ALTER statement:</p>
<div id="wpshdo_4" class="wp-synhighlighter-outer"><div id="wpshdt_4" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_4"></a><a id="wpshat_4" class="wp-synhighlighter-title" href="#codesyntax_4"  onClick="javascript:wpsh_toggleBlock(4)" title="Click to show/hide code block">Source code</a></td><td align="right"><a href="#codesyntax_4" onClick="javascript:wpsh_code(4)" title="Show code only"><img decoding="async" border="0" style="border: 0 none" src="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_4" onClick="javascript:wpsh_print(4)" title="Print code"><img decoding="async" border="0" style="border: 0 none" src="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img decoding="async" border="0" style="border: 0 none" src="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_4" class="wp-synhighlighter-inner" style="display: block;"><pre class="tsql" style="font-family:monospace;"><span class="kw1">ALTER</span> <span class="kw1">VIEW</span> <span class="br0">[</span>Person<span class="br0">]</span>.<span class="br0">[</span>vPerson2<span class="br0">]</span>
<span class="kw1">WITH</span> SCHEMAB<span class="sy0">IN</span>D<span class="sy0">IN</span>G
<span class="kw1">AS</span>
	<span class="kw1">SELECT</span> pp.<span class="me1">BusinessEntityID</span>
			, pp.<span class="me1">FirstName</span>
			, pp.<span class="me1">LastName</span>
			, sp.<span class="br0">[</span>StateProvinceID<span class="br0">]</span>
			, sp.<span class="br0">[</span>StateProvinceCode<span class="br0">]</span>
			, sp.<span class="br0">[</span>IsOnlyStateProvinceFlag<span class="br0">]</span>
			, sp.<span class="br0">[</span>Name<span class="br0">]</span> <span class="kw1">AS</span> <span class="br0">[</span>StateProvinceName<span class="br0">]</span>
			, sp.<span class="br0">[</span>TerritoryID<span class="br0">]</span>
			, cr.<span class="br0">[</span>CountryRegionCode<span class="br0">]</span>
			, cr.<span class="br0">[</span>Name<span class="br0">]</span> <span class="kw1">AS</span> <span class="br0">[</span>CountryRegionName<span class="br0">]</span>
			, pbe.<span class="me1">AddressTypeID</span>
		<span class="kw1">FROM</span> <span class="br0">[</span>Person<span class="br0">]</span>.<span class="br0">[</span>StateProvince<span class="br0">]</span> sp 
			<span class="kw1">INNER</span> <span class="sy0">JOIN</span> <span class="br0">[</span>Person<span class="br0">]</span>.<span class="br0">[</span>CountryRegion<span class="br0">]</span> cr
				<span class="kw1">ON</span> <span class="kw2">RTRIM</span><span class="br0">&#40;</span><span class="kw2">LTRIM</span><span class="br0">&#40;</span>sp.<span class="br0">[</span>CountryRegionCode<span class="br0">]</span><span class="br0">&#41;</span><span class="br0">&#41;</span> <span class="sy0">=</span> <span class="kw2">RTRIM</span><span class="br0">&#40;</span><span class="kw2">LTRIM</span><span class="br0">&#40;</span>cr.<span class="br0">[</span>CountryRegionCode<span class="br0">]</span><span class="br0">&#41;</span><span class="br0">&#41;</span>
			<span class="kw1">INNER</span> <span class="sy0">JOIN</span> Person.<span class="me1">Address</span> pa
				<span class="kw1">ON</span> <span class="kw2">RTRIM</span><span class="br0">&#40;</span><span class="kw2">LTRIM</span><span class="br0">&#40;</span>sp.<span class="me1">StateProvinceID</span><span class="br0">&#41;</span><span class="br0">&#41;</span> <span class="sy0">=</span> <span class="kw2">RTRIM</span><span class="br0">&#40;</span><span class="kw2">LTRIM</span><span class="br0">&#40;</span>pa.<span class="me1">StateProvinceID</span><span class="br0">&#41;</span><span class="br0">&#41;</span>
			<span class="kw1">INNER</span> <span class="sy0">JOIN</span> Person.<span class="me1">BusinessEntityAddress</span> pbe
				<span class="kw1">ON</span> <span class="kw2">RTRIM</span><span class="br0">&#40;</span><span class="kw2">LTRIM</span><span class="br0">&#40;</span>pbe.<span class="me1">AddressID</span><span class="br0">&#41;</span><span class="br0">&#41;</span> <span class="sy0">=</span> <span class="kw2">RTRIM</span><span class="br0">&#40;</span><span class="kw2">LTRIM</span><span class="br0">&#40;</span>pa.<span class="me1">AddressID</span><span class="br0">&#41;</span><span class="br0">&#41;</span>
			<span class="kw1">INNER</span> <span class="sy0">JOIN</span> Person.<span class="me1">Person</span> pp
				<span class="kw1">ON</span> <span class="kw2">RTRIM</span><span class="br0">&#40;</span><span class="kw2">LTRIM</span><span class="br0">&#40;</span>pp.<span class="me1">BusinessEntityID</span><span class="br0">&#41;</span><span class="br0">&#41;</span> <span class="sy0">=</span> <span class="kw2">RTRIM</span><span class="br0">&#40;</span><span class="kw2">LTRIM</span><span class="br0">&#40;</span>pbe.<span class="me1">BusinessEntityID</span><span class="br0">&#41;</span><span class="br0">&#41;</span>
		<span class="kw1">WHERE</span> <span class="kw2">RTRIM</span><span class="br0">&#40;</span><span class="kw2">LTRIM</span><span class="br0">&#40;</span>pbe.<span class="me1">AddressTypeID</span><span class="br0">&#41;</span><span class="br0">&#41;</span> <span class="sy0">=</span> 2
;
&nbsp;
GO</pre></div></div>
<p>Take note of how long this ALTER command takes to complete &#8211; nearly instantaneous.  That rocks! Right? Let&#8217;s look at the indexes to make sure they are still in working order and don&#8217;t require a defrag. Based on that, I will re-run that query to check the indexes and sizes &#8211; for giggles.</p>
<div id="wpshdo_5" class="wp-synhighlighter-outer"><div id="wpshdt_5" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_5"></a><a id="wpshat_5" class="wp-synhighlighter-title" href="#codesyntax_5"  onClick="javascript:wpsh_toggleBlock(5)" title="Click to show/hide code block">Source code</a></td><td align="right"><a href="#codesyntax_5" onClick="javascript:wpsh_code(5)" title="Show code only"><img decoding="async" border="0" style="border: 0 none" src="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_5" onClick="javascript:wpsh_print(5)" title="Print code"><img decoding="async" border="0" style="border: 0 none" src="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img decoding="async" border="0" style="border: 0 none" src="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_5" class="wp-synhighlighter-inner" style="display: block;"><pre class="tsql" style="font-family:monospace;"><span class="kw1">SELECT</span> o.<span class="me1">name</span> <span class="kw1">AS</span> ObjName
		, i.<span class="me1">name</span> <span class="kw1">AS</span> IdxName
		, <span class="kw2">SUM</span><span class="br0">&#40;</span>ps.<span class="me1">page_count</span><span class="br0">&#41;</span> <span class="kw1">AS</span> page_count
		, <span class="kw2">SUM</span><span class="br0">&#40;</span>ps.<span class="me1">record_count</span><span class="br0">&#41;</span> <span class="kw1">AS</span> record_count
		, <span class="kw2">SUM</span><span class="br0">&#40;</span>ps.<span class="me1">page_count</span><span class="br0">&#41;</span> <span class="sy0">/</span> 128.0 <span class="kw1">AS</span> SizeMB
	<span class="kw1">FROM</span> sys.<span class="me1">indexes</span> i
		<span class="kw1">INNER</span> <span class="sy0">JOIN</span> sys.<span class="me1">objects</span> o
			<span class="kw1">ON</span> i.<span class="kw2">OBJECT_ID</span> <span class="sy0">=</span> o.<span class="kw2">OBJECT_ID</span>
		<span class="kw1">INNER</span> <span class="sy0">JOIN</span> sys.<span class="me1">dm_db_index_physical_stats</span><span class="br0">&#40;</span><span class="kw2">DB_ID</span><span class="br0">&#40;</span><span class="br0">&#41;</span>, <span class="sy0">NULL</span>, <span class="sy0">NULL</span>, <span class="sy0">NULL</span>,
													<span class="st0">'detailed'</span><span class="br0">&#41;</span> ps
			<span class="kw1">ON</span> ps.<span class="kw2">OBJECT_ID</span> <span class="sy0">=</span> o.<span class="kw2">OBJECT_ID</span>
	<span class="kw1">WHERE</span> o.<span class="me1">name</span> <span class="sy0">LIKE</span> <span class="st0">'vperson%'</span>
	<span class="kw1">GROUP</span> <span class="kw1">BY</span> o.<span class="me1">name</span>
		, i.<span class="me1">name</span>;</pre></div></div>
<p>This time when the query completes, I get a nice little surprise: I no longer have any indexes on that view. Changing the view definition instantly drops any indexes that were built on that view. If this is done in a production system, imagine the implications and problems that could arise.</p>
<h3>Conclusion</h3>
<p>In this article, I have covered indexed views and three different considerations that are often overlooked when looking to implement a materialized view. The three considerations for indexed views, as discussed, are: storage, changes to the view, and the band-aid effect. Overlooking the cost of storage can have serious repercussions on the production environment. Implementing an indexed view without attempting to tune the code first could lead to overlooking the storage cost and could end up just being a waste of resources. The last tidbit is that any change to an indexed view will drop the indexes. That is an easily overlooked feature of indexed views. If you forget to recreate the indexes on the view after making changes, you could be looking at a production outage.</p>
<p>There are a couple more critical considerations for indexed views that are often overlooked as well. These additional considerations are blocking, deadlocking, and maintenance. These considerations will be discussed further in a future article.</p>
<p>The post <a href="https://sqlsolutionsgroup.com/indexed-view/">Indexed Views &#8211; Performance Panacea or Plight</a> appeared first on <a href="https://sqlsolutionsgroup.com">SQL Solutions Group</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://sqlsolutionsgroup.com/indexed-view/feed/</wfw:commentRss>
			<slash:comments>2</slash:comments>
		
		
			</item>
		<item>
		<title>Disk Space and SQL Server</title>
		<link>https://sqlsolutionsgroup.com/sql-disk-space/</link>
					<comments>https://sqlsolutionsgroup.com/sql-disk-space/#comments</comments>
		
		<dc:creator><![CDATA[Jason Brimhall]]></dc:creator>
		<pubDate>Thu, 21 Jan 2016 12:20:30 +0000</pubDate>
				<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Group]]></category>
		<category><![CDATA[#microsftcertifedmaster]]></category>
		<category><![CDATA[#microsftpartner]]></category>
		<category><![CDATA[#SQLAB]]></category>
		<category><![CDATA[#SQlatino]]></category>
		<category><![CDATA[#SQlatinoamerica]]></category>
		<category><![CDATA[#sqldatabase]]></category>
		<category><![CDATA[#sqldeveloper]]></category>
		<category><![CDATA[#SQLgroupie]]></category>
		<category><![CDATA[#sqlimer]]></category>
		<category><![CDATA[#sqlimerbymay]]></category>
		<category><![CDATA[#sqlinjection]]></category>
		<category><![CDATA[#sqlinternals]]></category>
		<category><![CDATA[#sqlite]]></category>
		<category><![CDATA[#sqlite3]]></category>
		<category><![CDATA[#SQLLearning]]></category>
		<category><![CDATA[#SQLMagazine]]></category>
		<category><![CDATA[#sqlmanagementstudio]]></category>
		<category><![CDATA[#sqlmanager]]></category>
		<category><![CDATA[#Sqlmap]]></category>
		<category><![CDATA[#sqlrun]]></category>
		<category><![CDATA[#sqlsaturday2017]]></category>
		<category><![CDATA[#sqlsatvienna]]></category>
		<category><![CDATA[#sqlserver]]></category>
		<category><![CDATA[#SQLserver2012]]></category>
		<category><![CDATA[#sqlserver2014]]></category>
		<category><![CDATA[#sqlserver2017]]></category>
		<category><![CDATA[#sqlserver2022]]></category>
		<category><![CDATA[#SQLServeronLinux]]></category>
		<category><![CDATA[#SQLsolutionsgroup]]></category>
		<category><![CDATA[#SQLTraining]]></category>
		<category><![CDATA[#SQLYog]]></category>
		<category><![CDATA[Database Administration]]></category>
		<category><![CDATA[SQL Administration]]></category>
		<category><![CDATA[SQL Internals]]></category>
		<category><![CDATA[SQLPASS]]></category>
		<category><![CDATA[SQLSaturday]]></category>
		<category><![CDATA[SSG]]></category>
		<guid isPermaLink="false">http://sqlsolutionsgroup.com/?p=2785</guid>

					<description><![CDATA[<p>One of the frequently required job functions of the database administrator is to track disk space consumption. Whether this requirement comes from management or from a learning opportunity after a production outage, the need exists. As a hard working DBA, you want to make sure you hit all of the notes to make management sing your [&#8230;]</p>
<p>The post <a href="https://sqlsolutionsgroup.com/sql-disk-space/">Disk Space and SQL Server</a> appeared first on <a href="https://sqlsolutionsgroup.com">SQL Solutions Group</a>.</p>
]]></description>
										<content:encoded><![CDATA[<h2><img loading="lazy" decoding="async" class="wp-image-2790 alignright" src="https://sqlsolutionsgroup.com/wp-content/uploads/2016/01/sunburst_space-1024x836.jpg" alt="sunburst_space" width="365" height="298" srcset="https://sqlsolutionsgroup.com/wp-content/uploads/2016/01/sunburst_space-1024x836.jpg 1024w, https://sqlsolutionsgroup.com/wp-content/uploads/2016/01/sunburst_space-300x245.jpg 300w, https://sqlsolutionsgroup.com/wp-content/uploads/2016/01/sunburst_space-768x627.jpg 768w, https://sqlsolutionsgroup.com/wp-content/uploads/2016/01/sunburst_space.jpg 1059w" sizes="(max-width: 365px) 100vw, 365px" /></h2>
<p>One of the frequently required job functions of the database administrator is to track disk space consumption. Whether this requirement comes from management or from a learning opportunity after a production outage, the need exists.</p>
<p>As a hard working DBA, you want to make sure you hit all of the notes to make management sing your praises. Knowing just when the database may fill the drives and prevent a production outage just happens to be one of those sharp notes that could result in a raise and management singing hallelujah. The problem is, how do you do it from within SQL Server? You are just a DBA after all and the disk system is not your domain, right?</p>
<p>Trying to figure it out, you come across a pretty cool function within SQL Server. The name of the function is <a href="https://docs.microsoft.com/en-us/sql/relational-databases/system-dynamic-management-views/sys-dm-os-volume-stats-transact-sql?view=sql-server-2017">sys.dm_os_volume_stats</a>. Bonus! This is an excellent discovery, right? Let’s see just how it might work. First a sample query:</p>
<div id="wpshdo_6" class="wp-synhighlighter-outer"><div id="wpshdt_6" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_6"></a><a id="wpshat_6" class="wp-synhighlighter-title" href="#codesyntax_6"  onClick="javascript:wpsh_toggleBlock(6)" title="Click to show/hide code block">Source code</a></td><td align="right"><a href="#codesyntax_6" onClick="javascript:wpsh_code(6)" title="Show code only"><img decoding="async" border="0" style="border: 0 none" src="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_6" onClick="javascript:wpsh_print(6)" title="Print code"><img decoding="async" border="0" style="border: 0 none" src="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img decoding="async" border="0" style="border: 0 none" src="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_6" class="wp-synhighlighter-inner" style="display: block;"><pre class="tsql" style="font-family:monospace;"><span class="kw1">SELECT</span> <span class="kw1">DISTINCT</span>  ovs.<span class="me1">logical_volume_name</span> <span class="kw1">AS</span> VolumeName
		, ovs.<span class="me1">volume_mount_point</span> <span class="kw1">AS</span> DiskDrive
		, ovs.<span class="me1">available_bytes</span> <span class="kw1">AS</span> FreeSpace
		, ovs.<span class="me1">total_bytes</span> <span class="kw1">AS</span> CurrentSize
	<span class="kw1">FROM</span> sys.<span class="me1">master_files</span> mf
		<span class="sy0">CROSS</span> APPLY sys.<span class="me1">dm_os_volume_stats</span><span class="br0">&#40;</span>mf.<span class="me1">database_id</span>, mf.<span class="kw2">FILE_ID</span><span class="br0">&#41;</span> ovs;</pre></div></div>
<p>If I run that on my local system, I <em>might</em> end up with something that looks like the following:</p>
<p><img decoding="async" src="https://jasonbrimhall.info/wp-content/uploads/2019/03/dmv_space_run1.png" /></p>
<p>Looking at the image you may be wondering to yourself right now why I have highlighted a couple of things. You may also be wondering why I used the word “might” in the previous paragraph as well. The reasoning will become more evident as we progress. For now, you have resolved to continue testing the script so execute it again and end up with something that <em>may</em> look like the following (for the same server):</p>
<p><img decoding="async" src="https://jasonbrimhall.info/wp-content/uploads/2019/03/dmv_space_run2.png" /></p>
<p>Whoa! What just happened there? Why are there two listings for the C: drive? Why does each register a different value for the FreeSpace column? In addition without any additional usage on the C drive (as verified through other tools) the FreeSpace is changing between executions as well as within the same execution. This is problematic, so you continue testing:</p>
<p><img decoding="async" src="https://jasonbrimhall.info/wp-content/uploads/2019/03/dmv_space_run3.png" /></p>
<p>And yet again!</p>
<p><img decoding="async" src="https://jasonbrimhall.info/wp-content/uploads/2019/03/dmv_space_run4.png" /></p>
<p>This can’t be correct, can it? Just for giggles let’s modify it just a bit to see if there are any additional clues. Using the following changed script, hopefully a clue will help shed some light on this:</p>
<div id="wpshdo_7" class="wp-synhighlighter-outer"><div id="wpshdt_7" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_7"></a><a id="wpshat_7" class="wp-synhighlighter-title" href="#codesyntax_7"  onClick="javascript:wpsh_toggleBlock(7)" title="Click to show/hide code block">Source code</a></td><td align="right"><a href="#codesyntax_7" onClick="javascript:wpsh_code(7)" title="Show code only"><img decoding="async" border="0" style="border: 0 none" src="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_7" onClick="javascript:wpsh_print(7)" title="Print code"><img decoding="async" border="0" style="border: 0 none" src="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img decoding="async" border="0" style="border: 0 none" src="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_7" class="wp-synhighlighter-inner" style="display: block;"><pre class="tsql" style="font-family:monospace;"><span class="kw1">SELECT</span> ovs.<span class="me1">logical_volume_name</span> <span class="kw1">AS</span> VolumeName
		, ovs.<span class="me1">volume_mount_point</span> <span class="kw1">AS</span> DiskDrive
		, ovs.<span class="me1">available_bytes</span> <span class="kw1">AS</span> FreeSpace
		, ovs.<span class="me1">total_bytes</span> <span class="kw1">AS</span> CurrentSize
	<span class="kw1">FROM</span> sys.<span class="me1">master_files</span> mf
		<span class="sy0">CROSS</span> APPLY sys.<span class="me1">dm_os_volume_stats</span><span class="br0">&#40;</span>mf.<span class="me1">database_id</span>, mf.<span class="kw2">FILE_ID</span><span class="br0">&#41;</span> ovs;</pre></div></div>
<p>This script yields the following potential results:</p>
<p><img decoding="async" src="https://jasonbrimhall.info/wp-content/uploads/2019/03/dmv_space_nodistincts-754x1024.jpg" /></p>
<p>Look at the different highlighted areas! There are three different values for FreeSpace for the C: drive in this particular execution. The case of the C: drive plays no role in whether the value is recorded differently or not. This seems to be more of a bug within the dynamic management function. From execution to execution, using this particular method, one could end up with duplicate entries but distinct values. The sort of the execution could be returned differently (though we could fix that).</p>
<p>All of these tests were run on my local machine and I really do only have one C: drive. I should never receive multiple entries back for any drive. If using this particular DMF to track space usage, it could be somewhat problematic if the duplicate drive data pops up. How do we get around it, you ask? Here is another example that I have that has not yet produced this duplication:</p>
<div id="wpshdo_8" class="wp-synhighlighter-outer"><div id="wpshdt_8" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_8"></a><a id="wpshat_8" class="wp-synhighlighter-title" href="#codesyntax_8"  onClick="javascript:wpsh_toggleBlock(8)" title="Click to show/hide code block">Source code</a></td><td align="right"><a href="#codesyntax_8" onClick="javascript:wpsh_code(8)" title="Show code only"><img decoding="async" border="0" style="border: 0 none" src="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_8" onClick="javascript:wpsh_print(8)" title="Print code"><img decoding="async" border="0" style="border: 0 none" src="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img decoding="async" border="0" style="border: 0 none" src="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_8" class="wp-synhighlighter-inner" style="display: block;"><pre class="tsql" style="font-family:monospace;"><span class="kw1">WITH</span> presel <span class="kw1">AS</span> <span class="br0">&#40;</span><span class="kw1">SELECT</span> database_id, <span class="kw2">FILE_ID</span>,<span class="kw1">LEFT</span><span class="br0">&#40;</span>mf1.<span class="me1">physical_name</span>,3<span class="br0">&#41;</span> <span class="kw1">AS</span> Volume, ROW_NUMBER<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="kw1">OVER</span> <span class="br0">&#40;</span>PARTITION <span class="kw1">BY</span> <span class="kw1">LEFT</span><span class="br0">&#40;</span>mf1.<span class="me1">physical_name</span>,3<span class="br0">&#41;</span> <span class="kw1">ORDER</span> <span class="kw1">BY</span> mf1.<span class="me1">database_id</span><span class="br0">&#41;</span> <span class="kw1">AS</span> RowNum
					<span class="kw1">FROM</span> sys.<span class="me1">master_files</span> mf1<span class="br0">&#41;</span>
,roundtwo <span class="kw1">AS</span> <span class="br0">&#40;</span><span class="kw1">SELECT</span> <span class="kw1">DISTINCT</span> pr.<span class="me1">database_id</span>, pr.<span class="kw2">FILE_ID</span>
				<span class="kw1">FROM</span> presel pr
				<span class="kw1">WHERE</span> pr.<span class="me1">RowNum</span> <span class="sy0">=</span> 1<span class="br0">&#41;</span>
&nbsp;
<span class="kw1">SELECT</span> ovs.<span class="me1">logical_volume_name</span> <span class="kw1">AS</span> VolumeName
		, ovs.<span class="me1">volume_mount_point</span> <span class="kw1">AS</span> DiskDrive
		, ovs.<span class="me1">available_bytes</span> <span class="kw1">AS</span> FreeSpace
		, ovs.<span class="me1">total_bytes</span> <span class="kw1">AS</span> CurrentSize
	<span class="kw1">FROM</span> roundtwo mf
		<span class="sy0">CROSS</span> APPLY sys.<span class="me1">dm_os_volume_stats</span><span class="br0">&#40;</span>mf.<span class="me1">database_id</span>, mf.<span class="kw2">FILE_ID</span><span class="br0">&#41;</span> ovs;</pre></div></div>
<p>Using this version of the script is not terribly more complex, but it will prove to be more reliable. You can see I used some CTEs to provide a little trickery and ensure that I limit my results. What if it is a mount point or a non-standard drive letter? I have not tested that. Let me know how that goes. As you can see, I am restricting the drive selection by using the row_number function against the drive letter.</p>
<p>For alternative reliable methods to find your disk space consumption, I would recommend something different because it is tried and tested. I would recommend using a wmi call to fetch the data. Samples are provided as follows:</p>
<div id="wpshdo_9" class="wp-synhighlighter-outer"><div id="wpshdt_9" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_9"></a><a id="wpshat_9" class="wp-synhighlighter-title" href="#codesyntax_9"  onClick="javascript:wpsh_toggleBlock(9)" title="Click to show/hide code block">Source code</a></td><td align="right"><a href="#codesyntax_9" onClick="javascript:wpsh_code(9)" title="Show code only"><img decoding="async" border="0" style="border: 0 none" src="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_9" onClick="javascript:wpsh_print(9)" title="Print code"><img decoding="async" border="0" style="border: 0 none" src="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img decoding="async" border="0" style="border: 0 none" src="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_9" class="wp-synhighlighter-inner" style="display: block;"><pre class="tsql" style="font-family:monospace;"><span class="coMULTI">/* for mount points, something like this */</span>
<span class="kw1">EXECUTE</span> sys.<span class="me1">xp_cmdshell</span> <span class="st0">'wmic volume get name, freespace, capacity, label'</span>
<span class="coMULTI">/* the base wmi query that does not support mount points */</span>
<span class="kw1">EXECUTE</span> xp_cmdshell <span class="st0">'wmic logicaldisk get name,freespace,size,volumename,blocksize'</span></pre></div></div>
<p>Easy peasy, right? Have at it and try tracking your disk space.</p>
<p>The post <a href="https://sqlsolutionsgroup.com/sql-disk-space/">Disk Space and SQL Server</a> appeared first on <a href="https://sqlsolutionsgroup.com">SQL Solutions Group</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://sqlsolutionsgroup.com/sql-disk-space/feed/</wfw:commentRss>
			<slash:comments>2</slash:comments>
		
		
			</item>
		<item>
		<title>SQL Black Box Recorder</title>
		<link>https://sqlsolutionsgroup.com/sql-black-box-recorder/</link>
		
		<dc:creator><![CDATA[Jason Brimhall]]></dc:creator>
		<pubDate>Tue, 12 Jan 2016 19:33:32 +0000</pubDate>
				<category><![CDATA[Extended Events]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Group]]></category>
		<category><![CDATA[#microsftcertifedmaster]]></category>
		<category><![CDATA[#microsftpartner]]></category>
		<category><![CDATA[#SQLAB]]></category>
		<category><![CDATA[#SQlatino]]></category>
		<category><![CDATA[#SQlatinoamerica]]></category>
		<category><![CDATA[#sqldatabase]]></category>
		<category><![CDATA[#sqldeveloper]]></category>
		<category><![CDATA[#SQLgroupie]]></category>
		<category><![CDATA[#sqlimer]]></category>
		<category><![CDATA[#sqlimerbymay]]></category>
		<category><![CDATA[#sqlinjection]]></category>
		<category><![CDATA[#sqlinternals]]></category>
		<category><![CDATA[#sqlite]]></category>
		<category><![CDATA[#sqlite3]]></category>
		<category><![CDATA[#SQLLearning]]></category>
		<category><![CDATA[#SQLMagazine]]></category>
		<category><![CDATA[#sqlmanagementstudio]]></category>
		<category><![CDATA[#sqlmanager]]></category>
		<category><![CDATA[#Sqlmap]]></category>
		<category><![CDATA[#sqlrun]]></category>
		<category><![CDATA[#sqlsaturday2017]]></category>
		<category><![CDATA[#sqlsatvienna]]></category>
		<category><![CDATA[#sqlserver]]></category>
		<category><![CDATA[#SQLserver2012]]></category>
		<category><![CDATA[#sqlserver2014]]></category>
		<category><![CDATA[#sqlserver2017]]></category>
		<category><![CDATA[#sqlserver2022]]></category>
		<category><![CDATA[#SQLServeronLinux]]></category>
		<category><![CDATA[#SQLsolutionsgroup]]></category>
		<category><![CDATA[#SQLTraining]]></category>
		<category><![CDATA[#SQLYog]]></category>
		<category><![CDATA[Audit]]></category>
		<category><![CDATA[SQL Administration]]></category>
		<category><![CDATA[SQL Internals]]></category>
		<category><![CDATA[SQLPASS]]></category>
		<category><![CDATA[SQLSaturday]]></category>
		<category><![CDATA[SSG]]></category>
		<guid isPermaLink="false">http://sqlsolutionsgroup.com/?p=2764</guid>

					<description><![CDATA[<p>Across many professions and industries there is often a need to have some sort of device that &#8220;audits&#8221; everything that happens with a device or process. We are probably all quite familiar with the infamous black box recorders used by the passenger airline and train industries. It is also quite possibly fairly common knowledge that [&#8230;]</p>
<p>The post <a href="https://sqlsolutionsgroup.com/sql-black-box-recorder/">SQL Black Box Recorder</a> appeared first on <a href="https://sqlsolutionsgroup.com">SQL Solutions Group</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p><a href="https://bit.ly/1ZhJI1f"><img loading="lazy" decoding="async" class="alignright wp-image-2769" src="https://sqlsolutionsgroup.com/wp-content/uploads/2016/01/blackbox2.png" alt="trace blackbox" width="280" height="213" srcset="https://sqlsolutionsgroup.com/wp-content/uploads/2016/01/blackbox2.png 506w, https://sqlsolutionsgroup.com/wp-content/uploads/2016/01/blackbox2-300x228.png 300w" sizes="(max-width: 280px) 100vw, 280px" /></a>Across many professions and industries there is often a need to have some sort of device that &#8220;audits&#8221; everything that happens with a device or process. We are probably all quite familiar with the infamous black box recorders used by the passenger airline and train industries. It is also quite possibly fairly common knowledge that many pace makers perform the same sort of functionality &#8211; on a smaller scale. Various metrics and data points are registered and recorded somewhere. Whether to a local storage device or whether the device phones home to some other remote location, it is recorded.</p>
<p>We often pontificate about the what-ifs for a black box recorder within SQL Server, right? We wish and dream and go about creating our own little recorder to suit the requirements we deem necessary for successful monitoring of the server for the just-in-case scenario. Well, the truth of the matter is that we really don&#8217;t need to go to such great lengths to create a &#8220;black box recorder&#8221; because Microsoft has already done all of that for us.</p>
<p>Wait, what? Yes, that’s right! Truth be told this shouldn’t be much news for most Senior Level data professionals working with SQL Server for the past few years. But if you are new to the product, this might be good news for you. Then again, it might just be bad news depending on your take.</p>
<p>Very much like what you may find with the airline industry, the black box recorder in SQL Server is not just a single method (device) implemented to capture all of the desired data points. On a passenger jet, you may find that there are three or more such devices that contribute to the capture and recording of the in-flight data. In SQL Server, there are three major processes that help capture our in-flight data. Over the next few articles I will discuss each of these processes. These processes include:</p>
<ul>
<li>Default Trace</li>
<li>system_health Extended Event Session</li>
<li>sp_server_diagnostics procedure</li>
</ul>
<p>This multi-tiered approach does provide a sort of fail-safe system. If one should be disabled, there still remain up to two more processes that may be running. That said, each of these can be disabled but it does take a bit of an overt effort. And since it does require somebody to put out effort to try and disable each of the black box components, you could potentially capture the culprit via audits from the other components or via an explicit audit that you create for these types of purposes.</p>
<p>Since this will be something to discuss over a few articles, I will break out each process into an individual article. For today, I will discuss the default trace.</p>
<h2>Default Trace</h2>
<p>The <a href="https://docs.microsoft.com/en-us/sql/database-engine/configure-windows/default-trace-enabled-server-configuration-option?view=sql-server-2017">default trace</a> by itself is something that can be turned off via configuration option. There may be good reason to disable the default trace. Before disabling the default trace, please consider the following that can be captured via the default trace. I will use a query to demonstrate the events and categories that are configured for capture in the default trace.</p>
<p><div id="wpshdo_10" class="wp-synhighlighter-outer"><div id="wpshdt_10" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_10"></a><a id="wpshat_10" class="wp-synhighlighter-title" href="#codesyntax_10"  onClick="javascript:wpsh_toggleBlock(10)" title="Click to show/hide code block">Source code</a></td><td align="right"><a href="#codesyntax_10" onClick="javascript:wpsh_code(10)" title="Show code only"><img decoding="async" border="0" style="border: 0 none" src="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_10" onClick="javascript:wpsh_print(10)" title="Print code"><img decoding="async" border="0" style="border: 0 none" src="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img decoding="async" border="0" style="border: 0 none" src="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_10" class="wp-synhighlighter-inner" style="display: block;"><pre class="tsql" style="font-family:monospace;"><span class="coMULTI">/* events */</span>
<span class="kw1">SELECT</span> te.<span class="me1">name</span> <span class="kw1">AS</span> EventName
		,tca.<span class="me1">name</span> <span class="kw1">AS</span> CategoryName
		, <span class="kw1">CASE</span> tca.<span class="me1">type</span> <span class="kw1">WHEN</span> <span class="st0">'0'</span> <span class="kw1">THEN</span> <span class="st0">'Normal'</span>
				<span class="kw1">WHEN</span> <span class="st0">'1'</span> <span class="kw1">THEN</span> <span class="st0">'Connection'</span>
				<span class="kw1">WHEN</span> <span class="st0">'2'</span> <span class="kw1">THEN</span> <span class="st0">'ERROR'</span> <span class="kw1">END</span> <span class="kw1">AS</span> CategoryType
		, t.<span class="kw1">PATH</span> <span class="kw1">AS</span> TracePath
		, oa.<span class="me1">logical_operator</span>,oa.<span class="me1">comparison_operator</span>, oa.<span class="kw1">VALUE</span> <span class="kw1">AS</span> FilteredValue
	<span class="kw1">FROM</span> sys.<span class="me1">traces</span> t
		<span class="sy0">CROSS</span> APPLY <span class="br0">&#40;</span><span class="kw1">SELECT</span> <span class="kw1">DISTINCT</span> gei.<span class="me1">eventid</span> <span class="kw1">FROM</span> sys.<span class="kw4">FN_TRACE_GETEVENTINFO</span><span class="br0">&#40;</span>t.<span class="me1">id</span><span class="br0">&#41;</span> gei<span class="br0">&#41;</span> ca
		<span class="kw1">INNER</span> <span class="sy0">JOIN</span> sys.<span class="me1">trace_events</span> te
			<span class="kw1">ON</span> te.<span class="me1">trace_event_id</span> <span class="sy0">=</span> ca.<span class="me1">eventid</span>
		<span class="kw1">INNER</span> <span class="sy0">JOIN</span> sys.<span class="me1">trace_categories</span> tca
			<span class="kw1">ON</span> te.<span class="me1">category_id</span> <span class="sy0">=</span> tca.<span class="me1">category_id</span>
		<span class="sy0">OUTER</span> APPLY <span class="br0">&#40;</span><span class="kw1">SELECT</span> gfi.<span class="me1">columnid</span>,gfi.<span class="me1">logical_operator</span>,gfi.<span class="me1">comparison_operator</span>,gfi.<span class="kw1">VALUE</span> <span class="kw1">FROM</span> sys.<span class="kw4">FN_TRACE_GETFILTERINFO</span><span class="br0">&#40;</span>t.<span class="me1">id</span><span class="br0">&#41;</span> gfi<span class="br0">&#41;</span> oa
	<span class="kw1">WHERE</span> t.<span class="me1">is_default</span> <span class="sy0">=</span> <span class="nu0">1</span></pre></div></div></p>
<p>In this query, I have requested a few more data points than necessary to illustrate the point. That is to help illustrate an additional point that the default trace isn’t filtering out any data for these events. If the event fires (in this trace), it is recorded. Let’s divert right back to the events and categories for now. The results of that query will produce the following sample list of events for me on SQL Server 2014:</p>
<p><img decoding="async" src="https://jasonbrimhall.info/wp-content/uploads/2018/12/deftrace_events-968x1024.png" /></p>
<p>That is quite a range of events covered by this particular recording device. From changes to objects down to security related events and even errors and warnings. If somebody drops an object, the default trace can catch it. If one of the various DBCC statements is executed, it will be trapped in this trace.</p>
<p>There is one thing that is captured by this trace that is not overly obvious. In fact, it is rather annoying in how it is recorded in my opinion. Server configuration settings such as “cost threshold of parallelism” or the lesser utilized “user options” are not registered as an “Object:Alter” event but rather as an ErrorLog event. I wouldn’t call this type of “change” an error and don’t necessarily like seeing these registered with other legitimate errors. That said, it would be nice to see these logged differently (they are currently logged the same way with Extended Events). So, if somebody is dorking around with server configurations, it becomes a bit more convoluted to figure it out, but we can get there. Let’s see how that works:</p>
<p><div id="wpshdo_11" class="wp-synhighlighter-outer"><div id="wpshdt_11" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_11"></a><a id="wpshat_11" class="wp-synhighlighter-title" href="#codesyntax_11"  onClick="javascript:wpsh_toggleBlock(11)" title="Click to show/hide code block">Source code</a></td><td align="right"><a href="#codesyntax_11" onClick="javascript:wpsh_code(11)" title="Show code only"><img decoding="async" border="0" style="border: 0 none" src="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_11" onClick="javascript:wpsh_print(11)" title="Print code"><img decoding="async" border="0" style="border: 0 none" src="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img decoding="async" border="0" style="border: 0 none" src="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_11" class="wp-synhighlighter-inner" style="display: block;"><pre class="tsql" style="font-family:monospace;"><span class="co1">-- server configs Audited via Def Trace</span>
<span class="kw1">SELECT</span> 	T.<span class="me1">StartTime</span>
		, T.<span class="me1">SPID</span>
		, T.<span class="me1">LoginName</span>
		, T.<span class="me1">HostName</span>
		, T.<span class="me1">ApplicationName</span>
		, T.<span class="me1">DatabaseName</span>
		<span class="co1">--, ObjectName,sv.number AS ObjTypeVal, sv.name [ObjectType]</span>
		<span class="co1">--, T.TextData</span>
		, ConfigOption <span class="sy0">=</span> <span class="kw2">SUBSTRING</span><span class="br0">&#40;</span>T.<span class="me1">TextData</span>,<span class="kw2">CHARINDEX</span><span class="br0">&#40;</span><span class="st0">''</span><span class="st0">''</span>,T.<span class="me1">TextData</span><span class="br0">&#41;</span><span class="sy0">+</span>1,<span class="kw2">CHARINDEX</span><span class="br0">&#40;</span><span class="st0">' changed from '</span>,T.<span class="me1">TextData</span><span class="br0">&#41;</span><span class="sy0">-</span><span class="kw2">CHARINDEX</span><span class="br0">&#40;</span><span class="st0">''</span><span class="st0">''</span>,T.<span class="me1">TextData</span><span class="br0">&#41;</span><span class="sy0">-</span>2<span class="br0">&#41;</span>
		, PrevValue <span class="sy0">=</span> <span class="kw2">SUBSTRING</span><span class="br0">&#40;</span>T.<span class="me1">TextData</span>,<span class="kw2">CHARINDEX</span><span class="br0">&#40;</span><span class="st0">'from '</span>,T.<span class="me1">TextData</span><span class="br0">&#41;</span><span class="sy0">+</span>5,<span class="kw2">CHARINDEX</span><span class="br0">&#40;</span><span class="st0">'to '</span>,T.<span class="me1">TextData</span><span class="br0">&#41;</span><span class="sy0">-</span><span class="kw2">CHARINDEX</span><span class="br0">&#40;</span><span class="st0">'from '</span>,T.<span class="me1">TextData</span><span class="br0">&#41;</span><span class="sy0">-</span>5<span class="br0">&#41;</span>
		, NewValue <span class="sy0">=</span> <span class="kw2">SUBSTRING</span><span class="br0">&#40;</span>T.<span class="me1">TextData</span>,<span class="kw2">CHARINDEX</span><span class="br0">&#40;</span><span class="st0">'to '</span>,T.<span class="me1">TextData</span><span class="br0">&#41;</span><span class="sy0">+</span>3,<span class="kw2">CHARINDEX</span><span class="br0">&#40;</span><span class="st0">'. Run'</span>,T.<span class="me1">TextData</span><span class="br0">&#41;</span><span class="sy0">-</span><span class="kw2">CHARINDEX</span><span class="br0">&#40;</span><span class="st0">'to '</span>,T.<span class="me1">TextData</span><span class="br0">&#41;</span><span class="sy0">-</span>3<span class="br0">&#41;</span>
		, EventName <span class="sy0">=</span> te.<span class="me1">name</span>
		, T.<span class="me1">EventClass</span>
	<span class="kw1">FROM</span> sys.<span class="kw4">FN_TRACE_GETTABLE</span><span class="br0">&#40;</span><span class="kw1">CONVERT</span><span class="br0">&#40;</span><span class="kw1">VARCHAR</span><span class="br0">&#40;</span>150<span class="br0">&#41;</span>, 
			<span class="br0">&#40;</span> <span class="kw1">SELECT</span> <span class="kw2">REVERSE</span><span class="br0">&#40;</span><span class="kw2">SUBSTRING</span><span class="br0">&#40;</span><span class="kw2">REVERSE</span><span class="br0">&#40;</span><span class="kw1">PATH</span><span class="br0">&#41;</span>,
					<span class="kw2">CHARINDEX</span><span class="br0">&#40;</span><span class="st0">'<span class="es0">\'</span>,REVERSE(path)),256)) + '</span><span class="kw2">LOG</span>.<span class="me1">trc</span><span class="st0">'
				FROM    sys.traces
				WHERE   is_default = 1)), DEFAULT) AS T  
		INNER JOIN sys.trace_events AS te
			ON T.EventClass = te.trace_event_id
	WHERE T.EventClass = 22
		AND T.TextData LIKE '</span><span class="sy0">%</span>config<span class="sy0">%</span><span class="st0">'
	ORDER BY T.StartTime DESC;</span></pre></div></div></p>
<p>Looking at my system, I get the following sample results:</p>
<p><img decoding="async" src="https://jasonbrimhall.info/wp-content/uploads/2018/12/deftrace_servconfigs-1024x200.png" /></p>
<p>You can see there are plenty of settings that I have been playing with captured by the default trace. If you happen to have a junior DBA or maybe a cowboy DBA that loves to shoot from the hip and make changes, this will help you find all the necessary details to bring to that DBA for discussion.</p>
<p>The default trace comprises one third of the black box recorder. I have shown a quick use for the default trace and have also shown some of the various events that are captured from this trace. I recommend getting to know your default trace just a little bit better. You never know when you may need to resort to the flight recorder data held in the default trace. Being familiar with the default trace before you need to use it will help improve your comfort level when under the stress of trying to figure out what happened just before the server went belly up!</p>
<p>The post <a href="https://sqlsolutionsgroup.com/sql-black-box-recorder/">SQL Black Box Recorder</a> appeared first on <a href="https://sqlsolutionsgroup.com">SQL Solutions Group</a>.</p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>What Sessions are Deployed?</title>
		<link>https://sqlsolutionsgroup.com/view-deployed-sessions/</link>
		
		<dc:creator><![CDATA[Jason Brimhall]]></dc:creator>
		<pubDate>Tue, 01 Sep 2015 12:48:47 +0000</pubDate>
				<category><![CDATA[Extended Events]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Group]]></category>
		<category><![CDATA[#microsftcertifedmaster]]></category>
		<category><![CDATA[#microsftpartner]]></category>
		<category><![CDATA[#SQLAB]]></category>
		<category><![CDATA[#SQlatino]]></category>
		<category><![CDATA[#SQlatinoamerica]]></category>
		<category><![CDATA[#sqldatabase]]></category>
		<category><![CDATA[#sqldeveloper]]></category>
		<category><![CDATA[#SQLgroupie]]></category>
		<category><![CDATA[#sqlimer]]></category>
		<category><![CDATA[#sqlimerbymay]]></category>
		<category><![CDATA[#sqlinjection]]></category>
		<category><![CDATA[#sqlinternals]]></category>
		<category><![CDATA[#sqlite]]></category>
		<category><![CDATA[#sqlite3]]></category>
		<category><![CDATA[#SQLLearning]]></category>
		<category><![CDATA[#SQLMagazine]]></category>
		<category><![CDATA[#sqlmanagementstudio]]></category>
		<category><![CDATA[#sqlmanager]]></category>
		<category><![CDATA[#Sqlmap]]></category>
		<category><![CDATA[#sqlrun]]></category>
		<category><![CDATA[#sqlsaturday2017]]></category>
		<category><![CDATA[#sqlsatvienna]]></category>
		<category><![CDATA[#sqlserver]]></category>
		<category><![CDATA[#SQLserver2012]]></category>
		<category><![CDATA[#sqlserver2014]]></category>
		<category><![CDATA[#sqlserver2017]]></category>
		<category><![CDATA[#sqlserver2022]]></category>
		<category><![CDATA[#SQLServeronLinux]]></category>
		<category><![CDATA[#SQLsolutionsgroup]]></category>
		<category><![CDATA[#SQLTraining]]></category>
		<category><![CDATA[#SQLYog]]></category>
		<category><![CDATA[SQL Administration]]></category>
		<category><![CDATA[SQL Internals]]></category>
		<category><![CDATA[SQLPASS]]></category>
		<category><![CDATA[SQLSaturday]]></category>
		<category><![CDATA[SSG]]></category>
		<category><![CDATA[XE]]></category>
		<guid isPermaLink="false">http://sqlsolutionsgroup.com/?p=2550</guid>

					<description><![CDATA[<p>As a DBA, it is not uncommon to feel like you are marooned off on some tiny little island trying to figure things out for yourself. It may even be very common to feel the icy pain of the development cold shoulder (intentional or not) as you go about doing your duties to protect the [&#8230;]</p>
<p>The post <a href="https://sqlsolutionsgroup.com/view-deployed-sessions/">What Sessions are Deployed?</a> appeared first on <a href="https://sqlsolutionsgroup.com">SQL Solutions Group</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p><a href="https://sqlsolutionsgroup.com/wp-content/uploads/2015/08/polarbear.png"><img loading="lazy" decoding="async" class="alignright wp-image-2551" src="https://sqlsolutionsgroup.com/wp-content/uploads/2015/08/polarbear.png" alt="He's wondering about deployed sessions" width="270" height="241" srcset="https://sqlsolutionsgroup.com/wp-content/uploads/2015/08/polarbear.png 369w, https://sqlsolutionsgroup.com/wp-content/uploads/2015/08/polarbear-300x268.png 300w" sizes="(max-width: 270px) 100vw, 270px" /></a>As a DBA, it is not uncommon to feel like you are marooned off on some tiny little island trying to figure things out for yourself. It may even be very common to feel the icy pain of the development cold shoulder (intentional or not) as you go about doing your duties to protect the data and secure high octane performance out of the database.</p>
<p>Not only may it be very commonplace to experience this due to the nature of the job, but it can also get to feel that way when a new product is released. Suddenly, that icy island is further north into the Arctic Ocean and you are struggling to gasp for a bit of air in the frigid cold.</p>
<p>I am certain this may be some of what you feel as you try to take a look at Extended Events. It is a fancy, new technology and there is not a ton of time to get up to speed while trying to warm yourself against the development chill. As you stew about the situation, you begin to ponder, &#8220;How am I even supposed to know what an XE is, let alone what XEs are running on my server?&#8221; Never mind the confusion on what an XE is and that it is actually running or deployed sessions on the server. Don&#8217;t worry about the terminology just yet. <strong>What can we do to figure out what might be out there</strong>?</p>
<p>Well, that is where the trusty Google has come to help you. Thanks to a better than adequate ability to search the Internet, you landed on this page that has a script to help you explore what might be running as far as Extended Events on your server. You may be surprised to learn that you have more Extended Events Sessions running than you fathomed. Yes, SQL Server comes with some default sessions. Yes, some of these default sessions are more public than others. And yes, some of these</p>
<p><strong>Come on in out of the cold</strong>. Get off that arctic island and warm up with a little XE fun. This script is quite simple but will show you what you may have out there on your servers. Once you see what is there, hopefully your curiosity will get a little piqued and you will want to learn a little more (there is more of that here on this site too).</p>
<p>And now, for that script:</p>
<p><div id="wpshdo_12" class="wp-synhighlighter-outer"><div id="wpshdt_12" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_12"></a><a id="wpshat_12" class="wp-synhighlighter-title" href="#codesyntax_12"  onClick="javascript:wpsh_toggleBlock(12)" title="Click to show/hide code block">Source code</a></td><td align="right"><a href="#codesyntax_12" onClick="javascript:wpsh_code(12)" title="Show code only"><img decoding="async" border="0" style="border: 0 none" src="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_12" onClick="javascript:wpsh_print(12)" title="Print code"><img decoding="async" border="0" style="border: 0 none" src="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img decoding="async" border="0" style="border: 0 none" src="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_12" class="wp-synhighlighter-inner" style="display: block;"><pre class="tsql" style="font-family:monospace;"><span class="kw1">DECLARE</span> @SessionName <span class="kw1">VARCHAR</span><span class="br0">&#40;</span><span class="nu0">128</span><span class="br0">&#41;</span> <span class="sy0">=</span> <span class="sy0">NULL</span> <span class="co1">--'sp_server_diagnostics session' --NULL for all</span>
;
&nbsp;
<span class="kw1">SELECT</span> IS<span class="sy0">NULL</span><span class="br0">&#40;</span>ses.<span class="me1">name</span>,xse.<span class="me1">name</span><span class="br0">&#41;</span> <span class="kw1">AS</span> SessionName
		, <span class="kw1">CASE</span>
			<span class="kw1">WHEN</span> IS<span class="sy0">NULL</span><span class="br0">&#40;</span>ses.<span class="me1">name</span>,<span class="st0">''</span><span class="br0">&#41;</span> <span class="sy0">=</span> <span class="st0">''</span>
			<span class="kw1">THEN</span> <span class="st0">'Private'</span>
			<span class="kw1">ELSE</span> <span class="st0">'Public'</span>
			<span class="kw1">END</span> <span class="kw1">AS</span> SessionVisibility
		, <span class="kw1">CASE</span>
			<span class="kw1">WHEN</span> IS<span class="sy0">NULL</span><span class="br0">&#40;</span>xse.<span class="me1">name</span>,<span class="st0">''</span><span class="br0">&#41;</span> <span class="sy0">=</span> <span class="st0">''</span>
			<span class="kw1">THEN</span> <span class="st0">'NO'</span>
			<span class="kw1">ELSE</span> <span class="st0">'YES'</span>
			<span class="kw1">END</span> <span class="kw1">AS</span> SessionRunning
		, <span class="kw1">CASE</span>
			<span class="kw1">WHEN</span> IS<span class="sy0">NULL</span><span class="br0">&#40;</span>xse.<span class="me1">name</span>,<span class="st0">''</span><span class="br0">&#41;</span> <span class="sy0">=</span> <span class="st0">''</span>
				<span class="sy0">AND</span> IS<span class="sy0">NULL</span><span class="br0">&#40;</span>ses.<span class="me1">name</span>,<span class="st0">''</span><span class="br0">&#41;</span> <span class="sy0">=</span> <span class="st0">''</span>
			<span class="kw1">THEN</span> <span class="st0">'NO'</span>
			<span class="kw1">ELSE</span> <span class="st0">'YES'</span>
			<span class="kw1">END</span> <span class="kw1">AS</span> IsDeployed
	<span class="kw1">FROM</span> sys.<span class="me1">server_event_sessions</span> ses
	<span class="kw1">FULL</span> <span class="sy0">OUTER</span> <span class="sy0">JOIN</span> sys.<span class="me1">dm_xe_sessions</span> xse
		<span class="kw1">ON</span> xse.<span class="me1">name</span> <span class="sy0">=</span>ses.<span class="me1">name</span>
	<span class="kw1">WHERE</span> <span class="kw1">COALESCE</span><span class="br0">&#40;</span>@SessionName, ses.<span class="me1">name</span>, xse.<span class="me1">name</span><span class="br0">&#41;</span> <span class="sy0">=</span> IS<span class="sy0">NULL</span><span class="br0">&#40;</span>ses.<span class="me1">name</span>, xse.<span class="me1">name</span><span class="br0">&#41;</span>
	<span class="kw1">ORDER</span> <span class="kw1">BY</span> ses.<span class="me1">event_session_id</span>;
&nbsp;
GO</pre></div></div></p>
<p>Quick and simple, and full of that warming XE sensation. This script just shows you what is a private session versus public. What is a private session, you ask? Well, that happens to be all of those database audits you have deployed to your server. It also includes those little things like the “sp_server_diagnostics session” session (yes it sounds redundant but that is because session is also part of the name for that session).</p>
<p>This script will also show you what is deployed to the server versus what is running on the server (not all sessions have to be running). And to top it all off, I set it up so you can query for a specific session or for all sessions.</p>
<p>Try it out on one or more of your servers and let me know how it goes.</p>
<p>The post <a href="https://sqlsolutionsgroup.com/view-deployed-sessions/">What Sessions are Deployed?</a> appeared first on <a href="https://sqlsolutionsgroup.com">SQL Solutions Group</a>.</p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>An Experiment with Deadlocks</title>
		<link>https://sqlsolutionsgroup.com/an-experiment-with-deadlocks/</link>
		
		<dc:creator><![CDATA[Jason Brimhall]]></dc:creator>
		<pubDate>Tue, 21 Jul 2015 12:22:15 +0000</pubDate>
				<category><![CDATA[SQL Group]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[#microsftcertifedmaster]]></category>
		<category><![CDATA[#microsftpartner]]></category>
		<category><![CDATA[#SQLAB]]></category>
		<category><![CDATA[#SQlatino]]></category>
		<category><![CDATA[#SQlatinoamerica]]></category>
		<category><![CDATA[#sqldatabase]]></category>
		<category><![CDATA[#sqldeveloper]]></category>
		<category><![CDATA[#SQLgroupie]]></category>
		<category><![CDATA[#sqlimer]]></category>
		<category><![CDATA[#sqlimerbymay]]></category>
		<category><![CDATA[#sqlinjection]]></category>
		<category><![CDATA[#sqlinternals]]></category>
		<category><![CDATA[#sqlite]]></category>
		<category><![CDATA[#sqlite3]]></category>
		<category><![CDATA[#SQLLearning]]></category>
		<category><![CDATA[#SQLMagazine]]></category>
		<category><![CDATA[#sqlmanagementstudio]]></category>
		<category><![CDATA[#sqlmanager]]></category>
		<category><![CDATA[#Sqlmap]]></category>
		<category><![CDATA[#sqlrun]]></category>
		<category><![CDATA[#sqlsaturday2017]]></category>
		<category><![CDATA[#sqlsatvienna]]></category>
		<category><![CDATA[#sqlserver]]></category>
		<category><![CDATA[#SQLserver2012]]></category>
		<category><![CDATA[#sqlserver2014]]></category>
		<category><![CDATA[#sqlserver2017]]></category>
		<category><![CDATA[#sqlserver2022]]></category>
		<category><![CDATA[#SQLServeronLinux]]></category>
		<category><![CDATA[#SQLsolutionsgroup]]></category>
		<category><![CDATA[#SQLTraining]]></category>
		<category><![CDATA[#SQLYog]]></category>
		<category><![CDATA[deadlocks]]></category>
		<category><![CDATA[Extended Events]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Internals]]></category>
		<category><![CDATA[SQLPASS]]></category>
		<category><![CDATA[SQLSaturday]]></category>
		<category><![CDATA[SSG]]></category>
		<guid isPermaLink="false">http://sqlsolutionsgroup.com/?p=2460</guid>

					<description><![CDATA[<p>Everything can be fixed with a query hint (*cough* directive), right? If a certain process is consistently causing deadlocks, a simple ROWLOCK hint can be added to prevent it, right? Well, for whatever reason, there seems to be a myth out there that when deadlocks come a-knocking, then just throw this little directive at it and [&#8230;]</p>
<p>The post <a href="https://sqlsolutionsgroup.com/an-experiment-with-deadlocks/">An Experiment with Deadlocks</a> appeared first on <a href="https://sqlsolutionsgroup.com">SQL Solutions Group</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>Everything can be fixed with a query hint (*cough* directive), right? If a certain process is consistently causing deadlocks, a simple ROWLOCK hint can be added to prevent it, right?</p>
<p>Well, for whatever reason, there seems to be a myth out there that when deadlocks come a-knocking, then just throw this little directive at it and all will be well. Today, we get to test that and show what will continue to happen.</p>
<p>First, lets look at what the ROWLOCK actually means:</p>
<blockquote><p>Specifies that row locks are taken when page or table locks are ordinarily taken.</p></blockquote>
<p>This seems like a fabulous idea if the deadlocks are occurring against a table involving a massive update. Let’s take a look at a small update scenario involving just a handful of records.</p>
<p><div id="wpshdo_13" class="wp-synhighlighter-outer"><div id="wpshdt_13" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_13"></a><a id="wpshat_13" class="wp-synhighlighter-title" href="#codesyntax_13"  onClick="javascript:wpsh_toggleBlock(13)" title="Click to show/hide code block">Source code</a></td><td align="right"><a href="#codesyntax_13" onClick="javascript:wpsh_code(13)" title="Show code only"><img decoding="async" border="0" style="border: 0 none" src="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_13" onClick="javascript:wpsh_print(13)" title="Print code"><img decoding="async" border="0" style="border: 0 none" src="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img decoding="async" border="0" style="border: 0 none" src="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_13" class="wp-synhighlighter-inner" style="display: block;"><pre class="tsql" style="font-family:monospace;"><span class="kw1">USE</span> tempdb;
GO
<span class="kw1">IF</span> <span class="kw2">OBJECT_ID</span><span class="br0">&#40;</span><span class="st0">'dbo.Test1'</span><span class="br0">&#41;</span> <span class="kw1">IS</span> <span class="sy0">NOT</span> <span class="sy0">NULL</span>
	<span class="kw1">DROP</span> <span class="kw1">TABLE</span> dbo.<span class="me1">Test1</span>;
<span class="kw1">IF</span> <span class="kw2">OBJECT_ID</span><span class="br0">&#40;</span><span class="st0">'dbo.Test2'</span><span class="br0">&#41;</span> <span class="kw1">IS</span> <span class="sy0">NOT</span> <span class="sy0">NULL</span>
	<span class="kw1">DROP</span> <span class="kw1">TABLE</span> dbo.<span class="me1">Test2</span>;
<span class="kw1">CREATE</span> <span class="kw1">TABLE</span> dbo.<span class="me1">Test1</span> <span class="br0">&#40;</span> col1 <span class="kw1">INT</span> <span class="br0">&#41;</span>;
<span class="kw1">CREATE</span> <span class="kw1">TABLE</span> dbo.<span class="me1">Test2</span> <span class="br0">&#40;</span> col2 <span class="kw1">INT</span> <span class="br0">&#41;</span>;
<span class="kw1">INSERT</span> <span class="kw1">INTO</span> dbo.<span class="me1">Test1</span>
	<span class="kw1">VALUES</span>	<span class="br0">&#40;</span> 1 <span class="br0">&#41;</span>,
			<span class="br0">&#40;</span> 2 <span class="br0">&#41;</span>,
			<span class="br0">&#40;</span> 3 <span class="br0">&#41;</span>,
			<span class="br0">&#40;</span> 4 <span class="br0">&#41;</span>,
			<span class="br0">&#40;</span> 5 <span class="br0">&#41;</span>;
<span class="kw1">INSERT</span> <span class="kw1">INTO</span> dbo.<span class="me1">Test2</span>
	<span class="kw1">VALUES</span>	<span class="br0">&#40;</span> 1 <span class="br0">&#41;</span>,
			<span class="br0">&#40;</span> 2 <span class="br0">&#41;</span>,
			<span class="br0">&#40;</span> 3 <span class="br0">&#41;</span>,
			<span class="br0">&#40;</span> 4 <span class="br0">&#41;</span>,
			<span class="br0">&#40;</span> 5 <span class="br0">&#41;</span>;
GO
<span class="kw1">BEGIN</span> <span class="kw1">TRANSACTION</span>;
<span class="kw1">UPDATE</span> dbo.<span class="me1">Test1</span>
	<span class="kw1">SET</span>	col1 <span class="sy0">=</span> col1 <span class="sy0">*</span> 10
	<span class="kw1">WHERE</span> col1 <span class="sy0">=</span> <span class="nu0">3</span>;
&nbsp;
<span class="coMULTI">/* query window 2*/</span>
&nbsp;
<span class="kw1">USE</span> tempdb;
<span class="kw1">BEGIN</span> <span class="kw1">TRANSACTION</span>;
<span class="kw1">UPDATE</span> dbo.<span class="me1">Test2</span>
	<span class="kw1">SET</span>	col2 <span class="sy0">=</span> col2 <span class="sy0">*</span> 20
	<span class="kw1">WHERE</span> col2 <span class="sy0">=</span> <span class="nu0">4</span>;
<span class="kw1">UPDATE</span> dbo.<span class="me1">Test1</span>
	<span class="kw1">SET</span>	col1 <span class="sy0">=</span> col1 <span class="sy0">*</span> 20
	<span class="kw1">WHERE</span> col1 <span class="sy0">=</span> <span class="nu0">3</span>;
<span class="kw1">COMMIT</span> <span class="kw1">TRANSACTION</span>;
&nbsp;
<span class="coMULTI">/* back to the first query window*/</span>
<span class="kw1">UPDATE</span> dbo.<span class="me1">Test2</span>
	<span class="kw1">SET</span>	col2 <span class="sy0">=</span> col2 <span class="sy0">*</span> 10
	<span class="kw1">WHERE</span> col2 <span class="sy0">=</span> <span class="nu0">4</span>;
<span class="kw1">COMMIT</span> <span class="kw1">TRANSACTION</span>;</pre></div></div></p>
<p>Looking at the code, we can see there are only five rows in each of the tables. In addition, an update will be performed to both col1 and col2 in each table for one specific row. So we are keeping this to a singleton type of update, and we are able to force a deadlock by using this setup. Not only do we see that a deadlock will occur consistently, we would see the following in the sys.dm_tran_locks DMV as well as in the deadlock graphs.</p>
<p><img decoding="async" src="https://jasonbrimhall.info/wp-content/uploads/2019/03/locks_held1-1024x521.png" /></p>
<p>In my environment, I used session 51 and 54 consistently for the deadlock repro. In each of the tests, each spid did obtain page locks as well as RID locks (row locks) that were waiting on each other in order to perform an Update. This is what we saw when I ran the setup without the ROWLOCK directive. What if I used the ROWLOCK directive (are you guessing already that there would be no change because the deadlock occurs on the lock held for the update that is waiting on the other update to complete?)? Let’s take a look at that too!</p>
<p><div id="wpshdo_14" class="wp-synhighlighter-outer"><div id="wpshdt_14" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_14"></a><a id="wpshat_14" class="wp-synhighlighter-title" href="#codesyntax_14"  onClick="javascript:wpsh_toggleBlock(14)" title="Click to show/hide code block">Source code</a></td><td align="right"><a href="#codesyntax_14" onClick="javascript:wpsh_code(14)" title="Show code only"><img decoding="async" border="0" style="border: 0 none" src="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_14" onClick="javascript:wpsh_print(14)" title="Print code"><img decoding="async" border="0" style="border: 0 none" src="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img decoding="async" border="0" style="border: 0 none" src="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_14" class="wp-synhighlighter-inner" style="display: block;"><pre class="tsql" style="font-family:monospace;"><span class="coMULTI">/* what about using rowlock hint? let's try it */</span>
<span class="kw1">USE</span> tempdb;
GO
<span class="kw1">IF</span> <span class="kw2">OBJECT_ID</span><span class="br0">&#40;</span><span class="st0">'dbo.Test1'</span><span class="br0">&#41;</span> <span class="kw1">IS</span> <span class="sy0">NOT</span> <span class="sy0">NULL</span>
	<span class="kw1">DROP</span> <span class="kw1">TABLE</span> dbo.<span class="me1">Test1</span>;
<span class="kw1">IF</span> <span class="kw2">OBJECT_ID</span><span class="br0">&#40;</span><span class="st0">'dbo.Test2'</span><span class="br0">&#41;</span> <span class="kw1">IS</span> <span class="sy0">NOT</span> <span class="sy0">NULL</span>
	<span class="kw1">DROP</span> <span class="kw1">TABLE</span> dbo.<span class="me1">Test2</span>;
<span class="kw1">CREATE</span> <span class="kw1">TABLE</span> dbo.<span class="me1">Test1</span> <span class="br0">&#40;</span> col1 <span class="kw1">INT</span> <span class="br0">&#41;</span>;
<span class="kw1">CREATE</span> <span class="kw1">TABLE</span> dbo.<span class="me1">Test2</span> <span class="br0">&#40;</span> col2 <span class="kw1">INT</span> <span class="br0">&#41;</span>;
<span class="kw1">INSERT</span> <span class="kw1">INTO</span> dbo.<span class="me1">Test1</span>
	<span class="kw1">VALUES</span>	<span class="br0">&#40;</span> 1 <span class="br0">&#41;</span>,
			<span class="br0">&#40;</span> 2 <span class="br0">&#41;</span>,
			<span class="br0">&#40;</span> 3 <span class="br0">&#41;</span>,
			<span class="br0">&#40;</span> 4 <span class="br0">&#41;</span>,
			<span class="br0">&#40;</span> 5 <span class="br0">&#41;</span>;
<span class="kw1">INSERT</span> <span class="kw1">INTO</span> dbo.<span class="me1">Test2</span>
	<span class="kw1">VALUES</span>	<span class="br0">&#40;</span> 1 <span class="br0">&#41;</span>,
			<span class="br0">&#40;</span> 2 <span class="br0">&#41;</span>,
			<span class="br0">&#40;</span> 3 <span class="br0">&#41;</span>,
			<span class="br0">&#40;</span> 4 <span class="br0">&#41;</span>,
			<span class="br0">&#40;</span> 5 <span class="br0">&#41;</span>;
GO
<span class="kw1">BEGIN</span> <span class="kw1">TRANSACTION</span>;
<span class="kw1">UPDATE</span> dbo.<span class="me1">Test1</span> <span class="kw1">WITH</span> <span class="br0">&#40;</span>ROWLOCK<span class="br0">&#41;</span>
	<span class="kw1">SET</span>	col1 <span class="sy0">=</span> col1 <span class="sy0">*</span> 10
	<span class="kw1">WHERE</span> col1 <span class="sy0">=</span> <span class="nu0">3</span>;
&nbsp;
<span class="coMULTI">/* query window 2*/</span>
&nbsp;
<span class="kw1">USE</span> tempdb;
<span class="kw1">BEGIN</span> <span class="kw1">TRANSACTION</span>;
<span class="kw1">UPDATE</span> dbo.<span class="me1">Test2</span> <span class="kw1">WITH</span> <span class="br0">&#40;</span>ROWLOCK<span class="br0">&#41;</span>
	<span class="kw1">SET</span>	col2 <span class="sy0">=</span> col2 <span class="sy0">*</span> 20
	<span class="kw1">WHERE</span> col2 <span class="sy0">=</span> <span class="nu0">4</span>;
<span class="kw1">UPDATE</span> dbo.<span class="me1">Test1</span> <span class="kw1">WITH</span> <span class="br0">&#40;</span>ROWLOCK<span class="br0">&#41;</span>
	<span class="kw1">SET</span>	col1 <span class="sy0">=</span> col1 <span class="sy0">*</span> 20
	<span class="kw1">WHERE</span> col1 <span class="sy0">=</span> <span class="nu0">3</span>;
<span class="kw1">COMMIT</span> <span class="kw1">TRANSACTION</span>;
&nbsp;
<span class="coMULTI">/* back to the first query window*/</span>
<span class="kw1">UPDATE</span> dbo.<span class="me1">Test2</span> <span class="kw1">WITH</span> <span class="br0">&#40;</span>ROWLOCK<span class="br0">&#41;</span>
	<span class="kw1">SET</span>	col2 <span class="sy0">=</span> col2 <span class="sy0">*</span> 10
	<span class="kw1">WHERE</span> col2 <span class="sy0">=</span> <span class="nu0">4</span>;
<span class="kw1">COMMIT</span> <span class="kw1">TRANSACTION</span>;</pre></div></div></p>
<p>The only change to this setup is that the ROWLOCK directive has been added to the update statements. Examining the sys.dm_tran_locks DMV reveals the same locks being held as was seen without the directive. This shouldn’t be too big of a surprise since the updates are against a single row.</p>
<p>In addition to the same locks being held, we continue to experience the same deadlock problem. Using an Extended Events session to trap deadlock information (similar to the article previously discussed), we can pull out some pretty good info. Let’s examine some of the deadlock data trapped by an XE session.</p>
<p><div id="wpshdo_15" class="wp-synhighlighter-outer"><div id="wpshdt_15" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_15"></a><a id="wpshat_15" class="wp-synhighlighter-title" href="#codesyntax_15"  onClick="javascript:wpsh_toggleBlock(15)" title="Click to show/hide code block">Source code</a></td><td align="right"><a href="#codesyntax_15" onClick="javascript:wpsh_code(15)" title="Show code only"><img decoding="async" border="0" style="border: 0 none" src="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_15" onClick="javascript:wpsh_print(15)" title="Print code"><img decoding="async" border="0" style="border: 0 none" src="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img decoding="async" border="0" style="border: 0 none" src="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_15" class="wp-synhighlighter-inner" style="display: block;"><pre class="tsql" style="font-family:monospace;"><span class="kw1">WITH</span>	cte	<span class="kw1">AS</span> <span class="br0">&#40;</span> <span class="kw1">SELECT</span> t2.<span class="me1">event_data</span>.<span class="kw1">VALUE</span><span class="br0">&#40;</span><span class="st0">'(event/@name)[1]'</span>, <span class="st0">'varchar(50)'</span><span class="br0">&#41;</span> <span class="kw1">AS</span> event_name
							, t2.<span class="me1">event_data</span>.<span class="kw1">VALUE</span><span class="br0">&#40;</span><span class="st0">'(event/@timestamp)[1]'</span>,
													<span class="st0">'datetime2'</span><span class="br0">&#41;</span> <span class="kw1">AS</span> StartTime
							, t2.<span class="me1">event_data</span>.<span class="kw1">VALUE</span><span class="br0">&#40;</span><span class="st0">'(event/data[@name=&quot;duration&quot;]/value)[1]'</span>,
													<span class="st0">'bigint'</span><span class="br0">&#41;</span> <span class="kw1">AS</span> duration
							, t2.<span class="me1">event_data</span>.<span class="kw1">VALUE</span><span class="br0">&#40;</span><span class="st0">'(event/data[@name=&quot;database_name&quot;]/value)[1]'</span>,
													<span class="st0">'sysname'</span><span class="br0">&#41;</span> <span class="kw1">AS</span> DBName
							, t2.<span class="me1">event_data</span>.<span class="kw1">VALUE</span><span class="br0">&#40;</span><span class="st0">'(event/action[@name=&quot;sql_text&quot;]/value)[1]'</span>,
													<span class="st0">'varchar(max)'</span><span class="br0">&#41;</span> <span class="kw1">AS</span> sql_text
							, t2.<span class="me1">event_data</span>.<span class="kw1">VALUE</span><span class="br0">&#40;</span><span class="st0">'(event/action[@name=&quot;nt_username&quot;]/value)[1]'</span>,
													<span class="st0">'varchar(500)'</span><span class="br0">&#41;</span> <span class="kw1">AS</span> nt_username
							, t2.<span class="me1">event_data</span>.<span class="kw1">VALUE</span><span class="br0">&#40;</span><span class="st0">'(event/data[@name=&quot;mode&quot;]/value)[1]'</span>,
													<span class="st0">'varchar(15)'</span><span class="br0">&#41;</span> <span class="sy0">+</span> <span class="st0">' ('</span>
							<span class="sy0">+</span> t2.<span class="me1">event_data</span>.<span class="kw1">VALUE</span><span class="br0">&#40;</span><span class="st0">'(event/data[@name=&quot;mode&quot;]/text)[1]'</span>,
													<span class="st0">'varchar(50)'</span><span class="br0">&#41;</span> <span class="sy0">+</span> <span class="st0">')'</span> <span class="kw1">AS</span> mode
							, t2.<span class="me1">event_data</span>.<span class="kw1">VALUE</span><span class="br0">&#40;</span><span class="st0">'(event/data[@name=&quot;object_id&quot;]/value)[1]'</span>,
													<span class="st0">'integer'</span><span class="br0">&#41;</span> <span class="kw1">AS</span> <span class="kw2">OBJECT_ID</span>
							, t2.<span class="me1">event_data</span>.<span class="kw1">VALUE</span><span class="br0">&#40;</span><span class="st0">'(event/data[@name=&quot;resource_description&quot;]/value)[1]'</span>,
													<span class="st0">'varchar(max)'</span><span class="br0">&#41;</span> <span class="kw1">AS</span> resource_description
							, t2.<span class="me1">event_data</span>.<span class="kw1">VALUE</span><span class="br0">&#40;</span><span class="st0">'(event/data[@name=&quot;resource_owner_type&quot;]/text)[1]'</span>,
													<span class="st0">'varchar(max)'</span><span class="br0">&#41;</span> <span class="kw1">AS</span> resource_owner_type
							, t2.<span class="me1">event_data</span>.<span class="kw1">VALUE</span><span class="br0">&#40;</span><span class="st0">'(event/data[@name=&quot;resource_type&quot;]/text)[1]'</span>,
													<span class="st0">'varchar(max)'</span><span class="br0">&#41;</span> <span class="sy0">+</span> <span class="st0">' ('</span>
							<span class="sy0">+</span> t2.<span class="me1">event_data</span>.<span class="kw1">VALUE</span><span class="br0">&#40;</span><span class="st0">'(event/data[@name=&quot;resource_type&quot;]/value)[1]'</span>,
													<span class="st0">'varchar(max)'</span><span class="br0">&#41;</span> <span class="sy0">+</span> <span class="st0">')'</span> <span class="kw1">AS</span> resource_type
							, t2.<span class="me1">event_data</span>.<span class="kw1">VALUE</span><span class="br0">&#40;</span><span class="st0">'(event/action[@name=&quot;server_principal_name&quot;]/value)[1]'</span>,
													<span class="st0">'varchar(max)'</span><span class="br0">&#41;</span> <span class="kw1">AS</span> server_principal_name
							, t2.<span class="me1">event_data</span>.<span class="kw1">VALUE</span><span class="br0">&#40;</span><span class="st0">'(event/action[@name=&quot;session_id&quot;]/value)[1]'</span>,
													<span class="st0">'varchar(max)'</span><span class="br0">&#41;</span> <span class="kw1">AS</span> session_id
							, t2.<span class="me1">event_data</span>.<span class="kw1">VALUE</span><span class="br0">&#40;</span><span class="st0">'(event/action[@name=&quot;client_pid&quot;]/value)[1]'</span>,
													<span class="st0">'integer'</span><span class="br0">&#41;</span> <span class="kw1">AS</span> client_pid
							, t2.<span class="me1">event_data</span>.<span class="kw1">VALUE</span><span class="br0">&#40;</span><span class="st0">'(event/action[@name=&quot;client_app_name&quot;]/value)[1]'</span>,
													<span class="st0">'varchar(max)'</span><span class="br0">&#41;</span> <span class="kw1">AS</span> client_app_name
							, t2.<span class="me1">event_data</span>
							,ROW_NUMBER<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="kw1">OVER</span> <span class="br0">&#40;</span><span class="kw1">ORDER</span> <span class="kw1">BY</span> t2.<span class="me1">event_data</span>.<span class="kw1">VALUE</span><span class="br0">&#40;</span><span class="st0">'(event/@timestamp)[1]'</span>,
													<span class="st0">'datetime2'</span><span class="br0">&#41;</span>
													,t2.<span class="me1">event_data</span>.<span class="kw1">VALUE</span><span class="br0">&#40;</span><span class="st0">'(event/@name)[1]'</span>, <span class="st0">'varchar(50)'</span><span class="br0">&#41;</span><span class="br0">&#41;</span> <span class="kw1">AS</span> Event_Seq
						<span class="kw1">FROM</span> sys.<span class="me1">fn_xe_file_target_read_file</span><span class="br0">&#40;</span><span class="st0">'C:<span class="es0">\D</span>atabase<span class="es0">\X</span>E<span class="es0">\D</span>eadlock*.xel'</span>,
																<span class="sy0">NULL</span>, <span class="sy0">NULL</span>, <span class="sy0">NULL</span><span class="br0">&#41;</span> t1
							<span class="sy0">CROSS</span> APPLY <span class="br0">&#40;</span> <span class="kw1">SELECT</span> <span class="kw1">CONVERT</span><span class="br0">&#40;</span>XML, t1.<span class="me1">event_data</span><span class="br0">&#41;</span>
										<span class="br0">&#41;</span> t2 <span class="br0">&#40;</span> event_data <span class="br0">&#41;</span>
				<span class="br0">&#41;</span>
	<span class="kw1">SELECT</span> c1.<span class="me1">event_name</span> <span class="kw1">AS</span> LockEvent, c2.<span class="me1">event_name</span> <span class="kw1">AS</span> GraphEvent
			, c1.<span class="me1">StartTime</span>
			, <span class="kw2">DATEADD</span><span class="br0">&#40;</span>MICROSECOND, c1.<span class="me1">duration</span>, <span class="kw1">CONVERT</span><span class="br0">&#40;</span>DATETIME2, <span class="br0">[</span>c1<span class="br0">]</span>.<span class="me1">StartTime</span><span class="br0">&#41;</span><span class="br0">&#41;</span> <span class="kw1">AS</span> EndDate
			, c1.<span class="me1">duration</span>
			, c1.<span class="me1">DBName</span>
			, c1.<span class="me1">sql_text</span>
			, c1.<span class="me1">nt_username</span>
			, c1.<span class="me1">server_principal_name</span>
			, c1.<span class="me1">mode</span>
			, c1.<span class="kw2">OBJECT_ID</span>
			, c1.<span class="me1">resource_description</span>
			, c1.<span class="me1">resource_owner_type</span>
			, c1.<span class="me1">resource_type</span>
			, c1.<span class="me1">session_id</span>
			, c1.<span class="me1">client_pid</span>
			, c1.<span class="me1">client_app_name</span>
			, c1.<span class="me1">event_data</span>
			, graph.<span class="me1">query</span><span class="br0">&#40;</span><span class="st0">'.'</span><span class="br0">&#41;</span> <span class="kw1">AS</span> EventDeadlockGraph
		<span class="kw1">FROM</span> cte c1
			<span class="kw1">INNER</span> <span class="sy0">JOIN</span> cte c2
				<span class="kw1">ON</span> c1.<span class="me1">Event_Seq</span> <span class="sy0">=</span> c2.<span class="me1">Event_Seq</span> <span class="sy0">-</span> <span class="nu0">1</span>
			<span class="sy0">CROSS</span> APPLY c2.<span class="me1">event_data</span>.<span class="me1">nodes</span><span class="br0">&#40;</span><span class="st0">'(event/data[@name=&quot;xml_report&quot;]/value)[last()]/*'</span><span class="br0">&#41;</span>
				<span class="kw1">AS</span> deadlock <span class="br0">&#40;</span> graph <span class="br0">&#41;</span>
		<span class="kw1">WHERE</span> c1.<span class="me1">event_name</span> <span class="sy0">=</span> <span class="st0">'lock_deadlock'</span>
			<span class="sy0">AND</span> c2.<span class="me1">event_name</span> <span class="sy0">=</span> <span class="st0">'xml_deadlock_report'</span>
		<span class="kw1">ORDER</span> <span class="kw1">BY</span> c1.<span class="me1">StartTime</span>;</pre></div></div></p>
<p>The results from this query will show us the deadlock graph, the event data, as well as several other pieces of data already parsed from the session data for you. And looking at the session data, one can see that the sql_text from each of the queries will demonstrate both the ROWLOCK directive and the directive-free versions of the query. In this query you can also see that I did a little black magic to match up the two event types from the event session (lock_deadlock and xml_deadlock_report). Then I was able to join the two together to produce one row per deadlock event and to see the sql_text with the deadlock graph on one row. Otherwise, the sql_text does not produce with the deadlock_report event. I leave the rest of the query to the reader to discover and question.</p>
<p><img decoding="async" src="https://jasonbrimhall.info/wp-content/uploads/2019/03/xe_results-1024x141.png" /></p>
<p>From the EventDeadlockGraph column, we could click the cell and take a close look at the XML generated for the deadlock event. Further, if I choose to save the xml as an XDL file and then reopen it in SSMS, I can see the deadlock graphical report as shown in the following.</p>
<p><img decoding="async" src="https://jasonbrimhall.info/wp-content/uploads/2019/03/graphical_report-1024x348.png" /></p>
<p>We see that row locks are still in effect for the update coming from both sides. This further supports that the directive really is just a waste of time in trying to combat this type of deadlock. This is one of those cases where the best option would be to optimize the code and work things out without trying to take a shortcut.</p>
<h3>Wrapping Up</h3>
<p>Look to optimize the code instead of trying to take a shortcut. In addition, take a look at the deadlocks, the locks held, and the code to get a better understanding of what is truly happening.</p>
<p>The post <a href="https://sqlsolutionsgroup.com/an-experiment-with-deadlocks/">An Experiment with Deadlocks</a> appeared first on <a href="https://sqlsolutionsgroup.com">SQL Solutions Group</a>.</p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>Upgrading From SQL Server Profiler</title>
		<link>https://sqlsolutionsgroup.com/upgrading-from-sql-server-profiler/</link>
		
		<dc:creator><![CDATA[Jason Brimhall]]></dc:creator>
		<pubDate>Wed, 22 Apr 2015 14:38:46 +0000</pubDate>
				<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Group]]></category>
		<category><![CDATA[#microsftcertifedmaster]]></category>
		<category><![CDATA[#microsftpartner]]></category>
		<category><![CDATA[#SQLAB]]></category>
		<category><![CDATA[#SQlatino]]></category>
		<category><![CDATA[#SQlatinoamerica]]></category>
		<category><![CDATA[#sqldatabase]]></category>
		<category><![CDATA[#sqldeveloper]]></category>
		<category><![CDATA[#SQLgroupie]]></category>
		<category><![CDATA[#sqlimer]]></category>
		<category><![CDATA[#sqlimerbymay]]></category>
		<category><![CDATA[#sqlinjection]]></category>
		<category><![CDATA[#sqlinternals]]></category>
		<category><![CDATA[#sqlite]]></category>
		<category><![CDATA[#sqlite3]]></category>
		<category><![CDATA[#SQLLearning]]></category>
		<category><![CDATA[#SQLMagazine]]></category>
		<category><![CDATA[#sqlmanagementstudio]]></category>
		<category><![CDATA[#sqlmanager]]></category>
		<category><![CDATA[#Sqlmap]]></category>
		<category><![CDATA[#sqlrun]]></category>
		<category><![CDATA[#sqlsaturday2017]]></category>
		<category><![CDATA[#sqlsatvienna]]></category>
		<category><![CDATA[#sqlserver]]></category>
		<category><![CDATA[#SQLserver2012]]></category>
		<category><![CDATA[#sqlserver2014]]></category>
		<category><![CDATA[#sqlserver2017]]></category>
		<category><![CDATA[#sqlserver2022]]></category>
		<category><![CDATA[#SQLServeronLinux]]></category>
		<category><![CDATA[#SQLsolutionsgroup]]></category>
		<category><![CDATA[#SQLTraining]]></category>
		<category><![CDATA[#SQLYog]]></category>
		<category><![CDATA[Extended Events]]></category>
		<category><![CDATA[SQL Administration]]></category>
		<category><![CDATA[SQL Internals]]></category>
		<category><![CDATA[SQLPASS]]></category>
		<category><![CDATA[SQLSaturday]]></category>
		<category><![CDATA[SSG]]></category>
		<category><![CDATA[XE]]></category>
		<guid isPermaLink="false">http://sqlsolutionsgroup.com/?p=2223</guid>

					<description><![CDATA[<p>You may recall an article I had written a couple months back describing how to use Extended Events to perform your SQL Server Profiler duties. If not, you can read all about it here. In that article, I showed one common use-case for SQL Server Profiler and how to achieve the same result via Extended [&#8230;]</p>
<p>The post <a href="https://sqlsolutionsgroup.com/upgrading-from-sql-server-profiler/">Upgrading From SQL Server Profiler</a> appeared first on <a href="https://sqlsolutionsgroup.com">SQL Solutions Group</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>You may recall an article I had written a couple months back describing how to use <a href="https://bit.ly/2ELiQaf">Extended Events to perform your SQL Server Profiler duties</a>. If not, you can read all about it <a href="https://bit.ly/2ELiQaf">here</a>.</p>
<p><img decoding="async" class="alignnone" src="https://jasonbrimhall.info/wp-content/uploads/2018/03/xe_flysuperhero-1024x1024.png" alt="sql server profiler" width="350" /></p>
<p>In that article, I showed one common use-case for SQL Server Profiler and how to achieve the same result via Extended Events. What I didn&#8217;t show in that article was how to correlate all of your favorite trace/profiler options into the shiny new XE equivalents. Is there even a way to do that?</p>
<p>As luck would have it, there is a means to correlate trace/profiler events to Extended Event events (yeah that sounds weird).  With the release of SQL Server 2012, Microsoft introduced a couple of catalog views to help with this correlation effort. Those views have a pretty straightforward naming convention. For example, one of the views is named sys.trace_xe_event_map while the other is sys.trace_xe_action_map. For this article, I will be focusing on the former.</p>
<h3>Making the Upgrade</h3>
<p>When looking to finally make the upgrade away from trace/profiler, a big key is to figure out if your favorite trace events are even an option at the next level. I have talked about <a href="https://bit.ly/SSGTrifecta">finding events in XE</a> in previous articles by trying to search for a specific topic that might apply to your current condition.</p>
<p>But if you are already familiar with specific trace events, you may just want/need to know what the new name is in XE. This is where those catalog views come into play. And in support of that, here is a query that can help in that upgrade effort:</p>
<p><div id="wpshdo_16" class="wp-synhighlighter-outer"><div id="wpshdt_16" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_16"></a><a id="wpshat_16" class="wp-synhighlighter-title" href="#codesyntax_16"  onClick="javascript:wpsh_toggleBlock(16)" title="Click to show/hide code block">Source code</a></td><td align="right"><a href="#codesyntax_16" onClick="javascript:wpsh_code(16)" title="Show code only"><img decoding="async" border="0" style="border: 0 none" src="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_16" onClick="javascript:wpsh_print(16)" title="Print code"><img decoding="async" border="0" style="border: 0 none" src="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img decoding="async" border="0" style="border: 0 none" src="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_16" class="wp-synhighlighter-inner" style="display: block;"><pre class="tsql" style="font-family:monospace;"><span class="kw1">SELECT</span> te.<span class="me1">trace_event_id</span>,tc.<span class="me1">name</span> <span class="kw1">AS</span> CategoryName
	, <span class="kw1">CASE</span> tc.<span class="me1">type</span>
		<span class="kw1">WHEN</span> 0 <span class="kw1">THEN</span> <span class="st0">'Normal'</span>
		<span class="kw1">WHEN</span> 1 <span class="kw1">THEN</span> <span class="st0">'Connection'</span>
		<span class="kw1">WHEN</span> 2 <span class="kw1">THEN</span> <span class="st0">'ERROR'</span>
		<span class="kw1">END</span> <span class="kw1">AS</span> TraceEventType
	,te.<span class="me1">name</span> <span class="kw1">AS</span> TraceEventName
	, xem.<span class="me1">package_name</span>, xem.<span class="me1">xe_event_name</span>
<span class="kw1">FROM</span> sys.<span class="me1">trace_xe_event_map</span> xem
	<span class="kw1">RIGHT</span> <span class="sy0">OUTER</span> <span class="sy0">JOIN</span> sys.<span class="me1">trace_events</span> te
		<span class="kw1">ON</span> te.<span class="me1">trace_event_id</span> <span class="sy0">=</span> xem.<span class="me1">trace_event_id</span>
	<span class="kw1">INNER</span> <span class="sy0">JOIN</span> sys.<span class="me1">trace_categories</span> tc
		<span class="kw1">ON</span> te.<span class="me1">category_id</span> <span class="sy0">=</span> tc.<span class="me1">category_id</span>
<span class="kw1">ORDER</span> <span class="kw1">BY</span> tc.<span class="me1">category_id</span>,te.<span class="me1">trace_event_id</span> <span class="kw1">ASC</span>;</pre></div></div></p>
<p>Looking through the results, one would notice that not every trace event maps to an Extended Event event. The events that don’t map, in general, deal with audits (which is actually driven by Extended Events).</p>
<p>This is a really good start to getting you on your way to that much needed upgrade away from profiler/trace. What if there are several traces that are already in use or scripted for various reasons in the environment?</p>
<h3>Decision Time</h3>
<p>If you happen to have traces that are already deployed in the environment or in script form, it can be a bit of a tedious pain to convert those to Extended Events. Do you manually recreate the traces as XE sessions? Do you abandon the upgrade due to the effort and annoyance it will create? Or do you find some automated means of performing the conversion?</p>
<p>Of those three options, only two are valid options. Those options involve performing the conversion of the traces to XE sessions. There are pros and cons for each. You may opt to take your time and learn more about Extended Events by performing the manual upgrade, or you may choose to save time by using an automated routine.</p>
<p>Should you decide to try the automated routine, there is one already out and available to help you on your way. Jonathan Kehayias wrote the script and you can download it <a href="https://www.sqlskills.com/blogs/jonathan/converting-sql-trace-to-extended-events-in-sql-server-2012/">here</a>.</p>
<p>The post <a href="https://sqlsolutionsgroup.com/upgrading-from-sql-server-profiler/">Upgrading From SQL Server Profiler</a> appeared first on <a href="https://sqlsolutionsgroup.com">SQL Solutions Group</a>.</p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>Where did the Database Go?</title>
		<link>https://sqlsolutionsgroup.com/missing-database/</link>
		
		<dc:creator><![CDATA[Jason Brimhall]]></dc:creator>
		<pubDate>Tue, 07 Apr 2015 12:34:32 +0000</pubDate>
				<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Group]]></category>
		<category><![CDATA[#microsftcertifedmaster]]></category>
		<category><![CDATA[#microsftpartner]]></category>
		<category><![CDATA[#SQLAB]]></category>
		<category><![CDATA[#SQlatino]]></category>
		<category><![CDATA[#SQlatinoamerica]]></category>
		<category><![CDATA[#sqldatabase]]></category>
		<category><![CDATA[#sqldeveloper]]></category>
		<category><![CDATA[#SQLgroupie]]></category>
		<category><![CDATA[#sqlimer]]></category>
		<category><![CDATA[#sqlimerbymay]]></category>
		<category><![CDATA[#sqlinjection]]></category>
		<category><![CDATA[#sqlinternals]]></category>
		<category><![CDATA[#sqlite]]></category>
		<category><![CDATA[#sqlite3]]></category>
		<category><![CDATA[#SQLLearning]]></category>
		<category><![CDATA[#SQLMagazine]]></category>
		<category><![CDATA[#sqlmanagementstudio]]></category>
		<category><![CDATA[#sqlmanager]]></category>
		<category><![CDATA[#Sqlmap]]></category>
		<category><![CDATA[#sqlrun]]></category>
		<category><![CDATA[#sqlsaturday2017]]></category>
		<category><![CDATA[#sqlsatvienna]]></category>
		<category><![CDATA[#sqlserver]]></category>
		<category><![CDATA[#SQLserver2012]]></category>
		<category><![CDATA[#sqlserver2014]]></category>
		<category><![CDATA[#sqlserver2017]]></category>
		<category><![CDATA[#sqlserver2022]]></category>
		<category><![CDATA[#SQLServeronLinux]]></category>
		<category><![CDATA[#SQLsolutionsgroup]]></category>
		<category><![CDATA[#SQLTraining]]></category>
		<category><![CDATA[#SQLYog]]></category>
		<category><![CDATA[Audit]]></category>
		<category><![CDATA[Extended Events]]></category>
		<category><![CDATA[SQL Internals]]></category>
		<category><![CDATA[SQLPASS]]></category>
		<category><![CDATA[SQLSaturday]]></category>
		<category><![CDATA[SSG]]></category>
		<category><![CDATA[XE]]></category>
		<guid isPermaLink="false">http://sqlsolutionsgroup.com/?p=2206</guid>

					<description><![CDATA[<p>What do you do when a developer comes to you and asks, &#8220;Where did the database go?  The database was there one minute, and the next it was not.&#8221;  Only one thing could be worse than the feeling of losing a database on your watch, and that would be losing a production database. It&#8217;s like [&#8230;]</p>
<p>The post <a href="https://sqlsolutionsgroup.com/missing-database/">Where did the Database Go?</a> appeared first on <a href="https://sqlsolutionsgroup.com">SQL Solutions Group</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>What do you do when a developer comes to you and asks, &#8220;Where did the database go?  The database was there one minute, and the next it was not.&#8221;  Only one thing could be worse than the feeling of losing a database on your watch, and that would be losing a production database. It&#8217;s like magic—it&#8217;s there, and then it disappears. To compound the issue, when asking people if they know what might have happened, all will typically deny, deny, deny.</p>
<p>What do you do when you run into that missing database situation and the inevitable denial that will ensue?  This is when an audit can save the day.  Through an audit, you can discover who dropped the database and when it happened.  Then you have hard data to take back to the team to again ask what happened.  Taking the info from a <a title="Audit Schema Change" href="https://bit.ly/1wtDgeY">previous article of mine</a>, we can alter the script I published there and re-use it for our needs here.</p>
<p><div id="wpshdo_17" class="wp-synhighlighter-outer"><div id="wpshdt_17" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_17"></a><a id="wpshat_17" class="wp-synhighlighter-title" href="#codesyntax_17"  onClick="javascript:wpsh_toggleBlock(17)" title="Click to show/hide code block">Source code</a></td><td align="right"><a href="#codesyntax_17" onClick="javascript:wpsh_code(17)" title="Show code only"><img decoding="async" border="0" style="border: 0 none" src="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_17" onClick="javascript:wpsh_print(17)" title="Print code"><img decoding="async" border="0" style="border: 0 none" src="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img decoding="async" border="0" style="border: 0 none" src="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_17" class="wp-synhighlighter-inner" style="display: block;"><pre class="tsql" style="font-family:monospace;"><span class="kw1">DECLARE</span> @DBName sysname <span class="sy0">=</span> <span class="st0">'AdventureWorks2014'</span>
		,@d1 <span class="kw1">DATETIME</span>
		,@diff <span class="kw1">INT</span>;
&nbsp;
<span class="kw1">SELECT</span> ObjectName
		  , ObjectID
		  , DatabaseName
		  , StartTime
		  , EventClass
		  , EventSubClass
		  , ObjectType
		  , ServerName
		  , LoginName
		  , NTUserName
		  , ApplicationName
		  , <span class="kw1">CASE</span> EventClass
				<span class="kw1">WHEN</span> 46
					<span class="kw1">THEN</span> <span class="st0">'CREATE'</span>
				<span class="kw1">WHEN</span> 47
					<span class="kw1">THEN</span> <span class="st0">'DROP'</span>
				<span class="co1">--WHEN 164</span>
				<span class="co1">--	THEN 'ALTER'</span>
			<span class="kw1">END</span> <span class="kw1">AS</span> DDLOperation
		<span class="kw1">INTO</span> #temp_trace  
	<span class="kw1">FROM</span> sys.<span class="kw4">FN_TRACE_GETTABLE</span><span class="br0">&#40;</span><span class="kw1">CONVERT</span><span class="br0">&#40;</span><span class="kw1">VARCHAR</span><span class="br0">&#40;</span>150<span class="br0">&#41;</span>, 
			<span class="br0">&#40;</span> <span class="kw1">SELECT</span> <span class="kw2">REVERSE</span><span class="br0">&#40;</span><span class="kw2">SUBSTRING</span><span class="br0">&#40;</span><span class="kw2">REVERSE</span><span class="br0">&#40;</span><span class="kw1">PATH</span><span class="br0">&#41;</span>,
					<span class="kw2">CHARINDEX</span><span class="br0">&#40;</span><span class="st0">'<span class="es0">\'</span>,REVERSE(path)),256)) + '</span><span class="kw2">LOG</span>.<span class="me1">trc</span><span class="st0">'
				FROM    sys.traces
				WHERE   is_default = 1)), DEFAULT) T  
  WHERE EventClass in (46,47) 
			AND EventSubclass = 0
			AND ObjectType = 16964-- i just want database related events   
			AND DatabaseName = ISNULL(@DBName,DatabaseName);
&nbsp;
&nbsp;
SELECT @d1 = MIN(StartTime) 
	FROM #temp_trace;
&nbsp;
SET @diff= DATEDIFF(hh,@d1,GETDATE());
&nbsp;
&nbsp;
SELECT @diff AS HrsSinceFirstChange    
		, @d1 AS FirstChangeDate    
		, sv.name AS obj_type_desc
		, tt.ObjectType
		, tt.DDLOperation
		, tt.DatabaseName,tt.ObjectName,tt.StartTime
		, tt.EventClass,tt.EventSubClass
		, tt.ServerName,tt.LoginName, tt.NTUserName
		, tt.ApplicationName
		, (dense_rank() OVER (ORDER BY ObjectName,ObjectType ) )%2 AS l1     
		, (dense_rank() OVER (ORDER BY ObjectName,ObjectType,StartTime ))%2 AS l2    
	FROM #temp_trace tt
		INNER JOIN sys.trace_events AS te 
			ON tt.EventClass = te.trace_event_id
		INNER JOIN sys.trace_subclass_values tsv
			ON tt.EventClass = tsv.trace_event_id
			AND tt.ObjectType = tsv.subclass_value
		INNER JOIN master.dbo.spt_values sv 
			ON tsv.subclass_value = sv.number
			AND sv.type = '</span>EOD<span class="st0">'
	ORDER BY StartTime DESC;
&nbsp;
DROP TABLE #temp_trace;</span></pre></div></div></p>
<p>This script will now query the default trace to determine when a database was dropped or created.  I am limiting this result set through the use of this filter: ObjectType = 16964.  In addition to that, I have also trimmed the result-set down to just look for drop or create events.</p>
<p>This is the type of information that is already available within the default trace.  What if you wished to not be entirely dependent on the default trace for that information?  As luck would have it, you don’t need to be solely dependent on the default trace.  Instead you can use the robust tool called extended events.</p>
<p>The post <a href="https://sqlsolutionsgroup.com/missing-database/">Where did the Database Go?</a> appeared first on <a href="https://sqlsolutionsgroup.com">SQL Solutions Group</a>.</p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>How To: XEvents as Profiler</title>
		<link>https://sqlsolutionsgroup.com/xevents-as-profiler/</link>
					<comments>https://sqlsolutionsgroup.com/xevents-as-profiler/#comments</comments>
		
		<dc:creator><![CDATA[Jason Brimhall]]></dc:creator>
		<pubDate>Wed, 11 Mar 2015 06:37:22 +0000</pubDate>
				<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Group]]></category>
		<category><![CDATA[#microsftcertifedmaster]]></category>
		<category><![CDATA[#microsftpartner]]></category>
		<category><![CDATA[#SQLAB]]></category>
		<category><![CDATA[#SQlatino]]></category>
		<category><![CDATA[#SQlatinoamerica]]></category>
		<category><![CDATA[#sqldatabase]]></category>
		<category><![CDATA[#sqldeveloper]]></category>
		<category><![CDATA[#SQLgroupie]]></category>
		<category><![CDATA[#sqlimer]]></category>
		<category><![CDATA[#sqlimerbymay]]></category>
		<category><![CDATA[#sqlinjection]]></category>
		<category><![CDATA[#sqlinternals]]></category>
		<category><![CDATA[#sqlite]]></category>
		<category><![CDATA[#sqlite3]]></category>
		<category><![CDATA[#SQLLearning]]></category>
		<category><![CDATA[#SQLMagazine]]></category>
		<category><![CDATA[#sqlmanagementstudio]]></category>
		<category><![CDATA[#sqlmanager]]></category>
		<category><![CDATA[#Sqlmap]]></category>
		<category><![CDATA[#sqlrun]]></category>
		<category><![CDATA[#sqlsaturday2017]]></category>
		<category><![CDATA[#sqlsatvienna]]></category>
		<category><![CDATA[#sqlserver]]></category>
		<category><![CDATA[#SQLserver2012]]></category>
		<category><![CDATA[#sqlserver2014]]></category>
		<category><![CDATA[#sqlserver2017]]></category>
		<category><![CDATA[#sqlserver2022]]></category>
		<category><![CDATA[#SQLServeronLinux]]></category>
		<category><![CDATA[#SQLsolutionsgroup]]></category>
		<category><![CDATA[#SQLTraining]]></category>
		<category><![CDATA[#SQLYog]]></category>
		<category><![CDATA[Extended Events]]></category>
		<category><![CDATA[How To]]></category>
		<category><![CDATA[SQL Internals]]></category>
		<category><![CDATA[SQL Script]]></category>
		<category><![CDATA[SQLPASS]]></category>
		<category><![CDATA[SQLSaturday]]></category>
		<category><![CDATA[SSG]]></category>
		<category><![CDATA[TSQL]]></category>
		<guid isPermaLink="false">http://sqlsolutionsgroup.com/?p=2144</guid>

					<description><![CDATA[<p>A common excuse for not delving into extended events seems to be the jump from Profiler to extended events (aka XEvents).  There appears to be an inherent fear with how to use extended events.  In addition to that fear, is the fear that extended events does not do what Profiler can do so easily. Today, [&#8230;]</p>
<p>The post <a href="https://sqlsolutionsgroup.com/xevents-as-profiler/">How To: XEvents as Profiler</a> appeared first on <a href="https://sqlsolutionsgroup.com">SQL Solutions Group</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>A common excuse for not delving into extended events seems to be the jump from Profiler to extended events (aka XEvents).  There appears to be an inherent fear with how to use extended events.  In addition to that fear, is the fear that extended events does not do what Profiler can do so easily.</p>
<p>Today, I would like to share a short tutorial on how to use extended events as if it were a profiling session.  I am only going to show one of the many possible means to profile via extended events.  Just understand that this means you have flexibility and a way to do more with this simple example than what I am going to share.</p>
<h3>Requirement</h3>
<p>You need to capture T-SQL queries that are occurring against your instance in order to try and determine the code that is being passed to SQL Server from the application.</p>
<h3>XEvents How-To</h3>
<p>Rather than jump to Profiler, you are going to need to jump to a fresh, clean query window in SSMS.  Just like in Profiler, you will want to capture certain &#8220;statement&#8221; oriented events.</p>
<p>For this session, I want to start with sp_statement_starting and sql_statement_starting.  I am not going to use the _completed forms of those events because I want to capture as much as I can – without capturing too much noise.  Sometimes, there may be a _starting without a coordinated _completed and that could throw a wrench in the works.</p>
<p>With the basic information in hand, we are ready to implement some of the things learned in previous XEvents posts.  Let’s go ahead and start setting up the session that will capture the information we seek.</p>
<p><div id="wpshdo_18" class="wp-synhighlighter-outer"><div id="wpshdt_18" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_18"></a><a id="wpshat_18" class="wp-synhighlighter-title" href="#codesyntax_18"  onClick="javascript:wpsh_toggleBlock(18)" title="Click to show/hide code block">Source code</a></td><td align="right"><a href="#codesyntax_18" onClick="javascript:wpsh_code(18)" title="Show code only"><img decoding="async" border="0" style="border: 0 none" src="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_18" onClick="javascript:wpsh_print(18)" title="Print code"><img decoding="async" border="0" style="border: 0 none" src="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img decoding="async" border="0" style="border: 0 none" src="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_18" class="wp-synhighlighter-inner" style="display: block;"><pre class="tsql" style="font-family:monospace;"><span class="kw1">USE</span> master;
GO
<span class="co1">-- Create the Event Session</span>
<span class="kw1">IF</span> <span class="sy0">EXISTS</span><span class="br0">&#40;</span><span class="kw1">SELECT</span> <span class="sy0">*</span> 
          <span class="kw1">FROM</span> sys.<span class="me1">server_event_sessions</span> 
          <span class="kw1">WHERE</span> name<span class="sy0">=</span><span class="st0">'TraceIncomingQueries'</span><span class="br0">&#41;</span>
    <span class="kw1">DROP</span> EVENT <span class="kw1">SESSION</span> TraceIncomingQueries 
    <span class="kw1">ON</span> SERVER;
GO
<span class="kw1">CREATE</span> EVENT <span class="kw1">SESSION</span> TraceIncomingQueries
<span class="kw1">ON</span> SERVER
<span class="kw1">ADD</span> EVENT sqlserver.<span class="me1">sp_statement_starting</span> <span class="br0">&#40;</span>
	<span class="kw1">SET</span> collect_object_name<span class="sy0">=</span><span class="br0">&#40;</span>1<span class="br0">&#41;</span>
	<span class="kw1">ACTION</span><span class="br0">&#40;</span>sqlserver.<span class="me1">database_name</span>,sqlserver.<span class="me1">nt_username</span>,sqlserver.<span class="me1">session_id</span>,sqlserver.<span class="me1">client_hostname</span>,sqlserver.<span class="me1">client_app_name</span><span class="br0">&#41;</span>
<span class="kw1">WHERE</span> sqlserver.<span class="me1">client_app_name</span> <span class="sy0">&amp;</span>lt;<span class="sy0">&amp;</span>gt; <span class="st0">'Microsoft SQL Server Management Studio - Transact-SQL IntelliSense'</span>
	<span class="br0">&#41;</span>,
<span class="kw1">ADD</span> EVENT sqlserver.<span class="me1">sql_statement_starting</span><span class="br0">&#40;</span>
    <span class="kw1">ACTION</span><span class="br0">&#40;</span>sqlserver.<span class="me1">database_name</span>,sqlserver.<span class="me1">nt_username</span>,sqlserver.<span class="me1">session_id</span>,sqlserver.<span class="me1">client_hostname</span>,sqlserver.<span class="me1">client_app_name</span><span class="br0">&#41;</span>
<span class="kw1">WHERE</span> sqlserver.<span class="me1">database_name</span><span class="sy0">=</span><span class="st0">'AdventureWorks2014'</span>
	<span class="sy0">AND</span> sqlserver.<span class="me1">client_app_name</span> <span class="sy0">&amp;</span>lt;<span class="sy0">&amp;</span>gt; <span class="st0">'Microsoft SQL Server Management Studio - Transact-SQL IntelliSense'</span>
	<span class="br0">&#41;</span>
<span class="kw1">ADD</span> TARGET package0.<span class="me1">event_file</span><span class="br0">&#40;</span><span class="kw1">SET</span> filename<span class="sy0">=</span>N<span class="st0">'C:<span class="es0">\D</span>atabase<span class="es0">\X</span>E<span class="es0">\T</span>raceIncomingQueries.xel'</span><span class="br0">&#41;</span>
&nbsp;
<span class="coMULTI">/* start the session */</span>
<span class="kw1">ALTER</span> EVENT <span class="kw1">SESSION</span> TraceIncomingQueries 
<span class="kw1">ON</span> SERVER 
<span class="kw1">STATE</span> <span class="sy0">=</span> <span class="kw1">START</span>;
GO</pre></div></div></p>
<p>One caveat to consider is in the sp_statement_starting event, I have specified a specific set option be used.</p>
<p><div id="wpshdo_19" class="wp-synhighlighter-outer"><div id="wpshdt_19" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_19"></a><a id="wpshat_19" class="wp-synhighlighter-title" href="#codesyntax_19"  onClick="javascript:wpsh_toggleBlock(19)" title="Click to show/hide code block">Source code</a></td><td align="right"><a href="#codesyntax_19" onClick="javascript:wpsh_code(19)" title="Show code only"><img decoding="async" border="0" style="border: 0 none" src="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_19" onClick="javascript:wpsh_print(19)" title="Print code"><img decoding="async" border="0" style="border: 0 none" src="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img decoding="async" border="0" style="border: 0 none" src="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_19" class="wp-synhighlighter-inner" style="display: block;"><pre class="tsql" style="font-family:monospace;"><span class="kw1">SET</span> collect_object_name<span class="sy0">=</span><span class="br0">&#40;</span><span class="nu0">1</span><span class="br0">&#41;</span></pre></div></div></p>
<p>This particular option is disabled by default.  If you want to know the object_name of code that is being used (triggers, stored procedures, etc), then this option must be enabled.</p>
<p>I have specified the same actions to be used for each of the events I am trapping, but this is purely up to you.  If you do not wish to capture these actions, then you can remove them or add different actions as necessary.  I have also specified a different predicate for each of the events.  This is something that works so much better in XEvents than Profiler – the filtering that can be applied is much more flexible.</p>
<p><img decoding="async" class="alignnone" src="https://jasonbrimhall.info/wp-content/uploads/2018/03/xe_flysuperhero-1024x1024.png" alt="xevents man" width="350" /></p>
<p>Once the session is started, then it is merely a matter of figuring out how to look at the data.  I generally use T-SQL to parse the data from the file, but you could also fiddle with the GUI as well.  You can access that by right-clicking the event session in management studio and then selecting “Watch Live Data”.  If everything is configured properly, then you will start to see data after the first event is triggered.  From within the GUI, you can pick and choose which columns to display in your view.  You can pause the display of the session data, or you can even “erase” the data from the current view (just like in Profiler).  You can even filter, aggregate or group the data in the view from the GUI.</p>
<p>The use of the GUI is up to you.  I still prefer to use the script route.  With that route, here is a sample of what you may need to write in order to display the data from the session data file that has been captured.</p>
<p><div id="wpshdo_20" class="wp-synhighlighter-outer"><div id="wpshdt_20" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_20"></a><a id="wpshat_20" class="wp-synhighlighter-title" href="#codesyntax_20"  onClick="javascript:wpsh_toggleBlock(20)" title="Click to show/hide code block">Source code</a></td><td align="right"><a href="#codesyntax_20" onClick="javascript:wpsh_code(20)" title="Show code only"><img decoding="async" border="0" style="border: 0 none" src="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_20" onClick="javascript:wpsh_print(20)" title="Print code"><img decoding="async" border="0" style="border: 0 none" src="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img decoding="async" border="0" style="border: 0 none" src="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_20" class="wp-synhighlighter-inner" style="display: block;"><pre class="tsql" style="font-family:monospace;"><span class="kw1">USE</span> master;
GO
&nbsp;
<span class="kw1">SELECT</span>
event_data.<span class="kw1">VALUE</span><span class="br0">&#40;</span><span class="st0">'(event/@name)[1]'</span>, <span class="st0">'varchar(50)'</span><span class="br0">&#41;</span> <span class="kw1">AS</span> event_name,
    event_data.<span class="kw1">VALUE</span><span class="br0">&#40;</span><span class="st0">'(event/@timestamp)[1]'</span>, <span class="st0">'varchar(50)'</span><span class="br0">&#41;</span> <span class="kw1">AS</span> <span class="br0">[</span><span class="kw1">TIMESTAMP</span><span class="br0">]</span>,
	event_data.<span class="kw1">VALUE</span><span class="br0">&#40;</span><span class="st0">'(event/action[@name=&quot;database_name&quot;]/value)[1]'</span>, <span class="st0">'varchar(max)'</span><span class="br0">&#41;</span> <span class="kw1">AS</span> DBName
	,event_data.<span class="kw1">VALUE</span><span class="br0">&#40;</span><span class="st0">'(event/data[@name=&quot;statement&quot;]/value)[1]'</span>, <span class="st0">'varchar(max)'</span><span class="br0">&#41;</span> <span class="kw1">AS</span> SQLText
	,event_data.<span class="kw1">VALUE</span><span class="br0">&#40;</span><span class="st0">'(event/data[@name=&quot;object_name&quot;]/value)[1]'</span>, <span class="st0">'varchar(max)'</span><span class="br0">&#41;</span> <span class="kw1">AS</span> ObjName
	,event_data.<span class="kw1">VALUE</span><span class="br0">&#40;</span><span class="st0">'(event/action[@name=&quot;session_id&quot;]/value)[1]'</span>, <span class="st0">'varchar(max)'</span><span class="br0">&#41;</span> <span class="kw1">AS</span> SessionID
	,event_data.<span class="kw1">VALUE</span><span class="br0">&#40;</span><span class="st0">'(event/action[@name=&quot;nt_username&quot;]/value)[1]'</span>, <span class="st0">'varchar(max)'</span><span class="br0">&#41;</span> <span class="kw1">AS</span> ExecUser
	,event_data.<span class="kw1">VALUE</span><span class="br0">&#40;</span><span class="st0">'(event/action[@name=&quot;client_hostname&quot;]/value)[1]'</span>, <span class="st0">'varchar(max)'</span><span class="br0">&#41;</span> <span class="kw1">AS</span> Client_HostName,
	event_data.<span class="kw1">VALUE</span><span class="br0">&#40;</span><span class="st0">'(event/action[@name=&quot;client_app_name&quot;]/value)[1]'</span>, <span class="st0">'varchar(max)'</span><span class="br0">&#41;</span> <span class="kw1">AS</span> Client_AppName
<span class="kw1">FROM</span><span class="br0">&#40;</span>
<span class="kw1">SELECT</span> <span class="kw1">CONVERT</span><span class="br0">&#40;</span>XML, t2.<span class="me1">event_data</span><span class="br0">&#41;</span> <span class="kw1">AS</span> event_data
 <span class="kw1">FROM</span> <span class="br0">&#40;</span>
  <span class="kw1">SELECT</span> target_data <span class="sy0">=</span> <span class="kw1">CONVERT</span><span class="br0">&#40;</span>XML, target_data<span class="br0">&#41;</span>
   <span class="kw1">FROM</span> sys.<span class="me1">dm_xe_session_targets</span> t
    <span class="kw1">INNER</span> <span class="sy0">JOIN</span> sys.<span class="me1">dm_xe_sessions</span> s 
        <span class="kw1">ON</span> t.<span class="me1">event_session_address</span> <span class="sy0">=</span> s.<span class="me1">address</span>
   <span class="kw1">WHERE</span> t.<span class="me1">target_name</span> <span class="sy0">=</span> <span class="st0">'event_file'</span>
    <span class="sy0">AND</span> s.<span class="me1">name</span> <span class="sy0">=</span> <span class="st0">'TraceIncomingQueries'</span><span class="br0">&#41;</span> cte1
   <span class="sy0">CROSS</span> APPLY cte1.<span class="me1">target_data</span>.<span class="me1">nodes</span><span class="br0">&#40;</span><span class="st0">'//EventFileTarget/File'</span><span class="br0">&#41;</span> FileEvent<span class="br0">&#40;</span>FileTarget<span class="br0">&#41;</span>
   <span class="sy0">CROSS</span> APPLY  sys.<span class="me1">fn_xe_file_target_read_file</span><span class="br0">&#40;</span>FileEvent.<span class="me1">FileTarget</span>.<span class="kw1">VALUE</span><span class="br0">&#40;</span><span class="st0">'@name'</span>, <span class="st0">'varchar(1000)'</span><span class="br0">&#41;</span>, <span class="sy0">NULL</span>, <span class="sy0">NULL</span>, <span class="sy0">NULL</span><span class="br0">&#41;</span> t2<span class="br0">&#41;</span>
    <span class="kw1">AS</span> evts<span class="br0">&#40;</span>event_data<span class="br0">&#41;</span>;</pre></div></div></p>
<p>It really is that simple.  Now you have a means to run a Profiler-like trace on your server without the impact of Profiler.  XEvents provides the means to run a more evolved Profiler session on your data to capture things like queries from an application. XEvents does all of this without the severe penalty of Profiler and with many additional bonuses.  Try it out and enjoy!</p>
<p>The post <a href="https://sqlsolutionsgroup.com/xevents-as-profiler/">How To: XEvents as Profiler</a> appeared first on <a href="https://sqlsolutionsgroup.com">SQL Solutions Group</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://sqlsolutionsgroup.com/xevents-as-profiler/feed/</wfw:commentRss>
			<slash:comments>2</slash:comments>
		
		
			</item>
	</channel>
</rss>
