<?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/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Tech Crumb</title>
	<atom:link href="http://sarosh.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://sarosh.wordpress.com</link>
	<description>Crumbs collected along the way  ....</description>
	<lastBuildDate>Wed, 30 Sep 2009 19:30:57 +0000</lastBuildDate>
	<generator>http://wordpress.com/</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<cloud domain='sarosh.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://www.gravatar.com/blavatar/352d4b1f09fa5f71194b178d26807c02?s=96&#038;d=http://s.wordpress.com/i/buttonw-com.png</url>
		<title>Tech Crumb</title>
		<link>http://sarosh.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://sarosh.wordpress.com/osd.xml" title="Tech Crumb" />
		<item>
		<title>XMLTextWriter v/s XElement</title>
		<link>http://sarosh.wordpress.com/2009/09/29/xmltextwriter-vs-xelement/</link>
		<comments>http://sarosh.wordpress.com/2009/09/29/xmltextwriter-vs-xelement/#comments</comments>
		<pubDate>Tue, 29 Sep 2009 08:19:00 +0000</pubDate>
		<dc:creator>Rafat Sarosh</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[LINQ]]></category>
		<category><![CDATA[XML]]></category>

		<guid isPermaLink="false">http://sarosh.wordpress.com/2009/09/29/xmltextwriter-vs-xelement/</guid>
		<description><![CDATA[I have to move some data from Database to a XML file. A very simple task, write a SQL query, get the result and put it into a XML File. So here is my data query


DownloadDataContext db = new DownloadDataContext();
IQueryable downloads =
      from f in db.Files
      join fr in db.FamilyReleases
          on f.ReleaseID equals fr.ReleaseID 
      join [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sarosh.wordpress.com&blog=1119187&post=137&subd=sarosh&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>I have to move some data from Database to a XML file. A very simple task, write a SQL query, get the result and put it into a XML File. So here is my data query</p>
<pre class="brush: csharp;">

DownloadDataContext db = new DownloadDataContext();
IQueryable downloads =
      from f in db.Files
      join fr in db.FamilyReleases
          on f.ReleaseID equals fr.ReleaseID 
      join fdl in  db.FamilyDetailsByLocales
          on fr.FamilyID equals fdl.FamilyID
              select new DownloadDetails{
                FamilyID = fdl.FamilyID,
                FileName = f.FileName,
                FileSize = f.FileSize, URL = f.URL
)
</pre>
<p>Once I got the data in IQueryable downloads, Out of old habit, I proceeded to make the XML as follows:</p>
<pre class="brush: csharp;">
MemoryStream ms = new MemoryStream();
using (XmlTextWriter w = new XmlTextWriter( ms, Encoding.UTF8 )) {
     w.Formatting = Formatting.Indented;
     w.WriteStartElement(&quot;Products&quot;);
           foreach (DownloadDetails d in downloads) { 
          w.WriteStartElement(&quot;FamilyID&quot;); w.WriteValue(d.FamilyID.ToString());                                

w.WriteEndElement(); w.WriteStartElement(&quot;FileName&quot;); w.WriteValue(d.FileName ); w.WriteEndElement(); w.WriteStartElement(&quot;FileSize&quot;); w.WriteValue(d.FileSize); w.WriteEndElement(); w.WriteStartElement(&quot;URL&quot;); w.WriteValue(d.URL); w.WriteEndElement(); }
w.WriteEndElement(); //products
}
StreamReader r = new StreamReader(ms,Encoding.UTF8);
ms.Seek(0, SeekOrigin.Begin);
return r.ReadToEnd();
</pre>
<p>Very simple, However when I check the returned XML string,  it is always cut in between and a proper XML is not formed. As the data is localized there may be a possibility that there is some funny character came in between and stream reader <em>ReadToEnd </em>function got confused. Debugging with 40+ languages in data was a nightmare, and I had no clue why streamreader is not working as it is suppose to work. There were other problems too, If any of the values are null, <em>TextWriter.WriteValue</em> complain very loudly. So every where I have to put a check to see if the value is null or not. However, the XElement came to my rescue. XElement does take all the possible data types, and does not complain if any of the data type is null.</p>
<pre class="brush: csharp;">

XElement x = new XElement(&quot;Products&quot;);
foreach (DownloadDetails d in downloads) {
x.Add(new XElement(&quot;Product&quot;,
new XElement(&quot;FamilyID&quot;, d.FamilyID.ToString()),
new XElement(&quot;FileName&quot;, d.FileName),
new XElement(&quot;FileSize&quot;, d.FileSize),
new XElement(&quot;URL&quot;, d.URL + &quot;&quot;))); }
</pre>
<p>XElement not only handles NULL gracefully but does not complain about any of the unknown characters which threw StreamReader off. Clearly XElement is a better choice to make  XML out of data, <a href="http://sarosh.wordpress.com/2009/06/17/access-mdb-to-xml-with-linq/" target="_blank">here </a>is another example of XElement use.</p>
  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sarosh.wordpress.com/137/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sarosh.wordpress.com/137/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sarosh.wordpress.com/137/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sarosh.wordpress.com/137/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sarosh.wordpress.com/137/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sarosh.wordpress.com/137/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sarosh.wordpress.com/137/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sarosh.wordpress.com/137/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sarosh.wordpress.com/137/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sarosh.wordpress.com/137/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sarosh.wordpress.com&blog=1119187&post=137&subd=sarosh&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://sarosh.wordpress.com/2009/09/29/xmltextwriter-vs-xelement/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/d8e93bef927d029c824b91da0b59f703?s=96&#38;d=http%3A%2F%2F1.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96&#38;r=G" medium="image">
			<media:title type="html">rsarosh</media:title>
		</media:content>
	</item>
		<item>
		<title>Difference between facade and Decorator Pattern</title>
		<link>http://sarosh.wordpress.com/2009/09/22/difference-between-facade-and-decorator-pattern/</link>
		<comments>http://sarosh.wordpress.com/2009/09/22/difference-between-facade-and-decorator-pattern/#comments</comments>
		<pubDate>Tue, 22 Sep 2009 23:52:26 +0000</pubDate>
		<dc:creator>Rafat Sarosh</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[Pattern]]></category>

		<guid isPermaLink="false">http://sarosh.wordpress.com/2009/09/22/difference-between-facade-and-decorator-pattern/</guid>
		<description><![CDATA[Yesterday, I was discussing with our friends about pattern and differences between facade and decorator pattern? Facade gives the impression that your create a facade object over different desperate objects, or you actually decorate them with another layer, or they are two different name for the same pattern etc. All kind of confusion was there, [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sarosh.wordpress.com&blog=1119187&post=129&subd=sarosh&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>Yesterday, I was discussing with our friends about pattern and differences between facade and decorator pattern? Facade gives the impression that your create a facade object over different desperate objects, or you actually decorate them with another layer, or they are two different name for the same pattern etc. All kind of confusion was there, because of the name, I noticed people get confused between these two pattern, not the one who understand both the pattern well, but the people who read about them last year and did not understand them well.</p>
<p>With this blog entry, I want to make sure that you will never ever forget their definition and never get mixed up between these two patterns.</p>
<blockquote><p><a href="http://en.wikipedia.org/wiki/Decorator_pattern" target="_blank">Decorator pattern</a> is a design pattern that allows new/additional behavior to be added to an existing class dynamically. Decorators provide a flexible alternative to sub classing for extending functionality.</p></blockquote>
<p>The decorator pattern can be used to make it possible <strong>to extend (decorate) the functionality of a class at runtime</strong>. Taking the example from Allan Shalloway book, <a href="http://www.amazon.com/Design-Patterns-Explained-Perspective-Object-Oriented/dp/0201715945" target="_blank">Design patterns explained</a>. Suppose you have to print a SalesTicket, and depending up on sales, you may need to print multiple headers and multiple footers. Decorator pattern helps you control headers, by chaining together the headers desired in the correct order.</p>
<p>Header1-&gt;Header2-&gt;Header3-&gt;Concrete Component.</p>
<p><a href="http://sarosh.files.wordpress.com/2009/09/beadneckles2.jpg"><img style="border-bottom:0;border-left:0;display:inline;border-top:0;border-right:0;margin:0 10px 0 0;" title="beadneckles2" src="http://sarosh.files.wordpress.com/2009/09/beadneckles2_thumb.jpg?w=136&#038;h=164" border="0" alt="beadneckles2" width="136" height="164" align="left" /></a> The Decorator pattern separates the dynamic building of this chain of functionality from the client. It will also separates the building of the chain from the chain components (Header and Footer).</p>
<p>Now, here is a way to remember it forever, a different way to think is, <em>Decorator pattern help decorate</em>. decorator pattern help us create  necklaces with different bead color combinations.  <strong>Decorator pattern is chain of objects.</strong> To achieve this you wrap the original bead class with a decorator class.</p>
<p>Suppose we need to write a program, which make different necklaces with different color combinations of beads. Because of decorator pattern it is fairly easy to build the different necklaces and the change in code can be limited to factory class.</p>
<p> </p>
<p>Here is the code:</p>
<pre class="brush: csharp;">
using System;

namespace Decorator
{

///
/// The Beads abstract class
///

abstract class Beads
{
        public abstract void MakeNecklace();
}

///
/// The 'ConcreteComponent' class
///

class ConcerteBead : Beads
{

public override void MakeNecklace()
{
         Console.WriteLine(&quot;ConcreteBead&quot;);
}
}

///
/// The 'Decorator' abstract class
///

abstract class Decorator : Beads
{
      protected Beads beads;
      public Decorator(Beads _beads)
     {
           this.beads = _beads;
     }
    public Decorator()
   {
    }

 public override void MakeNecklace()
 {
       if (beads != null)
     {
         beads.MakeNecklace();
      }
 }
}

class RedBeads : Decorator
{
       public RedBeads(Beads _beads)
      {
           beads = _beads;
        }
       public override void MakeNecklace()
      {
          base.MakeNecklace();
         Console.WriteLine(&quot;RED&quot;);
     }
}

class GreenBeads : Decorator
{
         public GreenBeads(Beads _beads)
        {
              beads = _beads;
        }
        public override void MakeNecklace()
       {
          base.MakeNecklace();
          Console.WriteLine(&quot;GREEN&quot;);
        }
}

class WhiteBeads : Decorator
{
        public WhiteBeads(Beads _beads)
       {
           beads = _beads;
        }
         public override void MakeNecklace()
         {
            base.MakeNecklace();
            Console.WriteLine(&quot;WHITE&quot;);
         }
}

class Program
{
        static void Main()
       {
           Beads beads = BeadFactory(true);
           beads.MakeNecklace();
           Console.ReadKey();
       }

private static Beads BeadFactory(bool Colorful)
{
           if (Colorful)
         {
                return new GreenBeads(new RedBeads(new WhiteBeads(new ConcerteBead())));
          }
else
        {
              return new WhiteBeads(new WhiteBeads(new WhiteBeads(new ConcerteBead())));

       }
  }
 }
}
</pre>
<p>The output of the above code will be:</p>
<p>Concrete Bead<br />
WHITE<br />
RED<br />
GREEN</p>
<p> </p>
<p>To decouple client from the chain of object,  a factory objects can instantiate the chains based upon some configuration information.  Instantiation of the chains of objects is completely decoupled from the Client<a name="typically accomplished"></a> objects that use it. Decorator Allows for extending the functionality of an object without resorting to subclassing.</p>
<p>May be tomorrow we will talk about Facade pattern.</p>
  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sarosh.wordpress.com/129/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sarosh.wordpress.com/129/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sarosh.wordpress.com/129/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sarosh.wordpress.com/129/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sarosh.wordpress.com/129/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sarosh.wordpress.com/129/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sarosh.wordpress.com/129/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sarosh.wordpress.com/129/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sarosh.wordpress.com/129/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sarosh.wordpress.com/129/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sarosh.wordpress.com&blog=1119187&post=129&subd=sarosh&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://sarosh.wordpress.com/2009/09/22/difference-between-facade-and-decorator-pattern/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/d8e93bef927d029c824b91da0b59f703?s=96&#38;d=http%3A%2F%2F1.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96&#38;r=G" medium="image">
			<media:title type="html">rsarosh</media:title>
		</media:content>

		<media:content url="http://sarosh.files.wordpress.com/2009/09/beadneckles2_thumb.jpg" medium="image">
			<media:title type="html">beadneckles2</media:title>
		</media:content>
	</item>
		<item>
		<title>Suddenly working system performance nose dived, now what?</title>
		<link>http://sarosh.wordpress.com/2009/09/17/index-name/</link>
		<comments>http://sarosh.wordpress.com/2009/09/17/index-name/#comments</comments>
		<pubDate>Thu, 17 Sep 2009 21:49:16 +0000</pubDate>
		<dc:creator>Rafat Sarosh</dc:creator>
				<category><![CDATA[SQL]]></category>

		<guid isPermaLink="false">http://sarosh.wordpress.com/2009/09/17/index-name/</guid>
		<description><![CDATA[We had a ETL process, which used to process a small table of thirty million records a time. These records were moved in a staging table for the extensive processing. Once the processing completes, we truncate the staging table and get the data for the next day.
The system ran fine for couple of days, then [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sarosh.wordpress.com&blog=1119187&post=118&subd=sarosh&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>We had a ETL process, which used to process a small table of thirty million records a time. These records were moved in a staging table for the extensive processing. Once the processing completes, we truncate the staging table and get the data for the next day.</p>
<p>The system ran fine for couple of days, then suddenly the ETL processed slowed and the ETL complete time increased by many folds, from 15 minutes it reached to four hours or so.</p>
<p>Our first attempt to find the cause of slow query performance was to check the Query plan. There I noticed that we are using an Index Lookup.</p>
<p>
<pre class="brush: sql;">   SELECT @DLStartTime = MIN(DownloadTime)      FROM etl.VLSCRawDataFromStager(nolock)      WHERE (DownloadURI = @DownloadURI AND ASID = @ASID) OR TransactionID = @TransactionId&lt;/p&gt;  &lt;p&gt;</pre>
</p>
<p>We had couple of non-clustered indexes on DownloadURI, ASID and on TransactionId. And a clustered Index on RowId. Looking at the query, you may think the query must be using these simple non-crusted indexes to get the DownloadTime. However, the Query plan looks like as follows:</p>
<p><a href="http://sarosh.files.wordpress.com/2009/09/execution.jpg"><img style="display:inline;border-width:0;" title="execution" border="0" alt="execution" src="http://sarosh.files.wordpress.com/2009/09/execution_thumb.jpg?w=600&#038;h=300" width="600" height="300" /></a> </p>
<p>&#160;</p>
<p>Query is using the key lookup operator. <strong>The use of a Key Lookup operator in a query plan indicates that the query might benefit from performance tuning.</strong> For example, query performance might be improved by adding a covering index. read more on <a title="http://msdn.microsoft.com/en-us/library/bb326635.aspx" href="http://msdn.microsoft.com/en-us/library/bb326635.aspx">http://msdn.microsoft.com/en-us/library/bb326635.aspx</a></p>
<p>Detail of two non-clustered indexes seeks, output shows as&#160; ‘RowId’ and then this ‘RowId’ is used in turn to do a Key lookup on clustered Index for the output ‘DownloadTime’. Key lookup fetches the extra columns from the clustered index when the non clustered index that’s used to retrieve the rows doesn’t have all the columns required. </p>
<p>Bookmark lookup is not a cheap operation. The&#160; plan can be improved by&#160; adding one or more columns to an existing index so as to eliminate a bookmark lookup, in SQL Server 2005 onwards, you can add columns using the include clause of the create index statement.&#160; Included columns are more efficient than key columns; they save disk space and make searching and updating the index more efficient.&#160; SQL decide on the query plan depending on the number of rows returned. Read more on </p>
<p><a title="http://sqlserverpedia.com/blog/sql-server-bloggers/key-lookup-threshold/comment-page-1/" href="http://sqlserverpedia.com/blog/sql-server-bloggers/key-lookup-threshold/comment-page-1/">http://sqlserverpedia.com/blog/sql-server-bloggers/key-lookup-threshold/comment-page-1/</a>     <br /><a title="http://blogs.msdn.com/craigfr/archive/2006/06/30/652639.aspx" href="http://blogs.msdn.com/craigfr/archive/2006/06/30/652639.aspx">http://blogs.msdn.com/craigfr/archive/2006/06/30/652639.aspx</a></p>
<p>We modified one of the index and included other columns.</p>
<p>
<pre class="brush: sql;"> CREATE NONCLUSTERED INDEX [VLSCRawDataFromStager_ndx2] ON [etl].[VLSCRawDataFromStager]    (  [DownloadTime] ASC,  [DownloadURI] ASC,  [ASID] ASC,  [TransactionID] ASC   )     &lt;br /&gt;GO&lt;/p&gt;  &lt;p&gt;</pre>
</p>
<p>This change removed the Key lookup from the query plan, and suddenly our performance increased by many fold.</p>
<p>It worked for few hours, but before our celebration beer can become warm, the performance again tanked. Now what? </p>
<p>What the hell happened?&#160; For every run we are truncating the tables, and the system ran fine for weeks, now it is tanking, specially when we removed the nasty key lookup. We thought we improved the system but system performance is not improved.</p>
<p>Now, I used <strong>sys.dm_db_index_physical_stats </strong>check the indexes<strong>, </strong>this returns fragmentation information for the data and indexes of the specified table or view. </p>
<p>
<pre class="brush: sql;"> select * from sys.dm_db_index_physical_stats (DB_ID(N'DataBaseName'), OBJECT_ID(N'TableName'), NULL, NULL , NULL); </pre>
</p>
<p>and of course we see the indexes are fragmented more then 95%. I&#160; realized the <strong>truncate table statement does not get rid of Indexes</strong>, TRUNCATE TABLE removes all rows from a table, but the table structure and its columns, constraints, indexes and so on remain. read more <a title="http://msdn.microsoft.com/en-us/library/aa260621(SQL.80).aspx" href="http://msdn.microsoft.com/en-us/library/aa260621(SQL.80).aspx">http://msdn.microsoft.com/en-us/library/aa260621(SQL.80).aspx</a></p>
<p>Overlooking this fact threw us in wrong direction, else this is the first thing we should be checking, but we thought truncate table is cleaning the indexes too, which turn out not be the case. However, the simple fix was to get rid of indexes too, and recreate them after every truncate. This fix bought our ETL performance back to acceptable level.</p>
<p>&#160;</p>
<p>P.S</p>
<p>However the result set from <strong>sys.dm_db_index_physical_stats </strong>has index_id and object_id as the column name. You may waste few minutes to write query to get the index name from these ids.&#160; So here is the query: </p>
<pre class="brush: sql;"> select i.*, stat.* from sys.dm_db_index_physical_stats (DB_ID(N'DataBaseName'), OBJECT_ID(N'TableName'), NULL, NULL , NULL) stat join sys.indexes i on i.object_id = stat.object_id and i.index_id = stat.index_id </pre>
<p>OBJECT_ID is evaluated in the current database context, not in the context of the database which you specified in for DB_ID. If the current database does not have the table, then Object_ID will return NULL and your result will be wrong</p>
<p>To get all the indexes in a database it is simply:</p>
<pre class="brush: sql;"> select i.*, stat.* from sys.dm_db_index_physical_stats (DB_ID(N'AdventureWorks'), NULL, NULL, NULL , NULL) stat join sys.indexes i on i.object_id = stat.object_id and i.index_id = stat.index_id where stat.avg_fragmentation_in_percent &amp;gt; 10   &lt;p&gt;&lt;/p&gt; </pre>
</p>
<p>Read more at <a title="http://msdn.microsoft.com/en-us/library/ms188917.aspx" href="http://msdn.microsoft.com/en-us/library/ms188917.aspx">http://msdn.microsoft.com/en-us/library/ms188917.aspx</a></p>
  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sarosh.wordpress.com/118/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sarosh.wordpress.com/118/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sarosh.wordpress.com/118/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sarosh.wordpress.com/118/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sarosh.wordpress.com/118/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sarosh.wordpress.com/118/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sarosh.wordpress.com/118/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sarosh.wordpress.com/118/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sarosh.wordpress.com/118/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sarosh.wordpress.com/118/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sarosh.wordpress.com&blog=1119187&post=118&subd=sarosh&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://sarosh.wordpress.com/2009/09/17/index-name/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/d8e93bef927d029c824b91da0b59f703?s=96&#38;d=http%3A%2F%2F1.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96&#38;r=G" medium="image">
			<media:title type="html">rsarosh</media:title>
		</media:content>

		<media:content url="http://sarosh.files.wordpress.com/2009/09/execution_thumb.jpg" medium="image">
			<media:title type="html">execution</media:title>
		</media:content>
	</item>
		<item>
		<title>Temp Table Variable v/s Temp table</title>
		<link>http://sarosh.wordpress.com/2009/09/16/temp-table-variable-vs-temp-table/</link>
		<comments>http://sarosh.wordpress.com/2009/09/16/temp-table-variable-vs-temp-table/#comments</comments>
		<pubDate>Wed, 16 Sep 2009 21:01:58 +0000</pubDate>
		<dc:creator>Rafat Sarosh</dc:creator>
				<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL SQL Performance]]></category>

		<guid isPermaLink="false">http://sarosh.wordpress.com/2009/09/16/temp-table-variable-vs-temp-table/</guid>
		<description><![CDATA[We know that using Temp table in a stored procedure&#160; reduced the chance to reuse the execution plan. Change the temp table to temp variable 

 select top 1000  LimitedProgramTBNEventID into #Events from LimitedProgramTBNEvent(NOLOCK)  	where eventStatus = 7 
we changed it as follows:
 DECLARE @Events table (LimitedProgramTBNEventID int NOT NULL) insert into @Events [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sarosh.wordpress.com&blog=1119187&post=117&subd=sarosh&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>We know that using Temp table in a stored procedure&#160; reduced the chance to reuse the execution plan. Change the temp table to temp variable </p>
<p>
<pre class="brush: sql;"> select top 1000  LimitedProgramTBNEventID into #Events from LimitedProgramTBNEvent(NOLOCK)  	where eventStatus = 7 </pre>
<p>we changed it as follows:</p>
<pre class="brush: sql;"> DECLARE @Events table (LimitedProgramTBNEventID int NOT NULL) insert into @Events select top 1000 LimitedProgramTBNEventID 	from LimitedProgramTBNEvent (NOLOCK)where eventStatus = 7  </pre>
<p>Here is another reason for not to use the Temp table. If you have a temp table then you cannot use the ‘Display Estimated execution plan&quot;’ option in the query analyzer. When a query is run using the &quot;Display Estimated Execution Plan&quot; option, it does not really run so no temp tables are created. Since they are not created, any references to them in the code will fail, which prevents an estimated execution plan from being created.&#160; </p>
  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sarosh.wordpress.com/117/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sarosh.wordpress.com/117/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sarosh.wordpress.com/117/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sarosh.wordpress.com/117/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sarosh.wordpress.com/117/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sarosh.wordpress.com/117/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sarosh.wordpress.com/117/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sarosh.wordpress.com/117/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sarosh.wordpress.com/117/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sarosh.wordpress.com/117/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sarosh.wordpress.com&blog=1119187&post=117&subd=sarosh&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://sarosh.wordpress.com/2009/09/16/temp-table-variable-vs-temp-table/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/d8e93bef927d029c824b91da0b59f703?s=96&#38;d=http%3A%2F%2F1.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96&#38;r=G" medium="image">
			<media:title type="html">rsarosh</media:title>
		</media:content>
	</item>
		<item>
		<title>Not for replication</title>
		<link>http://sarosh.wordpress.com/2009/08/24/not-for-replication/</link>
		<comments>http://sarosh.wordpress.com/2009/08/24/not-for-replication/#comments</comments>
		<pubDate>Mon, 24 Aug 2009 21:41:15 +0000</pubDate>
		<dc:creator>Rafat Sarosh</dc:creator>
				<category><![CDATA[SQL]]></category>
		<category><![CDATA[replication]]></category>

		<guid isPermaLink="false">http://sarosh.wordpress.com/?p=90</guid>
		<description><![CDATA[If you have a database which is replicated, then run the following query on the publisher and if you get any result back then you got some work at your hand

select distinct t.[name], c.* from sys.tables t
    join sys.columns c on t.object_id = c.object_id
             and c.is_identity = 1 and c.is_replicated = 0

this query basically tells [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sarosh.wordpress.com&blog=1119187&post=90&subd=sarosh&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>If you have a database which is replicated, then run the following query on the publisher and if you get any result back then you got some work at your hand</p>
<pre class="brush: sql;">
select distinct t.[name], c.* from sys.tables t
    join sys.columns c on t.object_id = c.object_id
             and c.is_identity = 1 and c.is_replicated = 0
</pre>
<p>this query basically tells that you have tables which have identity columns but they are not created with ‘NOT FOR REPLICATION’ option.</p>
<p>If that is the case then your identity columns are out of synch between publisher and subscriber. You can fix it by setting the is_replicated column to 1.</p>
<pre class="brush: sql;">
EXEC %%
ColumnEx(
ObjectID = @
object_id,
Name = @
identity_column).
SetIdentityNotForRepl(VALUE = 1)
</pre>
  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sarosh.wordpress.com/90/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sarosh.wordpress.com/90/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sarosh.wordpress.com/90/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sarosh.wordpress.com/90/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sarosh.wordpress.com/90/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sarosh.wordpress.com/90/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sarosh.wordpress.com/90/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sarosh.wordpress.com/90/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sarosh.wordpress.com/90/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sarosh.wordpress.com/90/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sarosh.wordpress.com&blog=1119187&post=90&subd=sarosh&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://sarosh.wordpress.com/2009/08/24/not-for-replication/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/d8e93bef927d029c824b91da0b59f703?s=96&#38;d=http%3A%2F%2F1.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96&#38;r=G" medium="image">
			<media:title type="html">rsarosh</media:title>
		</media:content>
	</item>
		<item>
		<title>ACCESS (MDB) to XML with LINQ</title>
		<link>http://sarosh.wordpress.com/2009/06/17/access-mdb-to-xml-with-linq/</link>
		<comments>http://sarosh.wordpress.com/2009/06/17/access-mdb-to-xml-with-linq/#comments</comments>
		<pubDate>Wed, 17 Jun 2009 00:30:59 +0000</pubDate>
		<dc:creator>Rafat Sarosh</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[LINQ]]></category>
		<category><![CDATA[MDB]]></category>
		<category><![CDATA[MS ACCESS]]></category>
		<category><![CDATA[XML]]></category>

		<guid isPermaLink="false">http://sarosh.wordpress.com/?p=79</guid>
		<description><![CDATA[I had some data in Access DB and wanted to convert it to XML. Data in Access was in one table called ‘Words’, with the following columns: Word, Meaning, Synonyms, Antonyms, Example etc.  I wanted to convert this data in XML as follows:

&#60;Dictionary&#62;
&#60;Word Spelling=&#34;Munificent&#34;&#62;
    &#60;Meaning&#62;very generous&#60;/Meaning&#62;
    &#60;Synonyms /&#62;
 [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sarosh.wordpress.com&blog=1119187&post=79&subd=sarosh&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>I had some data in Access DB and wanted to convert it to XML. Data in Access was in one table called ‘Words’, with the following columns: Word, Meaning, Synonyms, Antonyms, Example etc.  I wanted to convert this data in XML as follows:</p>
<pre class="brush: csharp;">
&lt;Dictionary&gt;
&lt;Word Spelling=&quot;Munificent&quot;&gt;
    &lt;Meaning&gt;very generous&lt;/Meaning&gt;
    &lt;Synonyms /&gt;
    &lt;Antonyms /&gt;
    &lt;Example /&gt;
  &lt;/Word&gt;
  &lt;Word Spelling=&quot;Anfractuosity&quot;&gt;
    &lt;Meaning&gt;Twist or turn, A complicated or involved process. &lt;/Meaning&gt;
    &lt;Synonyms /&gt;
    &lt;Antonyms /&gt;
    &lt;Example&gt; The anfractuosities of his intellect and temper. –Macaulay &lt;/Example&gt;
  &lt;/Word&gt;
&lt;/Dictionary&gt;
</pre>
<p>LINQ made job to read the data and convert it to XML very easy, as you can see all this happen in one line.</p>
<pre class="brush: csharp;">
using System;
using System.Linq;
using System.Text;
using System.Data.OleDb;
using System.Xml.Linq;
using System.Data.Linq;
using System.Data.Linq.Mapping;

namespace Convert2XML
{
    class Program
    {
static void Main(string[] args)
        {
                  string ConnectionString = @&quot;Provider=Microsoft.ACE.OLEDB.12.0;Data Source=Vocabulary.mdb;Persist Security Info=false&quot;;
                  DataContext db = new DataContext(new OleDbConnection(ConnectionString)) ;
                  Table &lt;words&gt; tblwords =   db.GetTable&lt;words&gt;();
                  System.IO.File.WriteAllText(&quot;Data.XML&quot;,
                            ( new XElement(&quot;Dictionary&quot;, from w in tblwords
                                                   select new XElement(&quot;Word&quot; ,
                                                          new XAttribute(&quot;Spelling&quot;, w.Word),
                                                   new XElement(&quot;Meaning&quot;, w.Meaning),
                                                   new XElement(&quot;Synonyms&quot;, w.Synonyms),
                                                   new XElement(&quot;Antonyms&quot;, w.Antonyms),
                                                   new XElement(&quot;Example&quot;, w.Example)
                                                   )
                                           )
                             ).ToString()
                             , Encoding.UTF8);
        }

        [Table(Name = &quot;words&quot;)]
        public class words
        {
            [Column]
            public string Word;
            [Column]
            public string Meaning;
            [Column]
            public string Synonyms;
            [Column]
            public string Antonyms;
            [Column]
            public int weight;
            [Column]
            public string Reference;
            [Column]
            public string Example;
            [Column]
            public string link;
        }
    }
}
</pre>
<p>If you don’t have OLEDB 12.0 then You can use the following connection string too</p>
<p>string ConnectionString = @&#8221;Provider=Microsoft.Jet.OLEDB.4.0;Data Source=Vocabulary.mdb;Persist Security Info=True&#8221; ;</p>
<p>If you don’t have Microsoft.ACE.OLEDB.12.0 then get it from here</p>
<p>http://www.microsoft.com/downloads/details.aspx?familyid=7554F536-8C28-4598-9B72-EF94E038C891&amp;displaylang=en</p>
<p>These oledb providers are 32 bit, so make sure to compile your project for 32 bit on a 64 bit machine.<br />
<em>Error handling and comments are removed for the brevity.</em></p>
  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sarosh.wordpress.com/79/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sarosh.wordpress.com/79/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sarosh.wordpress.com/79/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sarosh.wordpress.com/79/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sarosh.wordpress.com/79/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sarosh.wordpress.com/79/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sarosh.wordpress.com/79/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sarosh.wordpress.com/79/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sarosh.wordpress.com/79/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sarosh.wordpress.com/79/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sarosh.wordpress.com&blog=1119187&post=79&subd=sarosh&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://sarosh.wordpress.com/2009/06/17/access-mdb-to-xml-with-linq/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/d8e93bef927d029c824b91da0b59f703?s=96&#38;d=http%3A%2F%2F1.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96&#38;r=G" medium="image">
			<media:title type="html">rsarosh</media:title>
		</media:content>
	</item>
		<item>
		<title>How to Partition Cube – using C#</title>
		<link>http://sarosh.wordpress.com/2009/06/03/how-to-partition-cube-%e2%80%93-using-c/</link>
		<comments>http://sarosh.wordpress.com/2009/06/03/how-to-partition-cube-%e2%80%93-using-c/#comments</comments>
		<pubDate>Wed, 03 Jun 2009 23:39:05 +0000</pubDate>
		<dc:creator>Rafat Sarosh</dc:creator>
				<category><![CDATA[Analysis Server]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[Cube]]></category>
		<category><![CDATA[Cube Partition]]></category>
		<category><![CDATA[SQL Analysis Server]]></category>

		<guid isPermaLink="false">http://sarosh.wordpress.com/2009/06/03/how-to-partition-cube-%e2%80%93-using-c/</guid>
		<description><![CDATA[There are many different ways you can partition a cube. Here I am talking about &#8211; how you can use C# to partition a cube from a DTS package?
Using  Microsoft.AnalysisServices  Object API, you can travel the whole Hierarchy of objects  and completely administer an Analysis Service Instance. From the connection Object you can get to [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sarosh.wordpress.com&blog=1119187&post=53&subd=sarosh&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>There are many different ways you can partition a cube. Here I am talking about &#8211; how you can use C# to partition a cube from a DTS package?</p>
<p>Using  Microsoft.AnalysisServices  Object API, you can travel the whole Hierarchy of objects  and completely administer an Analysis Service Instance. From the connection Object you can get to the Server and database instances. And in database you can transverse the Cube and their different measures. Once you get hold of a measure then you can transverse the different partitions of measures and dig deep in to each partition query definitions etc.<br />
<span id="more-53"></span><br />
To keep things simple, there is one prerequisite to name your measure partitions in the in following format: MeasureName_DateSKey. DateSKey is a long number showing the date from 1<sup>st</sup> Jan 1990. To get the DateSKey for a date you can use the following formula:</p>
<p> DateTime d;<br />
d.Date.Subtract(new System.DateTime(1900, 1, 1, 0, 0, 0)).Days</p>
<p>So, if your measure name is ‘Distinct Downloaders Daily’ then it may have following partitions for every 7 days as follows: Distinct Downloaders Daily_39812, Distinct Downloaders Daily_398129… etc<br />
<img src="http://sarosh.files.wordpress.com/2009/06/cubeparition.jpg?w=433&#038;h=280" alt="CubeParition" title="CubeParition" width="433" height="280" class="alignnone size-full wp-image-77" /><br />
Open a new DTS package, drag the script component and double click to start editing a C# script.</p>
<p>Include the following library:</p>
<pre class="brush: csharp;">
using Microsoft.AnalysisServices;
using System.Collections.ObjectModel;
</pre>
<p>Define a new structure as measure, to hold the definition of your measures and there partition boundaries.</p>
<p> 
<pre class="brush: csharp;">
// Structure used to keep the Measure name and their partition durations
struct Measure
{
       public string MeasureName;
      // Partition duration in days
      public int iPartitionDays;
      public Measure(string measureName, int ipartitionDays)
        {
          MeasureName = measureName;
          iPartitionDays = ipartitionDays;
        }
}
</pre>
<p>Now define a collection of measures which you want to partition. Sure, this information you can get from a Config file too … It is hardcoded in code for the illustration purpose.</p>
<pre class="brush: csharp;">
Collection Measures = new Collection();
Measures.Add(new Measure(&quot;DownLoad Summary&quot;, 7));
Measures.Add(new Measure(&quot;Distinct Downloaders Daily&quot;, 7));
Measures.Add(new Measure(&quot;Distinct Downloaders Weekly&quot;, 30));
Measures.Add(new Measure(&quot;Top Referer&quot;, 30));
</pre>
<p>Now you can use this collection to call a method called ProcessCube. Here is the code, removed all the error handling for the sake of brevity.</p>
<pre class="brush: csharp;">
private void ProcessCube( Collection Measures, string cubeName)
{
Server oServer = new Server();
try
{
// Get Connection object and then get the Server and Database name from DTS connection object
ConnectionManager oConnection = null;
for (int i = 0; i &amp;lt; Dts.Connections.Count; i++)
{
oConnection = Dts.Connections[i];
if (oConnection.Name == &quot;CubeConnectionName&quot;) //Connection name as defined in ETL package
{
break;
}
else
oConnection = null;
}

string sServer = (string)(oConnection.Properties[&quot;ServerName&quot;].GetValue(oConnection));
string sDatabase = (string)(oConnection.Properties[&quot;InitialCatalog&quot;].GetValue(oConnection));
oServer.Connect(sServer);// connect to the server and start scanning down the object hierarchy
Database oDB = oServer.Databases.FindByName(sDatabase);
Cube oCube = oDB.Cubes.FindByName(cubeName);
foreach (Measure m in Measures)
{
CreatePartition(oCube, m.MeasureName, m.iPartitionDays); //Create old paritions
DeletePartitions(oCube, m.MeasureName, m.iPartitionDays); // Delete old partitions

}
}
finally
{
if (oServer.Connected)
oServer.Disconnect();
}
}

///
/// Create a new partition based on the skipdays
///
/// Cube Name to be processed
/// MeasureName which need to be partitioned
/// Partition duration e.g. for daily=1, weekly=7, Monthly =30
/// true on success
///
///
/// This code Creates the new partition names, using the default Measure name as the partition name and the
/// DateSKey. for e.g. if the measure name is Browser and the DateSKey=39241
/// then the partition name will be 'Browser_39241'
///
///
///

bool CreatePartition(Cube oCube, string measureName, int iDaysToSkip)
{
Byte[] dataBytes = new Byte[5000];
QueryBinding oQueryBinding;
bool bflag = false;
int iCurrentDateSKey = (int)Dts.Variables[&quot;CubeCurrentDateSKey&quot;].Value; //Get the currentDateSkey
MeasureGroup oMeasureGroup = oCube.MeasureGroups.FindByName(measureName); // Find all measure groups
if (oMeasureGroup == null) return false;
Partition oPartition = oMeasureGroup.Partitions[oMeasureGroup.Partitions.Count - 1]; //Get the Last partition
int LastCubeDateSkey = GetPartitionDateSKey(oPartition);//Get the lastkey DateSkey
string DefaultPartitionName = oMeasureGroup.Name; //Get the Partition name first part
string sNewPartitionName = string .Empty ;
if (LastCubeDateSkey == 1) //if it is first default partition
{
sNewPartitionName = DefaultPartitionName + &quot;_&quot; + iCurrentDateSKey; //Making the new partition Name
}
else
{
if (LastCubeDateSkey + iDaysToSkip &amp;lt;= iCurrentDateSKey)
sNewPartitionName = DefaultPartitionName + &quot;_&quot; + iCurrentDateSKey;
else
return true; //No need to create the partition yet. Every thing is fine, go back
}

//Just another check, if the partition already existing then skip
oPartition = oMeasureGroup.Partitions.FindByName(sNewPartitionName);
Partition oPartitionNew;
if (oPartition == null)
{
//Did not get the partition, lets create one,Get the last partition
oPartition = oMeasureGroup.Partitions[oMeasureGroup.Partitions.Count - 1];
//Clone the properties from the last partition to the new partition.
oPartitionNew = oPartition.Clone();
oPartitionNew.ID = sNewPartitionName;
oPartitionNew.Name = sNewPartitionName;
oQueryBinding = oPartitionNew.Source as QueryBinding;
if (oQueryBinding == null)
{
return true; // No Query, No Partition
}
oQueryBinding.QueryDefinition = GetNewQuery(oQueryBinding.QueryDefinition, iCurrentDateSKey, iDaysToSkip);
if (oQueryBinding.QueryDefinition == null || oQueryBinding.QueryDefinition == string.Empty)
{
Dts.Log(&quot;Partition : &quot; + oPartitionNew.Name + &quot; Empty Query returned&quot;, 0, dataBytes);
Dts.Events.FireInformation(0, &quot;CreatePartition&quot;, &quot;Partition : &quot; + oPartitionNew.Name + &quot; Empty Query returned&quot;, null, 0, ref bflag);
return false;
}

if (oMeasureGroup.Partitions.Contains(oPartitionNew))
{
Dts.Log(&quot;Partition : &quot; + oPartitionNew.Name + &quot; Already exists.&quot;, 0, dataBytes);
return true; //Oh! what a surprise, this partition already existing.
}
oMeasureGroup.Partitions.Add(oPartitionNew);
try
{
oPartitionNew.Update();
Dts.Log(&quot;Partition updated: &quot; + oPartitionNew.Name, 0, dataBytes);
Dts.Events.FireInformation(0, &quot;CreatePartition&quot;, &quot;Partition updated: &quot; + oPartitionNew.Name, null, 0, ref bflag);
oPartitionNew.Process(ProcessType.ProcessFull);
Dts.Log(&quot;Partition Processed: &quot; + oPartitionNew.Name, 0, dataBytes);
Dts.Events.FireInformation(0, &quot;CreatePartition&quot;, &quot;Partition Processed: &quot; + oPartitionNew.Name, null, 0, ref bflag);

}
catch (Exception e)
{
if (oPartitionNew != null)
Dts.Log(&quot;Error in Create partition: &quot; + oPartitionNew.Name + &quot;Exception: &quot; + e.Message, 0, dataBytes);
else
Dts.Log(&quot;Error in Create partition: Unknown. Exception: &quot; + e.Message, 0, dataBytes);
return false;
}
}
return true;
} //~Create partitio ends here

///
/// This function Gets the partition DateSKey from the partition name
///
/// On Success the right DateSKey, on error zero
///
private int GetPartitionDateSKey(Partition oPartition)
{
if (oPartition == null) return 0;
try
{
return Convert.ToInt32(oPartition.Name.Substring(oPartition.Name.LastIndexOf(&quot;_&quot;) + 1));
}
catch (Exception ex)
{
return 0;
}
}

///
/// Get the new Query for the partition.
///

string GetNewQuery(string sourceQuery, int iCurrentDateSKey, int iDaysToSkip)
{

string mainQuery = string.Empty;
string newQuery = string.Empty;
string newCondition;
//From the Query get the Where clause
int LastIndex = sourceQuery.ToUpper().LastIndexOf(&quot;WHERE&quot;) ;
if (LastIndex &amp;gt; 0)
{
mainQuery = sourceQuery.Substring(0, LastIndex);
newCondition = &quot; [DateSKey] &amp;gt;= &quot; + Convert.ToString(iCurrentDateSKey) + &quot; AND [DateSKey] = &quot; + Convert.ToString(iCurrentDateSKey) + &quot; AND [DateSKey] &amp;lt; &quot; + Convert.ToString(iCurrentDateSKey + iDaysToSkip ) + &quot; &quot;;
newQuery = sourceQuery + &quot; WHERE &quot; + newCondition;
}

return newQuery;
}

//Deletes old partitions based on the data retention period.
bool DeletePartitions(Cube oCube, string measureName, int iDaysToSkip)
{
Collection PartitionTobeDeleted = new Collection();
int iDateSkeyTobeDeleted ;
Byte [] dataBytes = new Byte [5000];
bool bflag = false;
// 13 * 30 = 390 days - 13 months
int iDaysTobeDeleted = 390 ; //TODO: Get from a variable [&quot;DataRetentionPeriodInDays&quot;]

//Getting the number for which partition needs to deleted. old partitions
iDateSkeyTobeDeleted = GetDateSKey (DateTime.Now) - iDaysTobeDeleted ;

MeasureGroup oMeasureGroup = oCube.MeasureGroups.FindByName (measureName);
if (oMeasureGroup == null)
{
Dts.Log(&quot;Measure : &quot; + oMeasureGroup.Name + &quot; not found.&quot;, 0, dataBytes);
return false;
}
Dts.Log(&quot;[DeletePartitions] Working on Measure: &quot; + oMeasureGroup.Name, 0, dataBytes);
string sPartitionName = string.Empty; //used for logging the correct PartitionName
int MaxPartitions = oMeasureGroup.Partitions.Count;
foreach (Partition oPartition2 in oMeasureGroup.Partitions)
{
sPartitionName = oPartition2.Name;
if (!oPartition2.Name.Contains(&quot;_&quot;))
continue; //_ is not there it means it is not the partition which we need to delete
try
{
// Get the boundary partition date from partition name
int spartitionBoundaryDateSkey = GetPartitionDateSKey(oPartition2);
if (spartitionBoundaryDateSkey == 0) continue;

//if partition DateSKey is smaller then the partition to be deleted then delete
//MaxPartition protects us from deleting all the partitions from the measures
if (spartitionBoundaryDateSkey 1)
{
MaxPartitions--;
//can not drop object here because of foreach.
PartitionTobeDeleted.Add(oPartition2);
#region Log
Dts.Log(&quot;Found partition that needs to be dropped: &quot; + oPartition2.Name, 0, dataBytes);
Dts.Events.FireInformation(0, &quot;DeletePartition&quot;, &quot;Found partition that needs to be dropped: &quot; + oPartition2.Name, null, 0, ref bflag);
#endregion
}
}
catch (Exception e)
{
Dts.Log(&quot;Error in Delete partition: &quot; + sPartitionName + &quot;Exception: &quot; + e.Message, 0, dataBytes);
Dts.Events.FireInformation(0, &quot;DeletePartition&quot;, &quot;Error in Delete partition: &quot; + sPartitionName + &quot;Exception: &quot; + e.Message, null, 0, ref bflag);
}
}
//Deleting partition from the measure group
foreach (Partition oPartition in PartitionTobeDeleted)
{
XmlaWarningCollection warningColln = new XmlaWarningCollection();
oPartition.Drop(DropOptions.Default, warningColln);
}
return true;
}
</pre>
  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sarosh.wordpress.com/53/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sarosh.wordpress.com/53/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sarosh.wordpress.com/53/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sarosh.wordpress.com/53/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sarosh.wordpress.com/53/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sarosh.wordpress.com/53/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sarosh.wordpress.com/53/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sarosh.wordpress.com/53/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sarosh.wordpress.com/53/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sarosh.wordpress.com/53/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sarosh.wordpress.com&blog=1119187&post=53&subd=sarosh&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://sarosh.wordpress.com/2009/06/03/how-to-partition-cube-%e2%80%93-using-c/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/d8e93bef927d029c824b91da0b59f703?s=96&#38;d=http%3A%2F%2F1.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96&#38;r=G" medium="image">
			<media:title type="html">rsarosh</media:title>
		</media:content>

		<media:content url="http://sarosh.files.wordpress.com/2009/06/cubeparition.jpg" medium="image">
			<media:title type="html">CubeParition</media:title>
		</media:content>
	</item>
		<item>
		<title>Push or Pull Replication</title>
		<link>http://sarosh.wordpress.com/2009/03/11/push-or-pull-replication/</link>
		<comments>http://sarosh.wordpress.com/2009/03/11/push-or-pull-replication/#comments</comments>
		<pubDate>Wed, 11 Mar 2009 14:18:55 +0000</pubDate>
		<dc:creator>Rafat Sarosh</dc:creator>
				<category><![CDATA[SQL]]></category>

		<guid isPermaLink="false">http://sarosh.wordpress.com/?p=47</guid>
		<description><![CDATA[A study conducted by MSCOM ops team found 1 GB of data took 110 Minute to push and 12.5 minutes to Pull across the continents. Furthermore SQL 2008 with Windows Server 2008 made a huge difference over SQL 2005 and Windows Server 2003. 
 
Push subscription replication of character data with SQL Server 2008 running on [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sarosh.wordpress.com&blog=1119187&post=47&subd=sarosh&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p class="MsoNormal" style="margin:0;"><span style="color:black;"><span style="font-size:small;"><span style="font-family:Calibri;">A study conducted by MSCOM ops team found 1 GB of data took 110 Minute to push and 12.5 minutes to Pull across the continents. Furthermore SQL 2008 with Windows Server 2008 made a huge difference over SQL 2005 and Windows Server 2003. </span></span></span></p>
<p class="MsoNormal" style="margin:0;"><span style="color:black;"><span style="font-family:Calibri;font-size:small;"> </span></span></p>
<p class="MsoNormal" style="margin:0;"><span style="color:black;"><span style="font-size:small;"><span style="font-family:Calibri;">Push subscription replication of character data with SQL Server 2008 running on Windows Server 2008 yielded a 104 percent increase over SQL Server 2005 running on Windows Server 2003, and pull subscription replication of the same data yielded a 1,298 percent gain.</span></span></span></p>
<p class="MsoNormal" style="margin:0;"><span style="color:black;"><span style="font-family:Calibri;font-size:small;"> </span></span></p>
<p class="MsoNormal" style="margin:0;"><span style="color:black;"><span style="font-size:small;"><span style="font-family:Calibri;">Lot of performance improvement is attributed to windows server TCP/IP Stack and SQL Native Client driver. Different reader and writer thread behavior resulted in push and pull performance difference. </span></span></span></p>
<p class="MsoNormal" style="margin:0;"><span style="color:black;"><span style="font-size:small;"></span></span> </p>
<p class="MsoNormal" style="margin:0;"><span style="color:black;"><span style="font-size:small;"><span style="font-family:Calibri;">Reader thread fills two buffers <span> </span>of 40 KB <span> </span>and signals the Writer thread to write the buffer on subscriber, so the 40 KB packet moves thru and fro over the wire, but in case of Pull model Writer thread pulls the data in one huge chunk as big as TCP/IP let it get.<span>  </span>One of the most significant improvements to Windows Server 2008 is the autotuning of the receive window size, designed for high-latency environments. In previous versions of the operating system, the maximum window size was limited by the 16-bit Window field in the TCP header amounting to a maximum window size of 64 KB. In Windows Server 2008, by combining the Window field together with a Scale Factor field of the TCP header, the window can be scaled or tuned, up to 16 megabytes (MB) in size.</span></span></span></p>
<p class="MsoNormal" style="margin:0;"><span style="color:black;"><span style="font-family:Calibri;font-size:small;"> </span></span></p>
<p class="MsoNormal" style="margin:0;"><span style="font-family:Calibri;font-size:small;">Read the full study here </span><a href="http://msdn.microsoft.com/en-us/library/dd263442.aspx"><span style="font-family:Calibri;font-size:small;">http://msdn.microsoft.com/en-us/library/dd263442.aspx</span></a></p>
<p class="MsoNormal" style="margin:0;"> </p>
<p class="MsoNormal" style="margin:0;"> </p>
  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sarosh.wordpress.com/47/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sarosh.wordpress.com/47/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sarosh.wordpress.com/47/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sarosh.wordpress.com/47/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sarosh.wordpress.com/47/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sarosh.wordpress.com/47/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sarosh.wordpress.com/47/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sarosh.wordpress.com/47/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sarosh.wordpress.com/47/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sarosh.wordpress.com/47/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sarosh.wordpress.com&blog=1119187&post=47&subd=sarosh&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://sarosh.wordpress.com/2009/03/11/push-or-pull-replication/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/d8e93bef927d029c824b91da0b59f703?s=96&#38;d=http%3A%2F%2F1.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96&#38;r=G" medium="image">
			<media:title type="html">rsarosh</media:title>
		</media:content>
	</item>
		<item>
		<title>Log Parser and SQL</title>
		<link>http://sarosh.wordpress.com/2009/02/27/log-parser-and-sql/</link>
		<comments>http://sarosh.wordpress.com/2009/02/27/log-parser-and-sql/#comments</comments>
		<pubDate>Fri, 27 Feb 2009 16:49:36 +0000</pubDate>
		<dc:creator>Rafat Sarosh</dc:creator>
				<category><![CDATA[SQL]]></category>

		<guid isPermaLink="false">http://sarosh.wordpress.com/?p=37</guid>
		<description><![CDATA[

I thought everyone who wants to know how to put the windows events or IIS Logs to SQL already know about it. But for my amazement it is not the case. Quick search on the web did not turned up a short tutorial. So how to move my window events to SQL? Here it is 

 
Install [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sarosh.wordpress.com&blog=1119187&post=37&subd=sarosh&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><div class="post">
<div class="posthead"><a id="bp___v___r___postlist___EntryItems_ctl00_AuthorLink" href="http://sarosh.wordpress.com/user/Profile.aspx?UserID=40268"></a></div>
<p><span style="font-size:small;"><span style="font-family:Calibri;">I thought everyone who wants to know how to put the windows events or IIS Logs to SQL already know about it. But for my amazement it is not the case. Quick search on the web did not turned up a short tutorial. So how to move my window events to SQL? Here it is </span></span></p>
<div class="postcontent">
<p class="MsoNormal" style="margin:0;"><span style="font-size:small;font-family:Calibri;"> </span></p>
<p class="MsoNormal" style="margin:0;"><span style="font-size:small;"><span style="font-family:Calibri;">Install the Log Parser on the box.</span></span></p>
<p class="MsoNormal" style="margin:0;"><span style="font-size:small;font-family:Calibri;">Get the Log Parser from </span><a href="http://www.microsoft.com/downloads/details.aspx?FamilyID=890cd06b-abf8-4c25-91b2-f8d975cf8c07&amp;displaylang=en"><span style="font-size:small;font-family:Calibri;">http://www.microsoft.com/downloads/details.aspx?FamilyID=890cd06b-abf8-4c25-91b2-f8d975cf8c07&amp;displaylang=en</span></a></p>
<p class="MsoNormal" style="margin:0;"><span style="font-size:small;font-family:Calibri;"> </span></p>
<p class="MsoNormal" style="margin:0;"><span style="font-size:small;"><span style="font-family:Calibri;">Create a database and a table in SQL.</span></span></p>
<p class="MsoNormal" style="margin:0;"><span style="font-size:small;font-family:Calibri;"> </span></p>
<p class="MsoNormal" style="margin:0;"><span style="font-size:small;"><span style="font-family:Calibri;">Run the following Query</span></span></p>
<p class="MsoNormal" style="margin:0;"><span style="font-size:10pt;color:blue;font-family:'Courier New';"> </span></p>
<p class="MsoNormal" style="margin:0;"><span style="font-size:10pt;color:blue;font-family:'Courier New';">Use</span><span style="font-size:10pt;font-family:'Courier New';"> <span style="color:blue;">master</span></span></p>
<p class="MsoNormal" style="margin:0;"><span style="font-size:10pt;color:blue;font-family:'Courier New';">Go</span></p>
<p class="MsoNormal" style="margin:0;"><span style="font-size:10pt;color:blue;font-family:'Courier New';">Create</span><span style="font-size:10pt;font-family:'Courier New';"> <span style="color:blue;">Database</span> MyEventDataBase</span></p>
<p class="MsoNormal" style="margin:0;"><span style="font-size:10pt;color:blue;font-family:'Courier New';">Go</span></p>
<p class="MsoNormal" style="margin:0;"><span style="font-size:10pt;color:blue;font-family:'Courier New';"> </span></p>
<p class="MsoNormal" style="margin:0;"><span style="font-size:10pt;color:blue;font-family:'Courier New';">use</span><span style="font-size:10pt;font-family:'Courier New';"> MyEventDataBase</span></p>
<p class="MsoNormal" style="margin:0;"><span style="font-size:10pt;color:blue;font-family:'Courier New';">go</span></p>
<p class="MsoNormal" style="margin:0;"><span style="font-size:10pt;color:blue;font-family:'Courier New';">CREATE</span><span style="font-size:10pt;font-family:'Courier New';"> <span style="color:blue;">TABLE</span> [Audit]<span style="color:blue;"> </span><span style="color:gray;">(</span></span></p>
<p class="MsoNormal" style="margin:0;"><span style="font-size:10pt;font-family:'Courier New';"><span>      </span>[EventLog] [varchar]<span style="color:blue;"> </span><span style="color:gray;">(</span>255<span style="color:gray;">)</span> <span style="color:gray;">NULL</span> <span style="color:gray;">,</span></span></p>
<p class="MsoNormal" style="margin:0;"><span style="font-size:10pt;font-family:'Courier New';"><span>      </span>[RecordNumber] [int] <span style="color:gray;">NULL</span> <span style="color:gray;">,</span></span></p>
<p class="MsoNormal" style="margin:0;"><span style="font-size:10pt;font-family:'Courier New';"><span>      </span>[TimeGenerated] [datetime] <span style="color:gray;">NULL</span> <span style="color:gray;">,</span></span></p>
<p class="MsoNormal" style="margin:0;"><span style="font-size:10pt;font-family:'Courier New';"><span>      </span>[TimeWritten] [datetime] <span style="color:gray;">NULL</span> <span style="color:gray;">,</span></span></p>
<p class="MsoNormal" style="margin:0;"><span style="font-size:10pt;font-family:'Courier New';"><span>      </span>[EventID] [int] <span style="color:gray;">NULL</span> <span style="color:gray;">,</span></span></p>
<p class="MsoNormal" style="margin:0;"><span style="font-size:10pt;font-family:'Courier New';"><span>      </span>[EventType] [int] <span style="color:gray;">NULL</span> <span style="color:gray;">,</span></span></p>
<p class="MsoNormal" style="margin:0;"><span style="font-size:10pt;font-family:'Courier New';"><span>      </span>[EventTypeName] [varchar]<span style="color:blue;"> </span><span style="color:gray;">(</span>255<span style="color:gray;">)</span> <span style="color:gray;">NULL</span> <span style="color:gray;">,</span></span></p>
<p class="MsoNormal" style="margin:0;"><span style="font-size:10pt;font-family:'Courier New';"><span>      </span>[EventCategory] [int] <span style="color:gray;">NULL</span> <span style="color:gray;">,</span></span></p>
<p class="MsoNormal" style="margin:0;"><span style="font-size:10pt;font-family:'Courier New';"><span>      </span>[EventCategoryName] [varchar]<span style="color:blue;"> </span><span style="color:gray;">(</span>255<span style="color:gray;">)</span> <span style="color:gray;">NULL</span> <span style="color:gray;">,</span></span></p>
<p class="MsoNormal" style="margin:0;"><span style="font-size:10pt;font-family:'Courier New';"><span>      </span>[SourceName] [varchar]<span style="color:blue;"> </span><span style="color:gray;">(</span>255<span style="color:gray;">)</span> <span style="color:gray;">NULL</span> <span style="color:gray;">,</span></span></p>
<p class="MsoNormal" style="margin:0;"><span style="font-size:10pt;font-family:'Courier New';"><span>      </span>[Strings] [varchar]<span style="color:blue;"> </span><span style="color:gray;">(</span>max<span style="color:gray;">)</span> <span style="color:gray;">NULL</span> <span style="color:gray;">,</span></span></p>
<p class="MsoNormal" style="margin:0;"><span style="font-size:10pt;font-family:'Courier New';"><span>      </span>[ComputerName] [varchar]<span style="color:blue;"> </span><span style="color:gray;">(</span>255<span style="color:gray;">)</span> <span style="color:gray;">NULL</span> <span style="color:gray;">,</span></span></p>
<p class="MsoNormal" style="margin:0;"><span style="font-size:10pt;font-family:'Courier New';"><span>      </span>[SID] [varchar]<span style="color:blue;"> </span><span style="color:gray;">(</span>255<span style="color:gray;">)</span> <span style="color:gray;">NULL</span> <span style="color:gray;">,</span></span></p>
<p class="MsoNormal" style="margin:0;"><span style="font-size:10pt;font-family:'Courier New';"><span>      </span>[Message] [varchar]<span style="color:blue;"> </span><span style="color:gray;">(</span>max<span style="color:gray;">)</span> <span style="color:gray;">NULL</span> <span style="color:gray;">,</span></span></p>
<p class="MsoNormal" style="margin:0;"><span style="font-size:10pt;font-family:'Courier New';"><span>      </span>[Data] [varchar]<span style="color:blue;"> </span><span style="color:gray;">(</span>max<span style="color:gray;">)</span> <span style="color:gray;">NULL</span> </span></p>
<p class="MsoNormal" style="margin:0;"><span style="font-size:10pt;color:gray;font-family:'Courier New';">)</span><span style="font-size:10pt;font-family:'Courier New';"><span>  </span></span></p>
<p class="MsoNormal" style="margin:0;"><span style="font-size:10pt;color:blue;font-family:'Courier New';">GO</span></p>
<p class="MsoNormal" style="margin:0;"><span style="font-size:10pt;color:blue;font-family:'Courier New';"> </span></p>
<p class="MsoNormal" style="margin:0;"><span style="font-size:small;font-family:Calibri;"> </span></p>
<p class="MsoNormal" style="margin:0;"><span style="font-size:small;"><span style="font-family:Calibri;">Make a directory called LogImport in C drive.</span></span></p>
<p class="MsoNormal" style="margin:0;"><span style="font-size:small;font-family:Calibri;"> </span></p>
<p class="MsoNormal" style="margin:0;"><span style="font-size:small;"><span style="font-family:Calibri;">Make a bat file with the following entries:</span></span></p>
<p class="MsoNormal" style="margin:0;"><span style="font-size:small;font-family:Calibri;"> </span></p>
<p class="MsoNormal" style="margin:0;"><span style="font-family:'Courier New';"><span style="font-size:small;"> </span></span></p>
<p class="MsoNormal" style="margin:0;"><span style="font-family:'Courier New';"><span style="font-size:small;">copy <span style="background:yellow;">\\LogMachineName\Logs\EventLog.evtx</span> .<span>  </span>/Y</span></span></p>
<p class="MsoNormal" style="margin:0;"><span style="font-family:'Courier New';"><span style="font-size:small;"> </span></span></p>
<p class="MsoNormal" style="margin:0;"><span style="font-family:'Courier New';"><span style="font-size:small;">&#8220;<span style="background:yellow;">c:\Program Files (x86)\Log Parser 2.2\</span>logparser.exe&#8221; -i:evt &#8220;select<span>  </span>* into Audit from<span>  </span><span style="background:yellow;">c:\logImport\</span>eventlog.evtx&#8221; -iCheckPoint:CheckPoint.lpc -o:SQL -oConnString: &#8220;Driver={SQL Server Native Client 10.0};<span style="background:lime;">server=RAFAT20082;Database=DownloadEventLog;Trusted_Connection=yes;</span>&#8220;</span></span></p>
<p class="MsoNormal" style="margin:0;"><span style="font-family:'Courier New';"><span style="font-size:small;">del *.evtx /Q</span></span></p>
<p class="MsoNormal" style="margin:0;"><span style="font-family:'Courier New';"><span style="font-size:small;"> </span></span></p>
<p class="MsoNormal" style="margin:0;"><span style="font-size:small;"><span style="font-family:Calibri;">NOTE: Pay attention to the highlighted area and fix the path as per your environment. </span></span></p>
<p class="MsoNormal" style="margin:0;"><span style="font-size:small;font-family:Calibri;"> </span></p>
<p class="MsoNormal" style="margin:0;"><span style="font-size:small;"><span style="font-family:Calibri;">Check help for LogParser and play with other options, it is a powerful tool; its numerous options will always give you a solution for your Log problems.</span></span></p>
</div>
</div>
  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sarosh.wordpress.com/37/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sarosh.wordpress.com/37/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sarosh.wordpress.com/37/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sarosh.wordpress.com/37/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sarosh.wordpress.com/37/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sarosh.wordpress.com/37/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sarosh.wordpress.com/37/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sarosh.wordpress.com/37/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sarosh.wordpress.com/37/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sarosh.wordpress.com/37/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sarosh.wordpress.com&blog=1119187&post=37&subd=sarosh&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://sarosh.wordpress.com/2009/02/27/log-parser-and-sql/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/d8e93bef927d029c824b91da0b59f703?s=96&#38;d=http%3A%2F%2F1.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96&#38;r=G" medium="image">
			<media:title type="html">rsarosh</media:title>
		</media:content>
	</item>
		<item>
		<title>Life saver queries</title>
		<link>http://sarosh.wordpress.com/2009/01/16/life-saver-queries/</link>
		<comments>http://sarosh.wordpress.com/2009/01/16/life-saver-queries/#comments</comments>
		<pubDate>Fri, 16 Jan 2009 16:39:27 +0000</pubDate>
		<dc:creator>Rafat Sarosh</dc:creator>
				<category><![CDATA[SQL]]></category>

		<guid isPermaLink="false">http://sarosh.wordpress.com/?p=30</guid>
		<description><![CDATA[These queries can save your life, at least they saved mine.
                                                                    Query to find the query using the most CPU:
select  
    highest_cpu_queries.plan_handle,  
    highest_cpu_queries.total_worker_time, 
    q.dbid, 
    q.objectid, 
    q.number, 
    q.encrypted, 
    q.[text] 
from  
    (select top 10  
        qs.plan_handle,  
        qs.total_worker_time 
    from  
        sys.dm_exec_query_stats qs 
    order by qs.total_worker_time desc) as highest_cpu_queries [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sarosh.wordpress.com&blog=1119187&post=30&subd=sarosh&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>These queries can save your life, at least they saved mine.</p>
<p><strong><span style="font-family:'Courier New';"><span><span style="font:7pt 'Times New Roman';">                                                                    </span></span></span><span style="font-size:small;font-family:Calibri;">Query to find the query using the most CPU:</span></strong></p>
<p class="MsoNormal" style="margin:0 0 0 1.5in;"><span style="font-size:10pt;color:blue;font-family:'Courier New';">select</span><span style="font-size:10pt;font-family:'Courier New';">  </span></p>
<p class="MsoNormal" style="margin:0 0 0 1.5in;"><span style="font-size:10pt;font-family:'Courier New';">    highest_cpu_queries<span style="color:gray;">.</span>plan_handle<span style="color:gray;">,</span>  </span></p>
<p class="MsoNormal" style="margin:0 0 0 1.5in;"><span style="font-size:10pt;font-family:'Courier New';">    highest_cpu_queries<span style="color:gray;">.</span>total_worker_time<span style="color:gray;">,</span> </span></p>
<p class="MsoNormal" style="margin:0 0 0 1.5in;"><span style="font-size:10pt;font-family:'Courier New';">    q<span style="color:gray;">.</span><span style="color:blue;">dbid</span><span style="color:gray;">,</span> </span></p>
<p class="MsoNormal" style="margin:0 0 0 1.5in;"><span style="font-size:10pt;font-family:'Courier New';">    q<span style="color:gray;">.</span>objectid<span style="color:gray;">,</span> </span></p>
<p class="MsoNormal" style="margin:0 0 0 1.5in;"><span style="font-size:10pt;font-family:'Courier New';">    q<span style="color:gray;">.</span>number<span style="color:gray;">,</span> </span></p>
<p class="MsoNormal" style="margin:0 0 0 1.5in;"><span style="font-size:10pt;font-family:'Courier New';">    q<span style="color:gray;">.</span>encrypted<span style="color:gray;">,</span> </span></p>
<p class="MsoNormal" style="margin:0 0 0 1.5in;"><span style="font-size:10pt;font-family:'Courier New';">    q<span style="color:gray;">.</span>[text] </span></p>
<p class="MsoNormal" style="margin:0 0 0 1.5in;"><span style="font-size:10pt;color:blue;font-family:'Courier New';">from</span><span style="font-size:10pt;font-family:'Courier New';">  </span></p>
<p class="MsoNormal" style="margin:0 0 0 1.5in;"><span style="font-size:10pt;color:blue;font-family:'Courier New';">    </span><span style="font-size:10pt;color:gray;font-family:'Courier New';">(</span><span style="font-size:10pt;color:blue;font-family:'Courier New';">select</span><span style="font-size:10pt;font-family:'Courier New';"> <span style="color:blue;">top</span> 10  </span></p>
<p class="MsoNormal" style="margin:0 0 0 1.5in;"><span style="font-size:10pt;font-family:'Courier New';">        qs<span style="color:gray;">.</span>plan_handle<span style="color:gray;">,</span>  </span></p>
<p class="MsoNormal" style="margin:0 0 0 1.5in;"><span style="font-size:10pt;font-family:'Courier New';">        qs<span style="color:gray;">.</span>total_worker_time </span></p>
<p class="MsoNormal" style="margin:0 0 0 1.5in;"><span style="font-size:10pt;font-family:'Courier New';">    <span style="color:blue;">from</span>  </span></p>
<p class="MsoNormal" style="margin:0 0 0 1.5in;"><span style="font-size:10pt;font-family:'Courier New';">        <span style="color:green;">sys</span><span style="color:gray;">.</span><span style="color:green;">dm_exec_query_stats</span> qs </span></p>
<p class="MsoNormal" style="margin:0 0 0 1.5in;"><span style="font-size:10pt;font-family:'Courier New';">    <span style="color:blue;">order</span> <span style="color:blue;">by</span> qs<span style="color:gray;">.</span>total_worker_time <span style="color:blue;">desc</span><span style="color:gray;">)</span> <span style="color:blue;">as</span> highest_cpu_queries </span></p>
<p class="MsoNormal" style="margin:0 0 0 1.5in;"><span style="font-size:10pt;font-family:'Courier New';">    <span style="color:gray;">cross</span> <span style="color:gray;">apply</span> <span style="color:green;">sys</span><span style="color:gray;">.</span><span style="color:green;">dm_exec_sql_text</span><span style="color:gray;">(</span>plan_handle<span style="color:gray;">)</span> <span style="color:blue;">as</span> q </span></p>
<p class="MsoNormal" style="margin:0 0 0 1.5in;"><span style="font-size:10pt;color:blue;font-family:'Courier New';">order</span><span style="font-size:10pt;font-family:'Courier New';"> <span style="color:blue;">by</span> highest_cpu_queries<span style="color:gray;">.</span>total_worker_time <span style="color:blue;">desc</span></span></p>
<p class="MsoNormal" style="margin:0 0 0 1.5in;"> </p>
<p class="MsoNormal" style="margin:0 0 0 1.5in;"><span style="font-size:small;font-family:Calibri;"><strong>Find out which Indexes are not used: </strong>Get rid of them, you are paying for  them.<strong> </strong></span></p>
<p class="MsoNormal" style="margin:0 0 0 1.5in;"><span style="font-size:10pt;color:blue;font-family:'Courier New';">select</span><span style="font-size:10pt;font-family:'Courier New';"> <span style="color:fuchsia;">object_name</span><span style="color:gray;">(</span>i<span style="color:gray;">.</span><span style="color:fuchsia;">object_id</span><span style="color:gray;">),</span> </span></p>
<p class="MsoNormal" style="margin:0 0 0 1.5in;"><span style="font-size:10pt;font-family:'Courier New';">i<span style="color:gray;">.</span>name<span style="color:gray;">,</span> </span></p>
<p class="MsoNormal" style="margin:0 0 0 1.5in;"><span style="font-size:10pt;font-family:'Courier New';">s<span style="color:gray;">.</span>user_updates<span style="color:gray;">,</span> </span></p>
<p class="MsoNormal" style="margin:0 0 0 1.5in;"><span style="font-size:10pt;font-family:'Courier New';">s<span style="color:gray;">.</span>user_seeks<span style="color:gray;">,</span> </span></p>
<p class="MsoNormal" style="margin:0 0 0 1.5in;"><span style="font-size:10pt;font-family:'Courier New';">s<span style="color:gray;">.</span>user_scans<span style="color:gray;">,</span> </span></p>
<p class="MsoNormal" style="margin:0 0 0 1.5in;"><span style="font-size:10pt;font-family:'Courier New';">s<span style="color:gray;">.</span>user_lookups</span></p>
<p class="MsoNormal" style="margin:0 0 0 1.5in;"><span style="font-size:10pt;color:blue;font-family:'Courier New';">from</span><span style="font-size:10pt;font-family:'Courier New';"> <span style="color:green;">sys</span><span style="color:gray;">.</span><span style="color:green;">indexes</span> i  </span></p>
<p class="MsoNormal" style="margin:0 0 0 1.5in;"><span style="font-size:10pt;font-family:'Courier New';">            <span style="color:gray;">left</span> <span style="color:gray;">join</span> <span style="color:green;">sys</span><span style="color:gray;">.</span><span style="color:green;">dm_db_index_usage_stats</span> s </span></p>
<p class="MsoNormal" style="margin:0 0 0 1.5in;"><span style="font-size:10pt;color:blue;font-family:'Courier New';">on</span><span style="font-size:10pt;font-family:'Courier New';"> s<span style="color:gray;">.</span><span style="color:fuchsia;">object_id</span> <span style="color:gray;">=</span> i<span style="color:gray;">.</span><span style="color:fuchsia;">object_id</span> <span style="color:gray;">and</span>  </span></p>
<p class="MsoNormal" style="margin:0 0 0 1.5in;"><span style="font-size:10pt;font-family:'Courier New';">                  i<span style="color:gray;">.</span>index_id <span style="color:gray;">=</span> s<span style="color:gray;">.</span>index_id <span style="color:gray;">and</span> s<span style="color:gray;">.</span>database_id <span style="color:gray;">=</span> <span style="color:fuchsia;">DB_ID</span><span style="color:gray;">(</span><span style="color:red;">&#8216;ILT_Stage&#8217;</span><span style="color:gray;">)</span></span></p>
<p class="MsoNormal" style="margin:0 0 0 1.5in;"><span style="font-size:10pt;color:blue;font-family:'Courier New';">where</span><span style="font-size:10pt;font-family:'Courier New';"> <span style="color:fuchsia;">objectproperty</span><span style="color:gray;">(</span>i<span style="color:gray;">.</span><span style="color:fuchsia;">object_id</span><span style="color:gray;">,</span> <span style="color:red;">&#8216;IsIndexable&#8217;</span><span style="color:gray;">)</span> <span style="color:gray;">=</span> 1 <span style="color:gray;">and</span></span></p>
<p class="MsoNormal" style="margin:0 0 0 1.5in;"><span style="font-size:10pt;color:green;font-family:'Courier New';">&#8211; index_usage_stats has no reference to this index (not being used)</span></p>
<p class="MsoNormal" style="margin:0 0 0 1.5in;"><span style="font-size:10pt;font-family:'Courier New';">s<span style="color:gray;">.</span>index_id <span style="color:gray;">is</span> <span style="color:gray;">null</span> <span style="color:gray;">or</span></span></p>
<p class="MsoNormal" style="margin:0 0 0 1.5in;"><span style="font-size:10pt;color:green;font-family:'Courier New';">&#8211; index is being updated, but not used by seeks/scans/lookups</span></p>
<p class="MsoNormal" style="margin:0 0 0 1.5in;"><span style="font-size:10pt;color:gray;font-family:'Courier New';">(</span><span style="font-size:10pt;font-family:'Courier New';">s<span style="color:gray;">.</span>user_updates <span style="color:gray;">&gt;</span> 0 <span style="color:gray;">and</span> s<span style="color:gray;">.</span>user_seeks <span style="color:gray;">=</span> 0 </span></p>
<p class="MsoNormal" style="margin:0 0 0 1.5in;"><span style="font-size:10pt;color:gray;font-family:'Courier New';">and</span><span style="font-size:10pt;font-family:'Courier New';"> s<span style="color:gray;">.</span>user_scans <span style="color:gray;">=</span> 0 <span style="color:gray;">and</span> s<span style="color:gray;">.</span>user_lookups <span style="color:gray;">=</span> 0<span style="color:gray;">)</span></span></p>
<p class="MsoNormal" style="margin:0 0 0 1.5in;"><span style="font-size:10pt;color:blue;font-family:'Courier New';">order</span><span style="font-size:10pt;font-family:'Courier New';"> <span style="color:blue;">by</span> <span style="color:fuchsia;">object_name</span><span style="color:gray;">(</span>i<span style="color:gray;">.</span><span style="color:fuchsia;">object_id</span><span style="color:gray;">)</span> <span style="color:blue;">asc</span></span></p>
<p class="MsoNormal" style="margin:0 0 0 1.5in;"> </p>
<p class="MsoListParagraph" style="text-indent:-.25in;margin:0 0 0 1in;"><strong><span style="font-family:'Courier New';"><span><span style="font:7pt 'Times New Roman';">                                   </span></span></span><span style="font-size:small;font-family:Calibri;">Find out missing indexes:</span></strong></p>
<p class="MsoNormal" style="margin:0 0 0 1.5in;"><span style="font-size:10pt;color:blue;font-family:'Courier New';">SELECT</span><span style="font-size:10pt;font-family:'Courier New';"> </span></p>
<p class="MsoNormal" style="margin:0 0 0 1.5in;"><span style="font-size:10pt;font-family:'Courier New';">  mig<span style="color:gray;">.</span>index_group_handle<span style="color:gray;">,</span> mid<span style="color:gray;">.</span>index_handle<span style="color:gray;">,</span> </span></p>
<p class="MsoNormal" style="margin:0 0 0 1.5in;"><span style="font-size:10pt;font-family:'Courier New';">  <span style="color:fuchsia;">CONVERT</span><span style="color:blue;"> </span><span style="color:gray;">(</span><span style="color:blue;">decimal </span><span style="color:gray;">(</span>28<span style="color:gray;">,</span>1<span style="color:gray;">),</span> </span></p>
<p class="MsoNormal" style="margin:0 0 0 1.5in;"><span style="font-size:10pt;font-family:'Courier New';">    migs<span style="color:gray;">.</span>avg_total_user_cost <span style="color:gray;">*</span> migs<span style="color:gray;">.</span>avg_user_impact <span style="color:gray;">*</span><span style="color:blue;"> </span><span style="color:gray;">(</span>migs<span style="color:gray;">.</span>user_seeks <span style="color:gray;">+</span> migs<span style="color:gray;">.</span>user_scans<span style="color:gray;">)</span></span></p>
<p class="MsoNormal" style="margin:0 0 0 1.5in;"><span style="font-size:10pt;font-family:'Courier New';">  <span style="color:gray;">)</span> <span style="color:blue;">AS</span> improvement_measure<span style="color:gray;">,</span> </span></p>
<p class="MsoNormal" style="margin:0 0 0 1.5in;"><span style="font-size:10pt;font-family:'Courier New';">  <span style="color:red;">&#8216;CREATE INDEX missing_index_&#8217;</span> <span style="color:gray;">+</span> <span style="color:fuchsia;">CONVERT</span><span style="color:blue;"> </span><span style="color:gray;">(</span><span style="color:blue;">varchar</span><span style="color:gray;">,</span> mig<span style="color:gray;">.</span>index_group_handle<span style="color:gray;">)</span> <span style="color:gray;">+</span> <span style="color:red;">&#8216;_&#8217;</span> <span style="color:gray;">+</span> <span style="color:fuchsia;">CONVERT</span><span style="color:blue;"> </span><span style="color:gray;">(</span><span style="color:blue;">varchar</span><span style="color:gray;">,</span> mid<span style="color:gray;">.</span>index_handle<span style="color:gray;">)</span> </span></p>
<p class="MsoNormal" style="margin:0 0 0 1.5in;"><span style="font-size:10pt;font-family:'Courier New';">  <span style="color:gray;">+</span> <span style="color:red;">&#8216; ON &#8216;</span> <span style="color:gray;">+</span> mid<span style="color:gray;">.</span><span style="color:blue;">statement</span> </span></p>
<p class="MsoNormal" style="margin:0 0 0 1.5in;"><span style="font-size:10pt;font-family:'Courier New';">  <span style="color:gray;">+</span> <span style="color:red;">&#8216; (&#8216;</span> <span style="color:gray;">+</span> <span style="color:fuchsia;">ISNULL</span><span style="color:blue;"> </span><span style="color:gray;">(</span>mid<span style="color:gray;">.</span>equality_columns<span style="color:gray;">,</span><span style="color:red;">&#8221;</span><span style="color:gray;">)</span> </span></p>
<p class="MsoNormal" style="margin:0 0 0 1.5in;"><span style="font-size:10pt;font-family:'Courier New';">    <span style="color:gray;">+</span> <span style="color:blue;">CASE</span> <span style="color:blue;">WHEN</span> mid<span style="color:gray;">.</span>equality_columns <span style="color:gray;">IS</span> <span style="color:gray;">NOT</span> <span style="color:gray;">NULL</span> <span style="color:gray;">AND</span> mid<span style="color:gray;">.</span>inequality_columns <span style="color:gray;">IS</span> <span style="color:gray;">NOT</span> <span style="color:gray;">NULL</span> <span style="color:blue;">THEN</span> <span style="color:red;">&#8216;,&#8217;</span> <span style="color:blue;">ELSE</span> <span style="color:red;">&#8221;</span> <span style="color:blue;">END</span> </span></p>
<p class="MsoNormal" style="margin:0 0 0 1.5in;"><span style="font-size:10pt;font-family:'Courier New';">    <span style="color:gray;">+</span> <span style="color:fuchsia;">ISNULL</span><span style="color:blue;"> </span><span style="color:gray;">(</span>mid<span style="color:gray;">.</span>inequality_columns<span style="color:gray;">,</span> <span style="color:red;">&#8221;</span><span style="color:gray;">)</span></span></p>
<p class="MsoNormal" style="margin:0 0 0 1.5in;"><span style="font-size:10pt;font-family:'Courier New';">  <span style="color:gray;">+</span> <span style="color:red;">&#8216;)&#8217;</span> </span></p>
<p class="MsoNormal" style="margin:0 0 0 1.5in;"><span style="font-size:10pt;font-family:'Courier New';">  <span style="color:gray;">+</span> <span style="color:fuchsia;">ISNULL</span><span style="color:blue;"> </span><span style="color:gray;">(</span><span style="color:red;">&#8216; INCLUDE (&#8216;</span> <span style="color:gray;">+</span> mid<span style="color:gray;">.</span>included_columns <span style="color:gray;">+</span> <span style="color:red;">&#8216;)&#8217;</span><span style="color:gray;">,</span> <span style="color:red;">&#8221;</span><span style="color:gray;">)</span> <span style="color:blue;">AS</span> create_index_statement<span style="color:gray;">,</span> </span></p>
<p class="MsoNormal" style="margin:0 0 0 1.5in;"><span style="font-size:10pt;font-family:'Courier New';">  migs<span style="color:gray;">.*,</span> mid<span style="color:gray;">.</span>database_id<span style="color:gray;">,</span> mid<span style="color:gray;">.</span>[object_id]</span></p>
<p class="MsoNormal" style="margin:0 0 0 1.5in;"><span style="font-size:10pt;color:blue;font-family:'Courier New';">FROM</span><span style="font-size:10pt;font-family:'Courier New';"> <span style="color:green;">sys</span><span style="color:gray;">.</span><span style="color:green;">dm_db_missing_index_groups</span> mig</span></p>
<p class="MsoNormal" style="margin:0 0 0 1.5in;"><span style="font-size:10pt;color:gray;font-family:'Courier New';">INNER</span><span style="font-size:10pt;font-family:'Courier New';"> <span style="color:gray;">JOIN</span> <span style="color:green;">sys</span><span style="color:gray;">.</span><span style="color:green;">dm_db_missing_index_group_stats</span> migs <span style="color:blue;">ON</span> migs<span style="color:gray;">.</span>group_handle <span style="color:gray;">=</span> mig<span style="color:gray;">.</span>index_group_handle</span></p>
<p class="MsoNormal" style="margin:0 0 0 1.5in;"><span style="font-size:10pt;color:gray;font-family:'Courier New';">INNER</span><span style="font-size:10pt;font-family:'Courier New';"> <span style="color:gray;">JOIN</span> <span style="color:green;">sys</span><span style="color:gray;">.</span><span style="color:green;">dm_db_missing_index_details</span> mid <span style="color:blue;">ON</span> mig<span style="color:gray;">.</span>index_handle <span style="color:gray;">=</span> mid<span style="color:gray;">.</span>index_handle</span></p>
<p class="MsoNormal" style="margin:0 0 0 1.5in;"><span style="font-size:10pt;color:blue;font-family:'Courier New';">WHERE</span><span style="font-size:10pt;font-family:'Courier New';"> <span style="color:fuchsia;">CONVERT</span><span style="color:blue;"> </span><span style="color:gray;">(</span><span style="color:blue;">decimal </span><span style="color:gray;">(</span>28<span style="color:gray;">,</span>1<span style="color:gray;">),</span> migs<span style="color:gray;">.</span>avg_total_user_cost <span style="color:gray;">*</span> migs<span style="color:gray;">.</span>avg_user_impact <span style="color:gray;">*</span><span style="color:blue;"> </span><span style="color:gray;">(</span>migs<span style="color:gray;">.</span>user_seeks <span style="color:gray;">+</span> migs<span style="color:gray;">.</span>user_scans<span style="color:gray;">))</span> <span style="color:gray;">&gt;</span> 10</span></p>
<p class="MsoNormal" style="margin:0 0 0 1.5in;"><span style="font-size:10pt;color:blue;font-family:'Courier New';">ORDER</span><span style="font-size:10pt;font-family:'Courier New';"> <span style="color:blue;">BY</span> migs<span style="color:gray;">.</span>avg_total_user_cost <span style="color:gray;">*</span> migs<span style="color:gray;">.</span>avg_user_impact <span style="color:gray;">*</span><span style="color:blue;"> </span><span style="color:gray;">(</span>migs<span style="color:gray;">.</span>user_seeks <span style="color:gray;">+</span> migs<span style="color:gray;">.</span>user_scans<span style="color:gray;">)</span> <span style="color:blue;">DESC</span></span></p>
<p class="MsoNormal" style="margin:0 0 0 1.5in;"> </p>
<p class="MsoListParagraph" style="text-indent:-.25in;margin:0 0 0 .5in;"><strong><span style="font-family:Symbol;"><span><span style="font:7pt 'Times New Roman';">                                                          </span></span></span><span style="font-size:small;font-family:Calibri;">TempDB: </span></strong><span style="font-size:small;font-family:Calibri;"> Queries to find which SQL statements are using Tempdb the most:</span></p>
<p class="MsoNormal" style="margin:0 0 0 1.5in;"><span style="font-size:10pt;color:blue;font-family:'Courier New';">SELECT</span><span style="font-size:10pt;font-family:'Courier New';">  </span></p>
<p class="MsoNormal" style="margin:0 0 0 1.5in;"><span style="font-size:10pt;font-family:'Courier New';">    t1<span style="color:gray;">.</span>session_id<span style="color:gray;">,</span> </span></p>
<p class="MsoNormal" style="margin:0 0 0 1.5in;"><span style="font-size:10pt;color:blue;font-family:'Courier New';">    </span><span style="font-size:10pt;color:gray;font-family:'Courier New';">(</span><span style="font-size:10pt;font-family:'Courier New';">t1<span style="color:gray;">.</span>internal_objects_alloc_page_count <span style="color:gray;">+</span> task_alloc<span style="color:gray;">)</span> <span style="color:blue;">as</span> allocated<span style="color:gray;">,</span> </span></p>
<p class="MsoNormal" style="margin:0 0 0 1.5in;"><span style="font-size:10pt;color:blue;font-family:'Courier New';">    </span><span style="font-size:10pt;color:gray;font-family:'Courier New';">(</span><span style="font-size:10pt;font-family:'Courier New';">t1<span style="color:gray;">.</span>internal_objects_dealloc_page_count <span style="color:gray;">+</span> task_dealloc<span style="color:gray;">)</span> <span style="color:blue;">as</span>     </span></p>
<p class="MsoNormal" style="margin:0 0 0 1.5in;"><span style="font-size:10pt;font-family:'Courier New';">    deallocated  </span></p>
<p class="MsoNormal" style="margin:0 0 0 1.5in;"><span style="font-size:10pt;color:blue;font-family:'Courier New';">from</span><span style="font-size:10pt;font-family:'Courier New';"> <span style="color:green;">sys</span><span style="color:gray;">.</span><span style="color:green;">dm_db_session_space_usage</span> <span style="color:blue;">as</span> t1<span style="color:gray;">,</span>  </span></p>
<p class="MsoNormal" style="margin:0 0 0 1.5in;"><span style="font-size:10pt;color:blue;font-family:'Courier New';">    </span><span style="font-size:10pt;color:gray;font-family:'Courier New';">(</span><span style="font-size:10pt;color:blue;font-family:'Courier New';">select</span><span style="font-size:10pt;font-family:'Courier New';"> session_id<span style="color:gray;">,</span>  </span></p>
<p class="MsoNormal" style="margin:0 0 0 1.5in;"><span style="font-size:10pt;font-family:'Courier New';">        <span style="color:fuchsia;">sum</span><span style="color:gray;">(</span>internal_objects_alloc_page_count<span style="color:gray;">)</span> </span></p>
<p class="MsoNormal" style="margin:0 0 0 1.5in;"><span style="font-size:10pt;font-family:'Courier New';">            <span style="color:blue;">as</span> task_alloc<span style="color:gray;">,</span> </span></p>
<p class="MsoNormal" style="margin:0 0 0 1.5in;"><span style="font-size:10pt;font-family:'Courier New';">    <span style="color:fuchsia;">sum</span><span style="color:blue;"> </span><span style="color:gray;">(</span>internal_objects_dealloc_page_count<span style="color:gray;">)</span> <span style="color:blue;">as</span>  </span></p>
<p class="MsoNormal" style="margin:0 0 0 1.5in;"><span style="font-size:10pt;font-family:'Courier New';">        task_dealloc  </span></p>
<p class="MsoNormal" style="margin:0 0 0 1.5in;"><span style="font-size:10pt;font-family:'Courier New';">      <span style="color:blue;">from</span> <span style="color:green;">sys</span><span style="color:gray;">.</span><span style="color:green;">dm_db_task_space_usage</span> <span style="color:blue;">group</span> <span style="color:blue;">by</span> session_id<span style="color:gray;">)</span> <span style="color:blue;">as</span> t2 </span></p>
<p class="MsoNormal" style="margin:0 0 0 1.5in;"><span style="font-size:10pt;color:blue;font-family:'Courier New';">where</span><span style="font-size:10pt;font-family:'Courier New';"> t1<span style="color:gray;">.</span>session_id <span style="color:gray;">=</span> t2<span style="color:gray;">.</span>session_id <span style="color:gray;">and</span> t1<span style="color:gray;">.</span>session_id <span style="color:gray;">&gt;</span>50 </span></p>
<p class="MsoNormal" style="margin:0 0 0 1.5in;"><span style="font-size:10pt;color:blue;font-family:'Courier New';">order</span><span style="font-size:10pt;font-family:'Courier New';"> <span style="color:blue;">by</span> allocated <span style="color:blue;">DESC</span></span></p>
<p class="MsoNormal" style="margin:0 0 0 1.5in;"> </p>
<p class="MsoNormal" style="margin:0 0 0 1.5in;"><strong><span style="font-family:'Courier New';"><span><span style="font:7pt 'Times New Roman';"> </span></span></span><span style="font-size:small;font-family:Calibri;">Highest CPU queries:</span></strong></p>
<p class="MsoNormal" style="margin:0 0 0 1.5in;"><span style="font-size:10pt;color:blue;font-family:'Courier New';">select</span><span style="font-size:10pt;font-family:'Courier New';">  </span></p>
<p class="MsoNormal" style="margin:0 0 0 1.5in;"><span style="font-size:10pt;font-family:'Courier New';">    t1<span style="color:gray;">.</span>session_id<span style="color:gray;">,</span>  </span></p>
<p class="MsoNormal" style="margin:0 0 0 1.5in;"><span style="font-size:10pt;font-family:'Courier New';">    t1<span style="color:gray;">.</span>request_id<span style="color:gray;">,</span>  </span></p>
<p class="MsoNormal" style="margin:0 0 0 1.5in;"><span style="font-size:10pt;font-family:'Courier New';">    t1<span style="color:gray;">.</span>task_alloc<span style="color:gray;">,</span> </span></p>
<p class="MsoNormal" style="margin:0 0 0 1.5in;"><span style="font-size:10pt;font-family:'Courier New';">    t1<span style="color:gray;">.</span>task_dealloc<span style="color:gray;">,</span> </span></p>
<p class="MsoNormal" style="margin:0 0 0 1.5in;"><span style="font-size:10pt;font-family:'Courier New';">    t2<span style="color:gray;">.</span><span style="color:blue;">sql_handle</span><span style="color:gray;">,</span>  </span></p>
<p class="MsoNormal" style="margin:0 0 0 1.5in;"><span style="font-size:10pt;font-family:'Courier New';">    t2<span style="color:gray;">.</span>statement_start_offset<span style="color:gray;">,</span>  </span></p>
<p class="MsoNormal" style="margin:0 0 0 1.5in;"><span style="font-size:10pt;font-family:'Courier New';">    t2<span style="color:gray;">.</span>statement_end_offset<span style="color:gray;">,</span>  </span></p>
<p class="MsoNormal" style="margin:0 0 0 1.5in;"><span style="font-size:10pt;font-family:'Courier New';">    t2<span style="color:gray;">.</span>plan_handle </span></p>
<p class="MsoNormal" style="margin:0 0 0 1.5in;"><span style="font-size:10pt;color:blue;font-family:'Courier New';">from </span><span style="font-size:10pt;color:gray;font-family:'Courier New';">(</span><span style="font-size:10pt;color:blue;font-family:'Courier New';">Select</span><span style="font-size:10pt;font-family:'Courier New';"> session_id<span style="color:gray;">,</span>  </span></p>
<p class="MsoNormal" style="margin:0 0 0 1.5in;"><span style="font-size:10pt;font-family:'Courier New';">             request_id<span style="color:gray;">,</span> </span></p>
<p class="MsoNormal" style="margin:0 0 0 1.5in;"><span style="font-size:10pt;font-family:'Courier New';">             <span style="color:fuchsia;">sum</span><span style="color:gray;">(</span>internal_objects_alloc_page_count<span style="color:gray;">)</span> <span style="color:blue;">as</span> task_alloc<span style="color:gray;">,</span> </span></p>
<p class="MsoNormal" style="margin:0 0 0 1.5in;"><span style="font-size:10pt;font-family:'Courier New';">             <span style="color:fuchsia;">sum</span><span style="color:blue;"> </span><span style="color:gray;">(</span>internal_objects_dealloc_page_count<span style="color:gray;">)</span> <span style="color:blue;">as</span> task_dealloc  </span></p>
<p class="MsoNormal" style="margin:0 0 0 1.5in;"><span style="font-size:10pt;font-family:'Courier New';">      <span style="color:blue;">from</span> <span style="color:green;">sys</span><span style="color:gray;">.</span><span style="color:green;">dm_db_task_space_usage</span>  </span></p>
<p class="MsoNormal" style="margin:0 0 0 1.5in;"><span style="font-size:10pt;font-family:'Courier New';">      <span style="color:blue;">group</span> <span style="color:blue;">by</span> session_id<span style="color:gray;">,</span> request_id<span style="color:gray;">)</span> <span style="color:blue;">as</span> t1<span style="color:gray;">,</span>  </span></p>
<p class="MsoNormal" style="margin:0 0 0 1.5in;"><span style="font-size:10pt;font-family:'Courier New';">      <span style="color:green;">sys</span><span style="color:gray;">.</span><span style="color:green;">dm_exec_requests</span> <span style="color:blue;">as</span> t2 </span></p>
<p class="MsoNormal" style="margin:0 0 0 1.5in;"><span style="font-size:10pt;color:blue;font-family:'Courier New';">where</span><span style="font-size:10pt;font-family:'Courier New';"> t1<span style="color:gray;">.</span>session_id <span style="color:gray;">=</span> t2<span style="color:gray;">.</span>session_id <span style="color:gray;">and</span>  </span></p>
<p class="MsoNormal" style="margin:0 0 0 1.5in;"><span style="font-size:10pt;color:blue;font-family:'Courier New';">     </span><span style="font-size:10pt;color:gray;font-family:'Courier New';">(</span><span style="font-size:10pt;font-family:'Courier New';">t1<span style="color:gray;">.</span>request_id <span style="color:gray;">=</span> t2<span style="color:gray;">.</span>request_id<span style="color:gray;">)</span> </span></p>
<p class="MsoNormal" style="margin:0 0 0 1.5in;"><span style="font-size:10pt;color:blue;font-family:'Courier New';">order</span><span style="font-size:10pt;font-family:'Courier New';"> <span style="color:blue;">by</span> t1<span style="color:gray;">.</span>task_alloc <span style="color:blue;">DESC</span></span></p>
  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sarosh.wordpress.com/30/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sarosh.wordpress.com/30/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sarosh.wordpress.com/30/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sarosh.wordpress.com/30/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sarosh.wordpress.com/30/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sarosh.wordpress.com/30/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sarosh.wordpress.com/30/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sarosh.wordpress.com/30/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sarosh.wordpress.com/30/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sarosh.wordpress.com/30/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sarosh.wordpress.com&blog=1119187&post=30&subd=sarosh&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://sarosh.wordpress.com/2009/01/16/life-saver-queries/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/d8e93bef927d029c824b91da0b59f703?s=96&#38;d=http%3A%2F%2F1.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96&#38;r=G" medium="image">
			<media:title type="html">rsarosh</media:title>
		</media:content>
	</item>
	</channel>
</rss>