<?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>&#8235;Madeira &#187; Tomer Shtrum&#8236;</title>	<atom:link href="http://www.madeira.co.il/author/tomer/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.madeira.co.il</link>
	<description>&#8235;SQL Server Services&#8236;</description>	<lastBuildDate>Sat, 19 May 2012 09:04:40 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>he</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>&#8235;SPARSE COLUMNS&#8236;</title>		<link>http://www.madeira.co.il/sparse-columns/</link>
		<comments>http://www.madeira.co.il/sparse-columns/#comments</comments>
		<pubDate>Sat, 25 Feb 2012 19:15:41 +0000</pubDate>
		<dc:creator>&#8235;Tomer Shtrum&#8236;</dc:creator>				<category><![CDATA[בלוגים]]></category>
		<category><![CDATA[מאמרים]]></category>
		<category><![CDATA[מקודמות]]></category>

		<guid isPermaLink="false">http://www.madeira.co.il/?p=4427</guid>
		<description><![CDATA[&#8235;
One of the basic rules when creating or altering a table is to configure and <a href="http://www.madeira.co.il/sparse-columns/"><strong>לסיפור המלא...</strong></a>&#8236;]]></description>			<content:encoded><![CDATA[<div dir="rtl"><p><img src='http://www.madeira.co.il/wp-content/plugins/simple-post-thumbnails/timthumb.php?src=/wp-content/thumbnails/4427.jpg&amp;w=214&amp;h=129&amp;zc=1&amp;ft=jpg' alt='post thumbnail' /></p>
<p dir="ltr">One of the basic rules when creating or altering a table is to configure and adjust the right columns properties such as data types and nullable values. It is done because we assume what kind of data those columns will contain.<br />
For example, we will not define a person's age column with &quot;int&quot; data type because it will just waste unnecessary storage space.<br />
Even if we will configure the optimal data types for each column we will still face columns that holds a lot of nullable or zeroes values. Those will use storage just as if they were containing with data.<br />
SQL server 2008 offers a new column property which is called &quot;sparse columns&quot;.<br />
The goal <em>of</em> sparse columns is to minimize storage ignoring empty columns cells.<br />
Obviously, nothing comes free. Every sparse column data will increase each column's data storage use by four bytes. Therefore if you define a column with the data type &quot;int&quot; that normally uses four bytes it will now need eight bytes.</p>
<p dir="ltr"><strong>Best practice recommendations:</strong></p>
<p dir="ltr">The main idea of this post is to use &quot;spares columns&quot; only when the nullable percentage of data is significantly high (Microsoft numbers are between 20-40 percent).<br />
Secondly, use &quot;sparse columns&quot; when dealing with big storage data types because then the addition of the four bytes will be less significant comparing small data types such as &quot;bit&quot; or &quot;tinyint&quot;.<br />
Third, when we want to create a filtered index on a column that holds nullable values, the best practice is to create the column with sparse. That way the SQL server knows to ignore the nullable values and save precious storage.</p>
<p dir="ltr"><strong>Pros </strong></p>
<p dir="ltr">· Reading and writing on &quot;sparse columns&quot; is no different than regular columns.<br />
· We can define an additional xml &quot;column set&quot; that will point to the &quot;sparse columns&quot; and gather their data (see an example at the end of the   post).</p>
<p dir="ltr"><strong>Cons</strong></p>
<p dir="ltr">· &quot;Sparse columns&quot; is a property of the storage layer and not apart of the table properties layer so when we perform a &quot;select into&quot; command     the sparse property will not be copied to the new table.<br />
· There are several data types that do not support &quot;sparse column&quot; such as:<br />
Text, Timestamp, Geometry, image, ntext, user-defined data types.<br />
· &quot;Sparse columns&quot; cannot be configured on &quot;file stream&quot; columns.<br />
· &quot;Sparse columns&quot; cannot be configured on a computed columns.<br />
· &quot;Sparse columns&quot; cannot have default values.<br />
· &quot;Sparse columns&quot; cannot be configured on clustered index columns.</p>
<p dir="ltr"><strong>Examples</strong></p>
<p dir="ltr">In order to present and examine the use of &quot;sparse columns&quot; I created two identical tables. One with &quot;sparse columns&quot; and the other without &quot;sparse columns&quot;.<br />
Then I inserted into the two tables 50,000 rows with nullable values.<br />
Then, I called the &quot;<code>sp_spaceused</code>&quot; procedure to see the differences between the results. It is easy to see that the table with the &quot;sparse columns&quot; used much less storage space.</p>
<div>
<div dir="ltr">
<p><!--CRLF--></p>
<pre><span style="color: #0000ff">CREATE</span> <span style="color: #0000ff">TABLE
</span>UnSparsed_Table(
ID <span style="color: #0000ff">INT</span> <span style="color: #0000ff">IDENTITY</span>(1,1),
ColX <span style="color: #0000ff">INT</span>,
ColY <span style="color: #0000ff">VARCHAR</span>(100),
ColZ DateTime)
<span style="color: #0000ff">GO</span></pre>
<p><!--CRLF--></p>
<p><!--CRLF--></p>
<pre><span style="color: #0000ff">CREATE</span> <span style="color: #0000ff">TABLE
</span>Sparsed_Table(ID <span style="color: #0000ff">INT</span> <span style="color: #0000ff">IDENTITY</span>(1,1),
ColX <span style="color: #0000ff">INT</span> SPARSE,
Coly <span style="color: #0000ff">VARCHAR</span>(100) SPARSE,
ColZ DateTime SPARSE)
<span style="color: #0000ff">GO</span></pre>
<p><!--CRLF--></p>
<p><!--CRLF--></p>
<pre><span style="color: #0000ff">DECLARE
</span>@iterator <span style="color: #0000ff">INT</span> = 0
<span style="color: #0000ff">WHILE</span> @iterator &lt; 50000
<span style="color: #0000ff">BEGIN
</span>    INSERT <span style="color: #0000ff">INTO
</span>    UnSparsed_Table <span style="color: #0000ff">VALUES</span> (<span style="color: #0000ff">NULL</span>,<span style="color: #0000ff">NULL</span>, <span style="color: #0000ff">NULL</span>)
    INSERT <span style="color: #0000ff">INTO
</span>    Sparsed_Table <span style="color: #0000ff">VALUES</span> (<span style="color: #0000ff">NULL</span>, <span style="color: #0000ff">NULL</span>, <span style="color: #0000ff">NULL</span>)
    <span style="color: #0000ff">SET
</span>    @iterator+=1
<span style="color: #0000ff">END
</span><span style="color: #0000ff">GO</span></pre>
<p><!--CRLF--></p>
<p><!--CRLF--></p>
<pre>sp_spaceused
<span style="color: #006080">'UnSparsed_Table'
</span><span style="color: #0000ff">GO</span></pre>
<p><!--CRLF--></p>
<p><!--CRLF--></p>
<pre>sp_spaceused�
<span style="color: #006080">'Sparsed_Table'
</span><span style="color: #0000ff">GO</span></pre>
<p><!--CRLF--></p>
<p><!--CRLF--></p>
<pre><span style="color: #0000ff">DROP</span> <span style="color: #0000ff">TABLE
</span>UnSparsed_Table
<span style="color: #0000ff">GO</span></pre>
<p><!--CRLF--></p>
<p><!--CRLF--></p>
<pre><span style="color: #0000ff">DROP
</span><span style="color: #0000ff">TABLE</span> Sparsed_Table
GO</pre>
<p><!--CRLF--></p>
</div>
</div>
<p dir="ltr">
<p dir="ltr"><a href="http://www.madeira.co.il/wp-content/uploads/2012/02/21.png" rel="wp-prettyPhoto[g4427]"><img class="alignleft size-full wp-image-4431" src="http://www.madeira.co.il/wp-content/uploads/2012/02/21.png" alt="" width="506" height="160" /></a></p>
<p dir="ltr">In order to present and examine the use of &quot;column set&quot; I created a table with two &quot;sparse columns&quot; and another XML column. Then I inserted one row into the table and selected it to see the results. As you can see the XML column holds the &quot;sparse columns&quot; data.</p>
<div>
<div dir="ltr">
<pre><span style="color: #0000ff">CREATE</span> <span style="color: #0000ff">TABLE</span> Example2(
ColI <span style="color: #0000ff">int</span> SPARSE,
ColJ <span style="color: #0000ff">int</span> SPARSE,
XMLC xml column_set <span style="color: #0000ff">FOR</span> ALL_SPARSE_COLUMNS)
<span style="color: #0000ff">GO</span></pre>
<p><!--CRLF--></p>
<p><!--CRLF--></p>
<pre>INSERT Example2(ColI, ColJ)
<span style="color: #0000ff">VALUES</span> (1,2)
<span style="color: #0000ff">GO</span></pre>
<p><!--CRLF--></p>
<p><!--CRLF--></p>
<pre><span style="color: #0000ff">SELECT</span> XMLC <span style="color: #0000ff">FROM</span> Example2
<span style="color: #0000ff">GO</span></pre>
<p><!--CRLF--></p>
</div>
</div>
<p dir="ltr">
<p dir="ltr"><a href="http://www.madeira.co.il/wp-content/uploads/2012/02/16.png" rel="wp-prettyPhoto[g4427]"><img class="alignleft size-full wp-image-4432" src="http://www.madeira.co.il/wp-content/uploads/2012/02/16.png" alt="" width="415" height="65" /></a></p>
<p dir="ltr">For conclusion, &quot;sparse columns&quot; is a great property when it comes to saving storage space, but remember to use it wisely.</p>
<p dir="ltr">Hope you enjoyed reading my post.<br />
Tomer.</p>
</div>]]></content:encoded>			<wfw:commentRss>http://www.madeira.co.il/sparse-columns/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>&#8235;How to run a query on multiple servers simultaneously in SSMS 2008&#8236;</title>		<link>http://www.madeira.co.il/how-to-run-a-query-on-multiple-servers-simultaneously-in-ssms-2008/</link>
		<comments>http://www.madeira.co.il/how-to-run-a-query-on-multiple-servers-simultaneously-in-ssms-2008/#comments</comments>
		<pubDate>Sun, 12 Feb 2012 18:18:07 +0000</pubDate>
		<dc:creator>&#8235;Tomer Shtrum&#8236;</dc:creator>				<category><![CDATA[בלוגים]]></category>
		<category><![CDATA[כללי]]></category>
		<category><![CDATA[מקודמות]]></category>

		<guid isPermaLink="false">http://www.madeira.co.il/?p=4223</guid>
		<description><![CDATA[&#8235;
At first, let's start with a cool relaxation exercise. Close your eyes and imagine that <a href="http://www.madeira.co.il/how-to-run-a-query-on-multiple-servers-simultaneously-in-ssms-2008/"><strong>לסיפור המלא...</strong></a>&#8236;]]></description>			<content:encoded><![CDATA[<div dir="rtl"><p><img src='http://www.madeira.co.il/wp-content/plugins/simple-post-thumbnails/timthumb.php?src=/wp-content/thumbnails/4223.png&amp;w=214&amp;h=129&amp;zc=1&amp;ft=jpg' alt='post thumbnail' /></p>
<p dir="ltr">At first, let's start with a cool relaxation exercise. Close your eyes and imagine that your DBA work environment includes only one server. Nice, isn't it? Now let's wake up to real life. As you all know typical, small, simple environment will include about 10 servers and in big companies it can reach to thousands or more servers (I can't imagine how many Microsoft has). Therefor raises the need to monitor, manage and organize all servers.</p>
<p dir="ltr">Examples of organized group of servers can be by their work environment (dev, qa, production), &quot;SQL Server&quot; versions and even by geographic spreading.</p>
<p dir="ltr">As part of the DBA work routine we would like to collect info to our servers on a daily basis. We can do it by connecting to each one of server separately or by third part applications, but that can be very inconvenient and can take a lot of time.</p>
<p dir="ltr">It would be very nice if in our basic tool the &quot;management studio&quot; we can execute one command and &quot;SQL Server&quot; will connect to each server and collect all the data.</p>
<p dir="ltr">It would also be nice if it will combine all the data in one table with a new &quot;server's name&quot; column.</p>
<p dir="ltr">Since 2008 it can be done, and it's called &quot;Central management servers&quot;, also known as CMS. With that new feature we can create groups of servers from all kinds of &quot;SQL servers&quot; versions (old and newer). You have to choose one server (2008 version) to be the central management server and it will hold the list of the server's groups. The list will be available for other users for as long as the server service is running. That way we will be able to query each of the groups.</p>
<p dir="ltr">In order to setup CMS there are few requirements:</p>
<p dir="ltr">- You will need at least one instance of &quot;SQL Server&quot; 2008 that will be the management server.</p>
<p dir="ltr">- All the connection to the servers should be with windows authentication because CMS cannot hold the connection details for SQL authentication.</p>
<p dir="ltr">- The server that was defined to manage the list of servers cannot participate in the registered servers.</p>
<p dir="ltr"><span style="text-decoration: underline">This is how to create CMS:</span></p>
<p dir="ltr">&quot;BOL&quot; manual</p>
<p dir="ltr">1. In SQL Server Management Studio, on the <strong>View</strong> menu, click <strong>Registered Servers</strong>.</p>
<p dir="ltr">2. In Registered Servers, expand <strong>Database Engine</strong>, right-click <strong>Central Management Servers</strong>, point to <strong>New</strong>, and then click <strong>Central Management Servers</strong>.</p>
<p dir="ltr">3. In the <strong>New Server Registration</strong> dialog box, register the instance of SQL Server that you want to become the central management server.</p>
<p dir="ltr">4. In Registered Servers, right-click the central management server, point to <strong>New</strong>, and then click <strong>New Server Group</strong>.</p>
<p dir="ltr">5. Type a group name and description, and then click <strong>OK</strong>.In Registered Servers, right-click the central management server group, and then click <strong>New Server Registration</strong>.</p>
<p dir="ltr">6. In the <strong>New Server Registration</strong> dialog box, register one or more instances of SQL Server that you want to become members of the server group.</p>
<p dir="ltr"><span style="text-decoration: underline">And here is an example of CMS creation:</span></p>
<p dir="ltr"><span style="text-decoration: underline"><a href="http://www.madeira.co.il/wp-content/uploads/2012/02/14.png" rel="wp-prettyPhoto[g4223]"><img class="alignleft size-full wp-image-4262" src="http://www.madeira.co.il/wp-content/uploads/2012/02/14.png" alt="" width="450" height="2234" /></a></span></p>
<p dir="ltr">Here are some examples for queries on multiple servers:</p>
<p dir="ltr"><a href="http://www.madeira.co.il/wp-content/uploads/2012/02/91.png" rel="wp-prettyPhoto[g4223]"><img class="alignleft size-full wp-image-4273" src="http://www.madeira.co.il/wp-content/uploads/2012/02/91.png" alt="" width="488" height="483" /></a></p>
<p dir="ltr"><span style="text-decoration: underline">Limitations and cons:</span></p>
<p dir="ltr">- It is impossible to save the table data because each row represent different server. If you try to insert the rows to another table it will create separate table for each server with only its rows.</p>
<p dir="ltr">- You cannot join the data with other tables.</p>
<p dir="ltr">- You cannot sort the data. The order by command will sort the rows by each server.</p>
<p dir="ltr">- &quot;SQL Server&quot; connects to each server and run your commands. If one of the servers is &quot;case sensitive&quot; and your commend is not, then you will get an error.</p>
<p dir="ltr">- You must confirm that all servers return the same number of columns. If that is not the case, you can change the setting of CMS to return separated tables instead of one big table.</p>
<p dir="ltr">Note about security:<br />
As you imagine, if you don’t have access to read the data of one of the servers you will not be able to run query on a server that is listed in the group.</p>
<p dir="ltr">For conclusion, CMS can save you a lot of expansive time and make your multiple servers much more manageable and easy to use and monitor.</p>
<p dir="ltr">Tomer Shtrum.</p>
</div>]]></content:encoded>			<wfw:commentRss>http://www.madeira.co.il/how-to-run-a-query-on-multiple-servers-simultaneously-in-ssms-2008/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>&#8235;?READPAST what is it good for&#8236;</title>		<link>http://www.madeira.co.il/readpast-what-is-it-good-for/</link>
		<comments>http://www.madeira.co.il/readpast-what-is-it-good-for/#comments</comments>
		<pubDate>Wed, 25 Jan 2012 19:33:18 +0000</pubDate>
		<dc:creator>&#8235;Tomer Shtrum&#8236;</dc:creator>				<category><![CDATA[בלוגים]]></category>
		<category><![CDATA[כללי]]></category>
		<category><![CDATA[מקודמות]]></category>

		<guid isPermaLink="false">http://www.madeira.co.il/?p=3890</guid>
		<description><![CDATA[&#8235;
After my last post about monitoring deadlocks I will now show a way to prevent <a href="http://www.madeira.co.il/readpast-what-is-it-good-for/"><strong>לסיפור המלא...</strong></a>&#8236;]]></description>			<content:encoded><![CDATA[<div dir="rtl"><p><img src='http://www.madeira.co.il/wp-content/plugins/simple-post-thumbnails/timthumb.php?src=/wp-content/thumbnails/3890.jpg&amp;w=214&amp;h=129&amp;zc=1&amp;ft=jpg' alt='post thumbnail' /></p>
<p dir="ltr">After my last post about <a href="http://www.madeira.co.il/monitoring-deadlocks/" target="_blank">monitoring deadlocks</a> I will now show a way to prevent deadlock and unnecessary locks and to improve system performance.<br />
<strong>Introduction:<br />
</strong>SQL server has a lot of working mechanisms. The lock mechanism is one of the most important mechanism. Its goal is to keep and secure the data reliability and consistently. This mechanism is very complex and depends on many variables. The mechanism is design to let the system work in the optimal way. It is based on the concept of &quot;all or nothing at all&quot;, which means either everything happens during a transaction or nothing happens at all. In specific cases we would like to intervene by setting lock options manually and by doing so to affect the lock mechanism. It is not recommended to use those options on a daily bases, only on specific cases, when we know the type of data that we are dealing with (whether we are receiving data or want to make changes).<br />
The option I am about to present is called &quot;Readpast&quot;. Readpast simply means that when we reach for a table data (select, update or delete) and one or more of the table rows or pages are locked by one or more transactions the lock mechanism will ignore those rows and retrieve the rest of the data which is not locked.<br />
The advantage is obviously a performance improvement because there is no waiting time for another transactions locks. But, the disadvantage is more significant than the advantage because even if we accept the loss of data we will not be able to know which part we lost and when.</p>
<p dir="ltr">The most common and recommendable way to use Readpast is when there is a need to implement a queue of command list. For example: there is a table which each row represents a command that needs to be executed. The table also has a separate and an independent process that inserts new commands every time. The order of the commands is not important as long as they are being done. That is why Readpast is the ultimate solution because each process that will turn to the table will receive the next command with no waiting time.</p>
<p dir="ltr"><strong>Here is a simple example of how Readpast option impacts the lock mechanism:</strong></p>
<p dir="ltr"><strong> <em> · Create a new table with two columns (command ID, command description).</em></strong></p>
<p dir="ltr"><em><strong> · Populate five new rows.</strong></em></p>
<div>
<div>
<pre style="text-align: left"><span style="color: #0000ff">CREATE</span> <span style="color: #0000ff">TABLE</span> ReadPastTest</pre>
<p style="text-align: left"><!--CRLF--></p>
<pre style="text-align: left">)</pre>
<p style="text-align: left"><!--CRLF--></p>
<pre style="text-align: left">CommandId <span style="color: #0000ff">INT</span>, CommandDescription <span style="color: #0000ff">VARCHAR</span>(<span style="color: #0000ff">MAX </span></pre>
<pre style="text-align: left">(</pre>
<p style="text-align: left"><!--CRLF--></p>
<pre style="text-align: left"><span style="color: #0000ff">GO </span></pre>
<p style="text-align: left"><!--CRLF--></p>
<pre style="text-align: left">INSERT <span style="color: #0000ff">INTO</span> ReadPastTest</pre>
<p style="text-align: left"><!--CRLF--></p>
<pre style="text-align: left"><span style="color: #0000ff"> VALUES</span> (1,<span style="color: #006080">'do a'</span>),(2,<span style="color: #006080">'do b'</span>),(3,<span style="color: #006080">'do c'</span>),(4,<span style="color: #006080">'do d'),</span><span style="color: #006080">(5,'do e')</span></pre>
<pre style="text-align: left"> GO</pre>
<pre style="text-align: left"><span style="color: #0000ff"> SELECT</span> CommandDescription</pre>
<p style="text-align: left"><!--CRLF--></p>
<pre style="text-align: left"><span style="color: #0000ff">FROM</span> ReadPastTest</pre>
<p style="text-align: left"><!--CRLF--></p>
<pre style="text-align: left">GO</pre>
<p><!--CRLF--></p>
</div>
</div>
<p dir="ltr"><strong><em>· Select the rows. </em></strong></p>
<p dir="ltr"><a href="http://www.madeira.co.il/wp-content/uploads/2012/01/11.jpg" rel="wp-prettyPhoto[g3890]"><img class="alignleft size-full wp-image-3897" src="http://www.madeira.co.il/wp-content/uploads/2012/01/11.jpg" alt="" width="147" height="135" /></a></p>
<p dir="ltr">
<p dir="ltr"><strong><em>· Open a new transaction and update one of the rows and keep the transaction open. </em></strong></p>
<div>
<div>
<pre style="text-align: left"><span style="color: #0000ff">BEGIN</span> <span style="color: #0000ff">TRANSACTION </span></pre>
<p style="text-align: left"><!--CRLF--></p>
<pre style="text-align: left"><span style="color: #0000ff">UPDATE</span> ReadPastTest</pre>
<p style="text-align: left"><!--CRLF--></p>
<pre style="text-align: left"><span style="color: #0000ff">SET</span> CommandId = 11</pre>
<p style="text-align: left"><!--CRLF--></p>
<pre style="text-align: left"><span style="color: #0000ff">WHERE</span> CommandId = 1</pre>
<p style="text-align: left"><!--CRLF--></p>
</div>
</div>
<p style="text-align: left">
<p dir="ltr"><em><strong>· Select the rows again with another query window that simulate a new process with the Readpast option. </strong></em></p>
<div>
<div>
<pre style="text-align: left"><span style="color: #0000ff">SELECT</span> CommandDescription</pre>
<p style="text-align: left"><!--CRLF--></p>
<pre style="text-align: left"><span style="color: #0000ff">(FROM</span> ReadPastTest <span style="color: #0000ff">WITH</span> (READPAST</pre>
<p style="text-align: left"><!--CRLF--></p>
<pre style="text-align: left">GO</pre>
</div>
</div>
<p dir="ltr"><em><strong>· Observe the changes. </strong></em></p>
<p dir="ltr"><a href="http://www.madeira.co.il/wp-content/uploads/2012/01/21.jpg" rel="wp-prettyPhoto[g3890]"><img class="alignleft size-full wp-image-3898" src="http://www.madeira.co.il/wp-content/uploads/2012/01/21.jpg" alt="" width="145" height="114" /></a></p>
<p dir="ltr"><em><strong>· Rollback the first transaction and select the table again.</strong></em></p>
<div>
<div>
<pre style="text-align: left"><span style="color: #0000ff">ROLLBACK</span></pre>
<p style="text-align: left"><!--CRLF--></p>
<pre style="text-align: left"><span style="color: #0000ff">SELECT</span> CommandDescription</pre>
<p style="text-align: left"><!--CRLF--></p>
<pre style="text-align: left"><span style="color: #0000ff">FROM</span> ReadPastTest</pre>
<p style="text-align: left"><!--CRLF--></p>
<pre style="text-align: left">GO</pre>
<p><!--CRLF--></p>
</div>
</div>
<p dir="ltr"><a href="http://www.madeira.co.il/wp-content/uploads/2012/01/12.jpg" rel="wp-prettyPhoto[g3890]"><img class="alignleft size-full wp-image-3899" src="http://www.madeira.co.il/wp-content/uploads/2012/01/12.jpg" alt="" width="147" height="135" /></a></p>
<p dir="ltr">
<p dir="ltr">As you can see at the first selection we get all five rows description and the second selection we get only four rows because one row is locked by the first transaction and it was skipped, at last the missing row returns after the rollback.</p>
<p dir="ltr"><strong>In conclusion:</strong></p>
<p dir="ltr">the Readpast option is suited to specific cases only, and can provides a perfect solution to those cases. It is important to beware not to use it on every deadlock or locks problems.</p>
</div>]]></content:encoded>			<wfw:commentRss>http://www.madeira.co.il/readpast-what-is-it-good-for/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>&#8235;MONITORING DEADLOCKS&#8236;</title>		<link>http://www.madeira.co.il/monitoring-deadlocks/</link>
		<comments>http://www.madeira.co.il/monitoring-deadlocks/#comments</comments>
		<pubDate>Tue, 10 Jan 2012 15:12:00 +0000</pubDate>
		<dc:creator>&#8235;Tomer Shtrum&#8236;</dc:creator>				<category><![CDATA[בלוגים]]></category>
		<category><![CDATA[כללי]]></category>
		<category><![CDATA[מקודמות]]></category>

		<guid isPermaLink="false">http://www.madeira.co.il/?p=3366</guid>
		<description><![CDATA[&#8235;
In order to monitor and analyze deadlocks in your server you will first need to <a href="http://www.madeira.co.il/monitoring-deadlocks/"><strong>לסיפור המלא...</strong></a>&#8236;]]></description>			<content:encoded><![CDATA[<div dir="rtl"><p><img src='http://www.madeira.co.il/wp-content/plugins/simple-post-thumbnails/timthumb.php?src=/wp-content/thumbnails/3366.jpg&amp;w=214&amp;h=129&amp;zc=1&amp;ft=jpg' alt='post thumbnail' /></p>
<p dir="ltr">In order to monitor and analyze deadlocks in your server you will first need to understand what does deadlock mean.</p>
<blockquote>
<p dir="ltr">Books on line definition:</p>
<p dir="ltr">&quot;A deadlock occurs when two or more tasks permanently block each other by each task having a lock on a resource which the other tasks are trying to lock.&quot;</p>
</blockquote>
<p dir="ltr">
<p dir="ltr">In other words, this is a situation when two or more processes holds a lock on a different resource try to get a lock on the other process resource which is already locked. This will eventually create an endless loop. Sql server knows how to deal with those kind of situations with the &quot;Deadlock detection&quot; mechanism.</p>
<p dir="ltr">The mechanism continuously (every five seconds) checks the lock monitor thread and searches for deadlocks.</p>
<p dir="ltr">If a deadlock occurs the mechanism choses a process &quot;victim&quot; and rolls back his transaction. The victim is the process that its rollback will need the less resources.</p>
<p dir="ltr">Note &#8211; If we want we can predefine the process importance level and then the victim will be chosen by our definition.</p>
<p dir="ltr">Rollback the victim process will enable the other process to finish its transaction and that will end the loop.</p>
<p dir="ltr">It will be wise to create a &quot;try and catch&quot; mechanism in the processes transactions that will re-execute the victim process after a few random seconds.</p>
<p style="direction: ltr">Here is a script as an example of a simple and classical deadlock, which contains two transactions for each query window.</p>
<p dir="ltr"><strong>first query:</strong></p>
<div>
<pre style="text-align: left"><span style="color: #0000ff">Transaction</span> A--

<span style="color: #0000ff">USE</span> AdventureWorks

<span style="color: #0000ff">GO</span>

<span style="color: #0000ff">BEGIN</span> <span style="color: #0000ff">TRANSACTION</span>

<span style="color: #0000ff">Statement</span> 1--

<span style="color: #0000ff">UPDATE</span> Sales.SalesOrderDetail

<span style="color: #0000ff">SET</span> OrderQty = OrderQty * 2

<span style="color: #0000ff">WHERE</span> SalesOrderID = 43659 <span style="color: #0000ff">and</span> SalesOrderDetailID = 1

Hold <span style="color: #0000ff">for</span> 10 seconds--

<span style="color: #0000ff">WAITFOR</span> DELAY <span style="color: #006080">'00:00:10'</span>

<span style="color: #0000ff">Statement</span> 2--

<span style="color: #0000ff">SELECT</span> * <span style="color: #0000ff">FROM</span> HumanResources.Department

<span style="color: #0000ff">WHERE</span> DepartmentID = 1

<span style="color: #0000ff">COMMIT</span>

<span style="color: #0000ff">GO</span></pre>
</div>
<p dir="ltr"><strong>second query:</strong></p>
<div>
<div>
<pre style="text-align: left"><span style="color: #606060">   </span> <span style="color: #0000ff">USE</span> AdventureWorks</pre>
<p style="text-align: left"><!--CRLF--></p>
<pre style="text-align: left"><span style="color: #606060">  </span> <span style="color: #0000ff">GO</span></pre>
<p style="text-align: left"><!--CRLF--></p>
<pre style="text-align: left"><span style="color: #606060">  </span> <span style="color: #0000ff">BEGIN</span> <span style="color: #0000ff">TRANSACTION</span></pre>
<p style="text-align: left"><!--CRLF--></p>
<pre style="text-align: left"><span style="color: #606060">  </span> <span style="color: #0000ff">Statement</span> 1--</pre>
<p style="text-align: left"><!--CRLF--></p>
<pre style="text-align: left"><span style="color: #606060">   </span> <span style="color: #0000ff">UPDATE</span> HumanResources.Department</pre>
<p style="text-align: left"><!--CRLF--></p>
<pre style="text-align: left"><span style="color: #606060">  </span> <span style="color: #0000ff">SET</span> Name = Name + <span style="color: #006080">' added text'</span></pre>
<p style="text-align: left"><!--CRLF--></p>
<pre style="text-align: left"><span style="color: #606060">  </span> <span style="color: #0000ff">WHERE</span> DepartmentID = 1</pre>
<p style="text-align: left"><!--CRLF--></p>
<pre style="text-align: left"><span style="color: #606060">  </span> Hold <span style="color: #0000ff">for</span> 10 seconds--</pre>
<p style="text-align: left"><!--CRLF--></p>
<pre style="text-align: left"><span style="color: #606060">  </span> <span style="color: #0000ff">WAITFOR</span> DELAY <span style="color: #006080">'00:00:10'</span></pre>
<p style="text-align: left"><!--CRLF--></p>
<pre style="text-align: left"><span style="color: #606060">  </span><span style="color: #0000ff">Statement</span> 2--</pre>
<p style="text-align: left"><!--CRLF--></p>
<pre style="text-align: left"><span style="color: #606060">  </span> <span style="color: #0000ff">SELECT</span> * <span style="color: #0000ff">FROM</span> Sales.SalesOrderDetail</pre>
<p style="text-align: left"><!--CRLF--></p>
<pre style="text-align: left"><span style="color: #606060">  </span> <span style="color: #0000ff">WHERE</span> SalesOrderID = 43659 <span style="color: #0000ff">and</span> SalesOrderDetailID = 1</pre>
<p style="text-align: left"><!--CRLF--></p>
<pre style="text-align: left"><span style="color: #606060">  </span> <span style="color: #0000ff">COMMIT</span></pre>
<p style="text-align: left"><!--CRLF--></p>
<pre style="text-align: left"><span style="color: #606060">  </span> <span style="color: #0000ff">GO</span></pre>
<p style="text-align: left"><!--CRLF--></p>
<pre style="text-align: left"><span style="color: #606060">  </span></pre>
<p><!--CRLF--></p>
</div>
</div>
<p dir="ltr">The scripts runs on AdvantureWorks database.</p>
<p dir="ltr">Script flow:</p>
<p dir="ltr">1. Open two queries with two transactions (each query resemble each process).</p>
<p dir="ltr">2. Each process updates different table.</p>
<p dir="ltr">3. Each process waits ten seconds (in order to execute the other query).</p>
<p dir="ltr">4. Each process tries to select the other process table.</p>
<p dir="ltr">5. You will be able to see in the end that one process finished its transaction and the other one will print a massage that a deadlock occurred.</p>
<p dir="ltr">Now that we understand what is a deadlock, these are the main tools to monitor and identify deadlocks:</p>
<p dir="ltr">
<p dir="ltr"><strong><span style="text-decoration: underline">Windows performance monitor.</span></strong></p>
<p dir="ltr">This tool allows you to see if a deadlock occurs on your server.</p>
<p dir="ltr">In order to start we need to open &quot;windows performance monitor&quot; and to add the &quot;deadlock\sec&quot; counter in the &quot;sql server: locks category&quot;. All is left to do is run again the two script queries that creates the deadlock or wait for a deadlock to occur on the server. We will be able to see the changes on the graph.</p>
<p dir="ltr">Graph example:</p>
<p dir="ltr"><a href="http://www.madeira.co.il/wp-content/uploads/2012/01/perfmon.png" rel="wp-prettyPhoto[g3366]"><img class="alignleft size-medium wp-image-3409" src="http://www.madeira.co.il/wp-content/uploads/2012/01/perfmon-300x142.png" alt="" width="300" height="142" /></a></p>
<p dir="ltr">
<p dir="ltr">
<p dir="ltr"><strong> </strong></p>
<p dir="ltr">
<p dir="ltr">
<p dir="ltr">
<p dir="ltr">
<p dir="ltr">
<p dir="ltr">
<p dir="ltr">
<p dir="ltr">
<p dir="ltr">
<p dir="ltr">
<p dir="ltr">
<p dir="ltr">
<p dir="ltr">
<p dir="ltr">
<p dir="ltr">
<p dir="ltr">
<p dir="ltr">
<p dir="ltr"><span style="font-weight: bold;text-decoration: underline">Sql server trace flags:</span></p>
<p dir="ltr">You can monitor and analyze deadlocks with two sql server trace flags.</p>
<p dir="ltr">Trace flag 1204 and trace flag 1222. They are similar and will write the deadlock description to the sql server error logs. The difference between them is that trace flag 1204 reports for each process separately and trace flag 1222 reports firstly on the processes and then on the resources. It is possible to turn both flags on by typing the command:</p>
<p>&quot;DBCC TRACEON (trace number)&quot; for each trace flag.</p>
<p dir="ltr">According to the explanation:</p>
<p dir="ltr">1. turn on both trace flag</p>
<p dir="ltr">2. execute both scripts</p>
<p dir="ltr">3. open sql server error logs to see which processes were participate in the deadlock, who was chosen to be the victim and most important, what is the transactions code for future analyze.</p>
<p dir="ltr">Error log example:</p>
<p dir="ltr"><a href="http://www.madeira.co.il/wp-content/uploads/2012/01/tarce.png" rel="wp-prettyPhoto[g3366]"><img class="alignleft size-medium wp-image-3410" src="http://www.madeira.co.il/wp-content/uploads/2012/01/tarce-300x101.png" alt="" width="300" height="101" /></a></p>
<p dir="ltr">
<p dir="ltr"><strong> </strong></p>
<p dir="ltr">
<p dir="ltr">
<p dir="ltr">
<p dir="ltr">
<p dir="ltr">
<p dir="ltr">
<p dir="ltr">
<p dir="ltr">
<p dir="ltr">
<p dir="ltr">
<p dir="ltr">
<p dir="ltr">
<p dir="ltr">
<p dir="ltr">
<p dir="ltr"><strong><span style="text-decoration: underline">Sql server profiler:</span></strong></p>
<p dir="ltr">The sql server profiler lets you monitor almost everything that occurs on your server including deadlocks. In my opinion this is the easiest and most efficient tool to monitor and analyze deadlocks. With this tool we are able to see in a shape of a graph:</p>
<p dir="ltr">1. Which processes participate in the deadlock.</p>
<p dir="ltr">2. Which resources they already locked.</p>
<p dir="ltr">3. Which resource they are trying to lock.</p>
<p dir="ltr">4. Who was chosen to be the victim.</p>
<p dir="ltr">5. Transactions code.</p>
<p dir="ltr">All we got to do is:</p>
<p dir="ltr">1. Open the sql server profiler.</p>
<p dir="ltr">2. On the &quot;general&quot; tab at &quot;use the template&quot; row choose &quot;TSQL_Locks&quot;.</p>
<p dir="ltr">3. On the &quot;Event Selection&quot; tab mark only the &quot;deadlock Graph&quot; under locks section.</p>
<p dir="ltr">4. Run the trace and wait for the deadlock to occur.</p>
<p dir="ltr">Sql server profile example:</p>
<p dir="ltr"><a href="http://www.madeira.co.il/wp-content/uploads/2012/01/profiler.png" rel="wp-prettyPhoto[g3366]"><img class="alignleft size-full wp-image-3423" src="http://www.madeira.co.il/wp-content/uploads/2012/01/profiler.png" alt="" width="485" height="242" /></a></p>
<p dir="ltr">
<p dir="ltr">
<p dir="ltr">
<p dir="ltr">
<p dir="ltr">
<p dir="ltr">
<p dir="ltr">
<p dir="ltr">
<p dir="ltr">
<p dir="ltr">
<p dir="ltr">
<p dir="ltr">
<p dir="ltr">
<p dir="ltr">
<p dir="ltr">
<p dir="ltr">
<p dir="ltr">
<p dir="ltr">
<p dir="ltr">
<p dir="ltr">
<p dir="ltr">
<p dir="ltr">
<p dir="ltr">
<p dir="ltr">
<p dir="ltr">
<p dir="ltr">
<p dir="ltr">
<p dir="ltr">
<p dir="ltr">
<p dir="ltr">Note &#8211; It is important to remember to turn off all the traces you used after you finish because they use a lot of resources.</p>
<p dir="ltr">For conclusion, now that you know how to identify and monitor your server deadlocks its time to dig into the processes themselves and try to figure how to re-write or synchronize them in order to avoid any unnecessary deadlocks on your server.</p>
<p dir="ltr">
<p dir="ltr">Shtrum Tomer.</p>
</div>]]></content:encoded>			<wfw:commentRss>http://www.madeira.co.il/monitoring-deadlocks/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>

