
<?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>merill.net &#187; SQL</title>
	<atom:link href="http://merill.net/category/sql/feed/" rel="self" type="application/rss+xml" />
	<link>http://merill.net</link>
	<description>My utmost for His highest, my best for His glory</description>
	<lastBuildDate>Thu, 02 Feb 2012 21:02:26 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Deleting millions of records from a table without blowing the transaction log</title>
		<link>http://merill.net/2009/01/deleting-millions-of-records-from-a-table-without-blowing-the-transaction-log/</link>
		<comments>http://merill.net/2009/01/deleting-millions-of-records-from-a-table-without-blowing-the-transaction-log/#comments</comments>
		<pubDate>Fri, 02 Jan 2009 08:26:56 +0000</pubDate>
		<dc:creator>Merill Fernando</dc:creator>
				<category><![CDATA[SQL]]></category>

		<guid isPermaLink="false">/post/2009/01/Deleting-millions-of-records-from-a-table-without-blowing-the-transaction-log.aspx</guid>
		<description><![CDATA[Over the weekend I realized that deleting 80% of the records on a table with 87 million rows is not quite as easy as issuing a delete statement with a where criteria. The main reason is that the transaction log quickly grows as it needs to keep track of all the uncommitted rows that are [...]]]></description>
			<content:encoded><![CDATA[<p>Over the weekend I realized that deleting 80% of the records on a table with 87 million rows is not quite as easy as issuing a delete statement with a where criteria.</p>
<p>The main reason is that the transaction log quickly grows as it needs to keep track of all the uncommitted rows that are being deleted. In my case the database file was 60GB and the log file grew quickly from a mere 40MB to 32GB before I ran out of disk space and the database goes into recovery mode.</p>
<p>Browsing through blogs I narrowed my scenario to two options.</p>
<ol>
<li>Move the required records over to a temporary table and issue a truncate on the table you want to delete.</li>
<li>Issue the deletes in batches so that the log file doesn’t fill up.</li>
</ol>
<p>The first option seems to take the shorter time to execute but I didn’t want to go through the effort of removing and adding foreign keys&#160; and renaming tables. So I went with the second option. Here’s the script I used for SQL Server which deletes 10,000 rows at a time and commits them.</p>
<div>
<div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #0000ff">DECLARE</span> @<span style="color: #0000ff">continue</span> <span style="color: #0000ff">INT</span></pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #0000ff">DECLARE</span> @<span style="color: #0000ff">rowcount</span> <font color="#0000ff">INT</font></pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">&#160;</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #0000ff">SET</span> @<span style="color: #0000ff">continue</span> = 1</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #0000ff">WHILE</span> @<span style="color: #0000ff">continue</span> = 1</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #0000ff">BEGIN</span></pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">PRINT</span> GETDATE()</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    <span style="color: #0000ff">SET</span> <span style="color: #0000ff">ROWCOUNT</span> 10000</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">BEGIN</span> <span style="color: #0000ff">TRANSACTION</span></pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    <span style="color: #0000ff">DELETE</span> <span style="color: #0000ff">FROM</span> Transactions <span style="color: #0000ff">WHERE</span>  TradeDate <span style="color: #0000ff">IS</span> <span style="color: #0000ff">NULL</span></pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">SET</span> @<span style="color: #0000ff">rowcount</span> = <span style="color: #cc6633">@@rowcount</span> </pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    <span style="color: #0000ff">COMMIT</span></pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">PRINT</span> GETDATE()</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    <span style="color: #0000ff">IF</span> @<span style="color: #0000ff">rowcount</span> = 0</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">BEGIN</span></pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        <span style="color: #0000ff">SET</span> @<span style="color: #0000ff">continue</span> = 0</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">END</span></pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #0000ff">END</span></pre>
</p></div>
</div>
<p>The print statements help to show you how things are moving along.</p>
<p><font face="Courier New" color="#000080" size="1">(10000 row(s) affected)<br />
    <br />Jan&#160; 1 2009 11:54PM</p>
<p>Jan&#160; 1 2009 11:54PM </font></p>
<p><font face="Courier New" color="#000080" size="1">(10000 row(s) affected)<br />
    <br />Jan&#160; 1 2009 11:55PM</p>
<p>Jan&#160; 1 2009 11:55PM</font></p>
<p>Seven hours later the script is still executing with 39 million rows deleted so far and the log file currently at 700MB.</p>
]]></content:encoded>
			<wfw:commentRss>http://merill.net/2009/01/deleting-millions-of-records-from-a-table-without-blowing-the-transaction-log/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Viewing the disk space usage (size) of all the tables in a SQL Server 2005/2008 database</title>
		<link>http://merill.net/2008/12/viewing-the-disk-space-usage-size-of-all-the-tables-in-a-sql-server-20052008-database/</link>
		<comments>http://merill.net/2008/12/viewing-the-disk-space-usage-size-of-all-the-tables-in-a-sql-server-20052008-database/#comments</comments>
		<pubDate>Wed, 31 Dec 2008 06:49:11 +0000</pubDate>
		<dc:creator>Merill Fernando</dc:creator>
				<category><![CDATA[SQL]]></category>

		<guid isPermaLink="false">/post/2008/12/Viewing-the-disk-space-usage-(size)-of-all-the-tables-in-a-SQL-Server-20052008-database.aspx</guid>
		<description><![CDATA[I spent a couple of hours searching for a script that returns the size of all the tables in a database. Most of the ones that you find use sp_msforeachtable but that is just for one table and you then need to use temporary tables to hold the values. &#160; This article on the Database [...]]]></description>
			<content:encoded><![CDATA[<div>I spent a couple of hours searching for a script that returns the size of all the tables in a database. Most of the ones that you find use sp_msforeachtable but that is just for one table and you then need to use temporary tables to hold the values.</div>
<div>&#160;</div>
<div>This <a href="http://www.databasejournal.com/features/mssql/article.php/3733996/Find-space-Usage-by-Table--Schema-in-SQL-Server-2005-and-2008.htm">article</a> on the Database Journal by Ananth Kumar (AKA MAK) had this handy script that simply works by copy-pasting it as is. Read the linked article for details on viewing the size by schema.</div>
<div>&#160;</div>
<div>
<div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #0000ff">BEGIN</span> TRY </pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #0000ff">SELECT</span> </pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    (row_number() <span style="color: #0000ff">over</span>(<span style="color: #0000ff">order</span> <span style="color: #0000ff">by</span> a3.name, a2.name))%2 <span style="color: #0000ff">as</span> l1,</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    a3.name <span style="color: #0000ff">AS</span> [schemaname],</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    a2.name <span style="color: #0000ff">AS</span> [tablename],</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    a1.<span style="color: #0000ff">rows</span> <span style="color: #0000ff">as</span> row_count,</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    (a1.reserved + ISNULL(a4.reserved,0))* 8 <span style="color: #0000ff">AS</span> reserved, </pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    a1.<span style="color: #0000ff">data</span> * 8 <span style="color: #0000ff">AS</span> <span style="color: #0000ff">data</span>,</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    (<span style="color: #0000ff">CASE</span> <span style="color: #0000ff">WHEN</span> (a1.used + ISNULL(a4.used,0)) &gt; a1.<span style="color: #0000ff">data</span> <span style="color: #0000ff">THEN</span> </pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    (a1.used + ISNULL(a4.used,0)) - a1.<span style="color: #0000ff">data</span> <span style="color: #0000ff">ELSE</span> 0 <span style="color: #0000ff">END</span>) * 8 <span style="color: #0000ff">AS</span> index_size,</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    (<span style="color: #0000ff">CASE</span> <span style="color: #0000ff">WHEN</span> (a1.reserved + ISNULL(a4.reserved,0)) &gt; a1.used <span style="color: #0000ff">THEN</span> </pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    (a1.reserved + ISNULL(a4.reserved,0)) - a1.used <span style="color: #0000ff">ELSE</span> 0 <span style="color: #0000ff">END</span>) * 8 <span style="color: #0000ff">AS</span> unused</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #0000ff">FROM</span></pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"> (<span style="color: #0000ff">SELECT</span> </pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">  ps.object_id,</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">  <span style="color: #0000ff">SUM</span> (</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">   <span style="color: #0000ff">CASE</span></pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    <span style="color: #0000ff">WHEN</span> (ps.index_id &lt; 2) <span style="color: #0000ff">THEN</span> row_count</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">ELSE</span> 0</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">   <span style="color: #0000ff">END</span></pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">   ) <span style="color: #0000ff">AS</span> [<span style="color: #0000ff">rows</span>],</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">  <span style="color: #0000ff">SUM</span> (ps.reserved_page_count) <span style="color: #0000ff">AS</span> reserved,</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">  <span style="color: #0000ff">SUM</span> (</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">   <span style="color: #0000ff">CASE</span></pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">     <span style="color: #0000ff">WHEN</span> (ps.index_id &lt; 2) <span style="color: #0000ff">THEN</span> </pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">   (ps.in_row_data_page_count + ps.lob_used_page_count + ps.row_overflow_used_page_count)</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">     <span style="color: #0000ff">ELSE</span> (ps.lob_used_page_count + ps.row_overflow_used_page_count)</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">   <span style="color: #0000ff">END</span></pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">   ) <span style="color: #0000ff">AS</span> <span style="color: #0000ff">data</span>,</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">  <span style="color: #0000ff">SUM</span> (ps.used_page_count) <span style="color: #0000ff">AS</span> used</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"> <span style="color: #0000ff">FROM</span> sys.dm_db_partition_stats ps</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"> <span style="color: #0000ff">GROUP</span> <span style="color: #0000ff">BY</span> ps.object_id) <span style="color: #0000ff">AS</span> a1</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #0000ff">LEFT</span> <span style="color: #0000ff">OUTER</span> <span style="color: #0000ff">JOIN</span> </pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"> (<span style="color: #0000ff">SELECT</span> </pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">  it.parent_id,</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">  <span style="color: #0000ff">SUM</span>(ps.reserved_page_count) <span style="color: #0000ff">AS</span> reserved,</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">  <span style="color: #0000ff">SUM</span>(ps.used_page_count) <span style="color: #0000ff">AS</span> used</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">  <span style="color: #0000ff">FROM</span> sys.dm_db_partition_stats ps</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">  <span style="color: #0000ff">INNER</span> <span style="color: #0000ff">JOIN</span> sys.internal_tables it <span style="color: #0000ff">ON</span> (it.object_id = ps.object_id)</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">  <span style="color: #0000ff">WHERE</span> it.internal_type <span style="color: #0000ff">IN</span> (202,204)</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">  <span style="color: #0000ff">GROUP</span> <span style="color: #0000ff">BY</span> it.parent_id) <span style="color: #0000ff">AS</span> a4 <span style="color: #0000ff">ON</span> (a4.parent_id = a1.object_id)</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #0000ff">INNER</span> <span style="color: #0000ff">JOIN</span> sys.all_objects a2  <span style="color: #0000ff">ON</span> ( a1.object_id = a2.object_id ) </pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #0000ff">INNER</span> <span style="color: #0000ff">JOIN</span> sys.schemas a3 <span style="color: #0000ff">ON</span> (a2.schema_id = a3.schema_id)</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #0000ff">WHERE</span> a2.type &lt;&gt; <span style="color: #006080">'S'</span> <span style="color: #0000ff">and</span> a2.type &lt;&gt; <span style="color: #006080">'IT'</span></pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #0000ff">ORDER</span> <span style="color: #0000ff">BY</span> a3.name, a2.name</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #0000ff">END</span> TRY </pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #0000ff">BEGIN</span> CATCH </pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #0000ff">SELECT</span> </pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"> -100 <span style="color: #0000ff">as</span> l1</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">, 1 <span style="color: #0000ff">as</span> schemaname </pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">,       ERROR_NUMBER() <span style="color: #0000ff">as</span> tablename</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">,       ERROR_SEVERITY() <span style="color: #0000ff">as</span> row_count</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">,       ERROR_STATE() <span style="color: #0000ff">as</span> reserved</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">,       ERROR_MESSAGE() <span style="color: #0000ff">as</span> <span style="color: #0000ff">data</span></pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">,       1 <span style="color: #0000ff">as</span> index_size</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">,   1 <span style="color: #0000ff">as</span> unused </pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #0000ff">END</span> CATCH</pre>
</p></div>
</div>
<p>&#160;</p>
<p>Quick extra tip, if you export the result-set to Excel you can do additional sorts to figure out more stuff. For example the first table on this list was taking up 47GB of disk space in a 63 GB database. With the index taking more space than the actual data! But this is more of an OLAP db with just one batch update daily and optimized for retrieving data faster so I guess that&#8217;s okay.</p>
<p><a href="http://www.merill.net/wp-content/uploads/files/WindowsLiveWriter/Viewingthediskspaceusageofallthetablesin_6829/sql-disk-space-usage.jpg" rel="lightbox"><img title="sql-disk-space-usage" style="border-right: 0px; border-top: 0px; display: inline; border-left: 0px; border-bottom: 0px" height="141" alt="sql-disk-space-usage" src="http://www.merill.net/wp-content/uploads/files/WindowsLiveWriter/Viewingthediskspaceusageofallthetablesin_6829/sql-disk-space-usage_thumb.jpg" width="667" border="0" /></a> </p>
<p>&#160;</p>
<style type="text/css">
<p>.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }</style>
]]></content:encoded>
			<wfw:commentRss>http://merill.net/2008/12/viewing-the-disk-space-usage-size-of-all-the-tables-in-a-sql-server-20052008-database/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>SQL query optimization</title>
		<link>http://merill.net/2006/11/sql-query-optimization/</link>
		<comments>http://merill.net/2006/11/sql-query-optimization/#comments</comments>
		<pubDate>Fri, 03 Nov 2006 12:29:11 +0000</pubDate>
		<dc:creator>Merill Fernando</dc:creator>
				<category><![CDATA[SQL]]></category>

		<guid isPermaLink="false">/post/2006/11/SQL-query-optimization.aspx</guid>
		<description><![CDATA[Wow. Two SQL posts on the same day. The SQL query processing team blog is quite a gem. I wonder how I missed this. The query processing team &#8211; query optimization &#38; execution &#8211; providing tips, tricks, advice and answers to frequently-asked questions in a continued effort to make your queries run faster and smoother [...]]]></description>
			<content:encoded><![CDATA[<p>Wow. Two SQL posts on the same day.</p>
<p>The SQL query processing team <a href="http://blogs.msdn.com/sqlqueryprocessing/default.aspx">blog</a> is quite a gem. I wonder how I missed this.</p>
<blockquote dir="ltr" style="MARGIN-RIGHT: 0px">
<p>The query processing team &#8211; query optimization &amp; execution &#8211; providing tips, tricks, advice and answers to frequently-asked questions in a continued effort to make your queries run faster and smoother in SQL Server.</p>
</blockquote>
<p dir="ltr">BTW: I consider <a href="http://msdn2.microsoft.com/en-us/library/ms190623.aspx">this short article</a> a must read for everyone who writes a SELECT query. The reason is that we never learn about the query optimizer in school when learning about databases.</p>
<p dir="ltr">Let me prove this to you. My best friend Shasheendra&nbsp;wrote this code the other day&nbsp;and it stumped me.&nbsp;Imagine this, you want to insert 10,000 rows into a temporary table.&nbsp; Let&rsquo;s say we can take two approaches. The first one is to simply write 10,000 inserts the second could be to&nbsp;insert the entries into a&nbsp;table with a single field with&nbsp;the 10,000 being comma delimited. Then parse the string and&nbsp;insert each row at a time. </p>
<p dir="ltr">Which do you think runs faster? No hurries, just take a look at the code and then scroll down to see the time comparison.</p>
<p dir="ltr"><strong>Approach 1: </strong></p>
<p dir="ltr">Write 10,000 insert statements. Like this</p>
<p><font color="#0000ff" size="2"></p>
<p>CREATE</font><font size="2"> </font><font color="#0000ff" size="2">TABLE</font><font size="2"> #filetab </font><font color="#808080" size="2">(</font><font color="#0000ff" size="2">filename</font><font size="2"> </font><font color="#0000ff" size="2">varchar</font><font color="#808080" size="2">(</font><font size="2">256</font><font color="#808080" size="2">))</font><font size="2"> </p>
<p></font><font color="#0000ff" size="2"></p>
<p>CREATE</font><font size="2"> </font><font color="#0000ff" size="2">CLUSTERED</font><font size="2"> </font><font color="#0000ff" size="2">INDEX</font><font size="2"> IX_filetab </font><font color="#0000ff" size="2">ON</font><font size="2"> #filetab </font><font color="#808080" size="2">(</font><font color="#0000ff" size="2">filename</font><font color="#808080" size="2">)</p>
<p></font><font color="#0000ff" size="2"></p>
<p>INSERT</font><font size="2"> </font><font color="#0000ff" size="2">INTO</font><font size="2"> #filetab </font><font color="#0000ff" size="2">VALUES</font><font size="2"> </font><font color="#808080" size="2">(</font><font color="#ff0000" size="2">&#8216;dummy&#8217;</font><font color="#808080" size="2">)</p>
<p></font><font color="#0000ff" size="2"></p>
<p><strong>INSERT</strong></font><strong><font size="2"> </font><font color="#0000ff" size="2">INTO</font><font size="2"> #filetab </font><font color="#0000ff" size="2">VALUES</font><font size="2"> </font><font color="#808080" size="2">(</font><font color="#ff0000" size="2">&#8216;ed7d1eda-025b-41be-b8b4-523dc9d53ece&#8217;</font><font color="#808080" size="2">)</p>
<p></font></strong><font color="#0000ff" size="2"></p>
<p>INSERT</font><font size="2"> </font><font color="#0000ff" size="2">INTO</font><font size="2"> #filetab </font><font color="#0000ff" size="2">VALUES</font><font size="2"> </font><font color="#808080" size="2">(</font><font color="#ff0000" size="2">&#8216;b61abf2e-3252-423c-9ad7-cc63cb5e51bc&#8217;</font><font color="#808080" size="2">)</p>
<p></font><font color="#0000ff" size="2"></p>
<p>INSERT</font><font size="2"> </font><font color="#0000ff" size="2">INTO</font><font size="2"> #filetab </font><font color="#0000ff" size="2">VALUES</font><font size="2"> </font><font color="#808080" size="2">(</font><font color="#ff0000" size="2">&#8217;4224b7f8-120a-447b-a8ae-63cefce78255&#8242;</font><font color="#808080" size="2">)</font></p>
<p><font color="#808080" size="2"><font color="#0000ff" size="2"></p>
<p>&hellip;.(10,000 entries)&hellip;</p>
<p>INSERT</font><font color="#000000" size="2"> </font><font color="#0000ff" size="2">INTO</font><font color="#000000" size="2"> #filetab </font><font color="#0000ff" size="2">VALUES</font><font color="#000000" size="2"> </font><font color="#808080" size="2">(</font><font color="#ff0000" size="2">&#8217;7f38bed5-be32-42c8-986f-20b3c78950ad&#8217;</font><font color="#808080" size="2">)</p>
<p></font><font color="#0000ff" size="2"></p>
<p>SELECT</font><font color="#000000" size="2"> </font><font color="#ff00ff" size="2">COUNT</font><font color="#808080" size="2">(*)</font><font color="#000000" size="2"> </font><font color="#0000ff" size="2">FROM</font><font size="2"><font color="#000000"> #filetab</font></p>
<p></font><font color="#0000ff" size="2"></p>
<p>DROP</font><font color="#000000" size="2"> </font><font color="#0000ff" size="2">TABLE</font><font size="2"><font color="#000000"> #filetab</font></p>
<p>GO</p>
<p></font></font></p>
<p dir="ltr"><strong>Approach 2:</strong></p>
<p><font color="#0000ff" size="2"></p>
<p>DECLARE</font><font size="2"> @filelisttab </font><font color="#0000ff" size="2">TABLE</font><font size="2"> </font><font color="#808080" size="2">(</font><font size="2">filelist </font><font color="#0000ff" size="2">text</font><font color="#808080" size="2">)</p>
<p></font><font color="#0000ff" size="2"></p>
<p>CREATE</font><font size="2"> </font><font color="#0000ff" size="2">TABLE</font><font size="2"> #filetab </font><font color="#808080" size="2">(</font><font color="#0000ff" size="2">filename</font><font size="2"> </font><font color="#0000ff" size="2">varchar</font><font color="#808080" size="2">(</font><font size="2">256</font><font color="#808080" size="2">))</font><font size="2"> </p>
<p></font><font color="#0000ff" size="2"></p>
<p>CREATE</font><font size="2"> </font><font color="#0000ff" size="2">CLUSTERED</font><font size="2"> </font><font color="#0000ff" size="2">INDEX</font><font size="2"> IX_filetab </font><font color="#0000ff" size="2">ON</font><font size="2"> #filetab </font><font color="#808080" size="2">(</font><font color="#0000ff" size="2">filename</font><font color="#808080" size="2">)</p>
<p></font><font color="#0000ff" size="2"></p>
<p>INSERT</font><font size="2"> </font><font color="#0000ff" size="2">INTO</font><font size="2"> @filelisttab </font><font color="#808080" size="2">(</font><font size="2">filelist</font><font color="#808080" size="2">)</font><font size="2"> </font><font color="#0000ff" size="2">VALUES</font><font size="2"> </font><font color="#808080" size="2">(</font><font color="#ff0000" size="2">&#8216;dummy,ed7d1eda-025b-41be-b8b4-523dc9d53ece,b61abf2e <font color="#0000ff">&hellip;.(10,000 comma seperated entries)&hellip;<font color="#ff0000" size="2">,7f38bed5-be32-42c8-986f-20b3c78950ad&#8217;</font><font color="#808080" size="2">)</p>
<p></font></font></font><font color="#0000ff" size="2"></p>
<p>DECLARE</font><font size="2"> @i </font><font color="#0000ff" size="2">int</font><font color="#808080" size="2">,</font><font size="2"> @l </font><font color="#0000ff" size="2">int</font><font color="#808080" size="2">,</font><font size="2"> @tl </font><font color="#0000ff" size="2">int</p>
<p>SET</font><font size="2"> @i </font><font color="#808080" size="2">=</font><font size="2"> 1</p>
<p></font><font color="#0000ff" size="2"></p>
<p>SELECT</font><font size="2"> @tl </font><font color="#808080" size="2">=</font><font size="2"> </font><font color="#ff00ff" size="2">DATALENGTH</font><font color="#808080" size="2">(</font><font size="2">filelist</font><font color="#808080" size="2">)</font><font size="2"> </font><font color="#0000ff" size="2">FROM</font><font size="2"> @filelisttab</p>
<p></font><font color="#0000ff" size="2"></p>
<p>WHILE</font><font size="2"> </font><font color="#808080" size="2">(</font><font size="2">@i </font><font color="#808080" size="2">&lt;</font><font size="2"> @tl</font><font color="#808080" size="2">)</p>
<p></font><font color="#0000ff" size="2"></p>
<p>BEGIN</p>
<p></font><font size="2"></p>
<p></font><font color="#0000ff" size="2">&nbsp;&nbsp;&nbsp; DECLARE</font><font size="2"> @filename </font><font color="#0000ff" size="2">Varchar</font><font color="#808080" size="2">(</font><font size="2">256</font><font color="#808080" size="2">)</p>
<p></font><font size="2"></p>
<p></font><font color="#0000ff" size="2">&nbsp;&nbsp;&nbsp; DECLARE</font><font size="2"> @nextcommat </font><font color="#0000ff" size="2">int</p>
<p></font><font size="2"></p>
<p></font><font color="#0000ff" size="2">&nbsp;&nbsp;&nbsp; SELECT</font><font size="2"> @filename </font><font color="#808080" size="2">=</font><font size="2"> </font><font color="#ff00ff" size="2">SUBSTRING</font><font color="#808080" size="2">(</font><font size="2">filelist</font><font color="#808080" size="2">,</font><font size="2"> @i</font><font color="#808080" size="2">,</font><font size="2"> 128</font><font color="#808080" size="2">)</font><font size="2"> </font><font color="#0000ff" size="2">FROM</font><font size="2"> @filelisttab </font><font color="#008000" size="2">&#8211; Use 128 character read buffer!</p>
<p></font><font size="2"></p>
<p></font><font color="#0000ff" size="2">&nbsp;&nbsp;&nbsp; SET</font><font size="2"> @nextcommat </font><font color="#808080" size="2">=</font><font size="2"> </font><font color="#ff00ff" size="2">CHARINDEX</font><font color="#808080" size="2">(</font><font color="#ff0000" size="2">&#8216;,&#8217;</font><font color="#808080" size="2">,</font><font size="2"> @filename</font><font color="#808080" size="2">)</p>
<p></font><font size="2"></p>
<p></font><font color="#0000ff" size="2">&nbsp;&nbsp;&nbsp; SET</font><font size="2"> @l </font><font color="#808080" size="2">=</font><font size="2"> </font><font color="#0000ff" size="2">CASE</font><font size="2"> @nextcommat </font><font color="#0000ff" size="2">WHEN</font><font size="2"> 0 </font><font color="#0000ff" size="2">THEN</font><font size="2"> 128 </font><font color="#0000ff" size="2">ELSE</font><font size="2"> </font><font color="#808080" size="2">(</font><font size="2">@nextcommat</font><font color="#808080" size="2">-</font><font size="2">1</font><font color="#808080" size="2">)</font><font size="2"> </font><font color="#0000ff" size="2">END</p>
<p></font><font size="2"></p>
<p></font><font color="#0000ff" size="2">&nbsp;&nbsp;&nbsp; SET</font><font size="2"> @filename </font><font color="#808080" size="2">=</font><font size="2"> </font><font color="#ff00ff" size="2">SUBSTRING</font><font color="#808080" size="2">(</font><font size="2">@filename</font><font color="#808080" size="2">,</font><font size="2"> 1</font><font color="#808080" size="2">,</font><font size="2"> @l</font><font color="#808080" size="2">)</p>
<p></font><font size="2"></p>
<p></font><font color="#0000ff" size="2">&nbsp;&nbsp;&nbsp; IF</font><font size="2"> </font><font color="#808080" size="2">(</font><font color="#ff00ff" size="2">LEN</font><font color="#808080" size="2">(</font><font size="2">@filename</font><font color="#808080" size="2">)&gt;</font><font size="2">0</font><font color="#808080" size="2">)</font><font size="2"> </font><strong><font color="#0000ff" size="2">INSERT</font><font size="2"> </font><font color="#0000ff" size="2">INTO</font><font size="2"> #filetab </font><font color="#808080" size="2">(</font><font color="#0000ff" size="2">filename</font><font color="#808080" size="2">)</font><font size="2"> </font><font color="#0000ff" size="2">VALUES</font><font size="2"> </font><font color="#808080" size="2">(</font><font size="2">@filename</font><font color="#808080" size="2">)</p>
<p></font></strong><font size="2"></p>
<p></font><font color="#0000ff" size="2">&nbsp;&nbsp;&nbsp; SET</font><font size="2"> @i </font><font color="#808080" size="2">=</font><font size="2"> @i </font><font color="#808080" size="2">+</font><font size="2"> @l </font><font color="#808080" size="2">+</font><font size="2"> 1</p>
<p></font><font color="#0000ff" size="2"></p>
<p>END</p>
<p>SELECT</font><font size="2"> </font><font color="#ff00ff" size="2">COUNT</font><font color="#808080" size="2">(*)</font><font size="2"> </font><font color="#0000ff" size="2">FROM</font><font size="2"> #filetab</p>
<p></font><font color="#0000ff" size="2"></p>
<p>DROP</font><font size="2"> </font><font color="#0000ff" size="2">TABLE</font><font size="2"> #filetab</p>
<p dir="ltr"></font><a href="http://www.merill.net/wp-content/uploads/binary/Query1.sql">Query1.sql (722 KB)</a></p>
<p dir="ltr"><a href="http://www.merill.net/wp-content/uploads/binary/Query2.sql">Query2.sql (401 KB)</a></p>
<p dir="ltr">&nbsp;</p>
<p dir="ltr">If you don&rsquo;t believe me, you can download the two sql statements and try running them on your machine. These scripts create a temporary table, insert data and then drop the table.</p>
<p dir="ltr"><em>Query 1&nbsp;: First run 2 min 48 seconds, second run 2 min 28 seconds</em></p>
<p dir="ltr"><em>Query 2:&nbsp;&nbsp;First run 9 seconds, second run 5 seconds</em></p>
<p dir="ltr">And here I was thinking all these days that I knew all the basics on SQL query optimization.</p>
]]></content:encoded>
			<wfw:commentRss>http://merill.net/2006/11/sql-query-optimization/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Publishing your SQL Server 2005 database?</title>
		<link>http://merill.net/2006/11/publishing-your-sql-server-2005-database/</link>
		<comments>http://merill.net/2006/11/publishing-your-sql-server-2005-database/#comments</comments>
		<pubDate>Fri, 03 Nov 2006 11:16:53 +0000</pubDate>
		<dc:creator>Merill Fernando</dc:creator>
				<category><![CDATA[SQL]]></category>

		<guid isPermaLink="false">/post/2006/11/Publishing-your-SQL-Server-2005-database.aspx</guid>
		<description><![CDATA[Ever wanted to MySQL feature that let&#8217;s you script out a database (including the data) when working with SQL Server? Fear no more the Database Publishing Wizard is here to help. The Database Publishing Wizard enables the deployment of SQL Server 2005 databases into a hosted environment on either a SQL Server 2000 or 2005 [...]]]></description>
			<content:encoded><![CDATA[<p>Ever wanted to MySQL feature that let&rsquo;s you script out a database (including the data) when working with SQL Server?</p>
<p>Fear no more the <a href="http://www.codeplex.com/Wiki/View.aspx?ProjectName=sqlhost&amp;title=Database%20Publishing%20Wizard">Database Publishing Wizard</a> is here to help. </p>
<blockquote dir="ltr" style="MARGIN-RIGHT: 0px">
<p>The Database Publishing Wizard enables the deployment of SQL Server 2005 databases into a hosted environment on either a SQL Server 2000 or 2005 server. It generates a SQL script file which can be used to recreate the database in shared hosting environments where the only connectivity to a server is through a web-based control panel with a scripting window.</p>
<p>The Database Publishing Wizard is currently a command line tool, though future releases will add a graphical user interface.</p>
</blockquote>
<p dir="ltr">Get the download from <a href="http://www.codeplex.com/Wiki/View.aspx?ProjectName=sqlhost&amp;title=Database%20Publishing%20Wizard">here</a>.&nbsp;Keep in mind that it&rsquo;s still a CTP and is being developed actively. </p>
]]></content:encoded>
			<wfw:commentRss>http://merill.net/2006/11/publishing-your-sql-server-2005-database/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Database diagram problems on a database migrated from SQL Server 2005</title>
		<link>http://merill.net/2006/04/database-diagram-problems-on-a-database-migrated-from-sql-server-2005/</link>
		<comments>http://merill.net/2006/04/database-diagram-problems-on-a-database-migrated-from-sql-server-2005/#comments</comments>
		<pubDate>Thu, 27 Apr 2006 02:00:37 +0000</pubDate>
		<dc:creator>Merill Fernando</dc:creator>
				<category><![CDATA[SQL]]></category>

		<guid isPermaLink="false">/post/2006/04/Database-diagram-problems-on-a-database-migrated-from-SQL-Server-2005.aspx</guid>
		<description><![CDATA[I ran into a wierd error message when trying to view the Diagrams on a database that was migrated to SQL Server 2005.&#160;Something about the user not being valid and to change the database owner. A quick search brought up this quick fix: Try running this command to&#160;change the compatibility level to SQL Server 2005. [...]]]></description>
			<content:encoded><![CDATA[<p>I ran into a wierd error message when trying to view the Diagrams on a database that was migrated to SQL Server 2005.&nbsp;Something about the user not being valid and to change the database owner. </p>
<p>A quick search brought up this quick fix:</p>
<p>Try running this command to&nbsp;change the compatibility level to SQL Server 2005.</p>
<p><font color="#0000ff" size="1"></p>
<p><font face="Lucida Console" size="2">EXEC</font></font><font face="Lucida Console" size="2"> <font color="#800000">sp_dbcmptlevel</font> database_name<font color="#808080">,</font> 90 </font></p>
<p>Works like a charm! [<a href="http://msdn2.microsoft.com/en-us/library/ms186345.aspx">SQL BOL</a>]</p>
]]></content:encoded>
			<wfw:commentRss>http://merill.net/2006/04/database-diagram-problems-on-a-database-migrated-from-sql-server-2005/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Use scope_identity() instead of @@identity</title>
		<link>http://merill.net/2003/11/use-scope_identity-instead-of-identity/</link>
		<comments>http://merill.net/2003/11/use-scope_identity-instead-of-identity/#comments</comments>
		<pubDate>Sun, 02 Nov 2003 13:15:10 +0000</pubDate>
		<dc:creator>Merill Fernando</dc:creator>
				<category><![CDATA[SQL]]></category>

		<guid isPermaLink="false">/post/2003/11/Use-scope_identity()-instead-of-identity.aspx</guid>
		<description><![CDATA[TravisL explains why we should use scope_identity instead of @@identity when we want to get the ID of the last record that was added. The problem with @@identity is that it returns the ID of the last record that was in the current connection. In which case if the table that you adding to has [...]]]></description>
			<content:encoded><![CDATA[<p><body xmlns="http://www.w3.org/1999/xhtml"></p>
<p>
        <a href="http://weblogs.sqlteam.com/travisl/">TravisL</a> <a href="http://weblogs.sqlteam.com/travisl/posts/405.aspx">explains</a> why<br />
        we should use scope_identity instead of @@identity when we want to get the ID of the<br />
        last record that was added.
    </p>
<p>
        The problem with @@identity is that it returns the ID of the last record that was<br />
        in the current connection. In which case if the table that you adding to has triggers<br />
        (which inserts records in other tables) then @@identity returns the ID of the last<br />
        record that was added by code inside the trigger. This becomes a potentially very<br />
        difficult bug to track down.
    </p>
<p>
        So what should you do? Use scope_identity() which returns the ID of the last record<br />
        that was inserted in the current scope. See&#160;<a href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/tsqlref/ts_sa-ses_6n8p.asp">MSDN:scope_identity()</a>&#160;for<br />
        detailed examples.&#160;
    </p>
<p></body></p>
]]></content:encoded>
			<wfw:commentRss>http://merill.net/2003/11/use-scope_identity-instead-of-identity/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

