<?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:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Motherofallgeeks</title>
	<atom:link href="http://motherofallgeeks.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://motherofallgeeks.com</link>
	<description>SQL Server Articles on performance, maintenance,architecture and more</description>
	<lastBuildDate>Fri, 06 Mar 2009 19:05:16 +0000</lastBuildDate>
	<generator>http://wordpress.com/</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<image>
		<url>http://www.gravatar.com/blavatar/0396c4bd72b66833b5a586b45000c314?s=96&#038;d=http://s.wordpress.com/i/buttonw-com.png</url>
		<title>Motherofallgeeks</title>
		<link>http://motherofallgeeks.com</link>
	</image>
			<item>
		<title>Simple Space Checker</title>
		<link>http://motherofallgeeks.com/2009/03/07/simple-space-checker/</link>
		<comments>http://motherofallgeeks.com/2009/03/07/simple-space-checker/#comments</comments>
		<pubDate>Fri, 06 Mar 2009 19:05:16 +0000</pubDate>
		<dc:creator>motherofallgeeks</dc:creator>
				<category><![CDATA[Monitoring]]></category>
		<category><![CDATA[alter database]]></category>
		<category><![CDATA[data file]]></category>
		<category><![CDATA[file allocation]]></category>
		<category><![CDATA[file comsumption]]></category>
		<category><![CDATA[index file]]></category>
		<category><![CDATA[ldf]]></category>
		<category><![CDATA[mdf]]></category>
		<category><![CDATA[ndf]]></category>
		<category><![CDATA[Space Checking]]></category>
		<category><![CDATA[Space Monitor]]></category>
		<category><![CDATA[System Stored Proc]]></category>

		<guid isPermaLink="false">http://motherofallgeeks.com/?p=66</guid>
		<description><![CDATA[I recently had someone ask me how to I see my actual usage of data and index files without using the GUI.  Well I am going to tell you that and I am also going to tell you how to set up a monitor job to alert  you when your file is approaching it&#8217;s capacity.  [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=motherofallgeeks.com&blog=5421765&post=66&subd=motherofallgeeks&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>I recently had someone ask me how to I see my actual usage of data and index files without using the GUI.  Well I am going to tell you that and I am also going to tell you how to set up a monitor job to alert  you when your file is approaching it&#8217;s capacity.  Why would you want to do that when you have AutoGrow? The first reason is that Auto grow will trigger an ALTER Database statement. You have no control over the time of day that this alter will fire.  Alter will Lock your system up until it has completed. This type of maintenance should be monitored and performed at a time when system impact will be less.  If auto grow is enabled and you are not monitoring your database space you could eat up all of the drive space and crash the server. DBA&#8217;s should be aware of how fast your systems are growing and proactively add the space as needed.</p>
<p>That being said. This script was developed for SQL Server 2005. It uses undocumented processes which are not recommended for use by microsoft because they may be removed at anytime.</p>
<p>There is an undocumented DBCC command which has been around for quite some time. We will use this command dbcc showfilestats as the basis of our script.</p>
<p>We are going to create a system stored procedure. That way you can run this proc from any database without having to create it in all databases. It makes maintenance much easier. You can schedule it to run from SQL Agent every 30 minutes and it will send you a mail based on your file reaching 90%.</p>
<p>Enjoy!</p>
<p style="padding-left:30px;">set ANSI_NULLS ON<br />
set QUOTED_IDENTIFIER ON<br />
go</p>
<p>/*******************************************************************************************<br />
* proc_checkspace<br />
* Created By: MotherofallGeeks<br />
* Created Date: March 2008<br />
* Purpose check available Free space in database and send alert<br />
* exec proc_CheckSpace &#8216;MyMailProfile&#8217; , &#8216;Mother@motherofallgeeks.com&#8217;<br />
*/</p>
<p>CREATE PROC [dbo].[sp_CheckSpace]</p>
<p>@MailProfile varchar(max),<br />
@recipients varchar(max)<br />
AS<br />
BEGIN<br />
SET NOCOUNT ON</p>
<p>DECLARE @DataUsedPercent float<br />
DECLARE @IndexUsedPercent float<br />
DECLARE @FileName varchar(max)<br />
DECLARE @Size numeric<br />
DECLARE @IndexSize float<br />
DECLARE @UsedSpace float</p>
<p>DECLARE @tmpspc table<br />
(Fileid int, FileGroup int,<br />
TotalExtents int,<br />
UsedExtents int,<br />
Name sysname,<br />
FileName nchar(520))</p>
<p>INSERT @tmpspc EXEC (&#8217;dbcc showfilestats&#8217;)</p>
<p>SELECT<br />
@FileName = s.physical_name ,<br />
@Size = convert (dec (15,2),s.size) * 8192 / 1048576,<br />
@UsedSpace =CAST(tspc.UsedExtents*convert(float,64) /1024 AS float) ,<br />
@DataUsedPercent =(CAST(tspc.UsedExtents*convert(float,64) /1024 AS float) / (convert (dec (15,2),s.size) * 8192 / 1048576) ) * 100<br />
FROM<br />
sys.filegroups AS g<br />
INNER JOIN sys.master_files AS s ON (s.type = 0 and s.database_id = db_id() and (s.drop_lsn IS NULL)) AND (s.data_space_id=g.data_space_id)<br />
INNER JOIN @tmpspc tspc ON tspc.Fileid = s.file_id<br />
WHERE<br />
(CAST(cast(g.name as varbinary(256)) AS sysname)=N&#8217;PRIMARY&#8217;)</p>
<p>IF (@DataUsedPercent &gt; 90)<br />
BEGIN<br />
DECLARE @Body varchar(max)<br />
SET @Body = db_name()+&#8217; Free Space is &lt; 10% in the following File: &#8216; + char(13) + @FileName +char(13)+<br />
+ &#8216;Size MB =&#8217;+convert(varchar(10),@Size) + &#8216; Used Space MB = &#8216;+convert(varchar(10),@UsedSpace)<br />
+ &#8216; %Used = &#8216;+ convert(varchar(10),@DataUsedPercent)</p>
<p>exec msdb..sp_send_dbmail @profile_name = @MailProfile<br />
, @recipients = @recipients<br />
, @subject = &#8216;Free Space Alert&#8217;<br />
, @body_format = &#8216;TEXT&#8217;<br />
, @body = @Body<br />
,@query_result_header = 0<br />
END</p>
<p>SET @FileName = &#8221;<br />
SET @Size = 0<br />
SET @UsedSpace =0</p>
<p>SELECT<br />
@FileName = s.physical_name ,<br />
@Size = convert (dec (15,2),s.size) * 8192 / 1048576,<br />
@UsedSpace =CAST(tspc.UsedExtents*convert(float,64) /1024 AS float) ,<br />
@IndexUsedPercent =(CAST(tspc.UsedExtents*convert(float,64) /1024 AS float) / (convert (dec (15,2),s.size) * 8192 / 1048576) ) * 100<br />
FROM<br />
sys.filegroups AS g<br />
INNER JOIN sys.master_files AS s ON (s.type = 0 and s.database_id = db_id() and (s.drop_lsn IS NULL<br />
)) AND (s.data_space_id=g.data_space_id)<br />
INNER JOIN @tmpspc tspc ON tspc.Fileid = s.file_id<br />
WHERE<br />
(CAST(cast(g.name as varbinary(256)) AS sysname)=N&#8217;INDEX&#8217;)<br />
IF (@IndexUsedPercent &gt; 90)<br />
BEGIN<br />
SET @Body = db_name()+&#8217; Free Space is &lt; 10% in the following File: &#8216; + char(13) + @FileName +char(13)+<br />
+ &#8216;Size MB =&#8217;+convert(varchar(10),@Size) + &#8216; Used Space MB = &#8216;+convert(varchar(10),@UsedSpace)<br />
+ &#8216; %Used = &#8216;+ convert(varchar(10),@indexUsedPercent)</p>
<p>exec msdb..sp_send_dbmail @profile_name = @MailProfile<br />
, @recipients = @recipients<br />
, @subject = &#8216;Free Space Alert&#8217;<br />
, @body_format = &#8216;TEXT&#8217;<br />
, @body = @Body<br />
,@query_result_header = 0<br />
END</p>
<p>END</p>
<p> </p>
<p>GO</p>
<pre>EXECUTE sp_ms_marksystemobject '[sp_CheckSpace]'</pre>
  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/motherofallgeeks.wordpress.com/66/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/motherofallgeeks.wordpress.com/66/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/motherofallgeeks.wordpress.com/66/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/motherofallgeeks.wordpress.com/66/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/motherofallgeeks.wordpress.com/66/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/motherofallgeeks.wordpress.com/66/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/motherofallgeeks.wordpress.com/66/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/motherofallgeeks.wordpress.com/66/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/motherofallgeeks.wordpress.com/66/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/motherofallgeeks.wordpress.com/66/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=motherofallgeeks.com&blog=5421765&post=66&subd=motherofallgeeks&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://motherofallgeeks.com/2009/03/07/simple-space-checker/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/67278767077faf724f5541cda8b66dfc?s=96&#38;d=identicon" medium="image">
			<media:title type="html">motherofallgeeks</media:title>
		</media:content>
	</item>
		<item>
		<title>Index This Index That</title>
		<link>http://motherofallgeeks.com/2008/12/30/index-this-index-that/</link>
		<comments>http://motherofallgeeks.com/2008/12/30/index-this-index-that/#comments</comments>
		<pubDate>Mon, 29 Dec 2008 18:24:15 +0000</pubDate>
		<dc:creator>motherofallgeeks</dc:creator>
				<category><![CDATA[Indexes]]></category>
		<category><![CDATA[Maintenance]]></category>
		<category><![CDATA[Performance]]></category>
		<category><![CDATA[index]]></category>
		<category><![CDATA[storage engine]]></category>

		<guid isPermaLink="false">http://motherofallgeeks.wordpress.com/?p=52</guid>
		<description><![CDATA[So let me ask you this&#8230; Do you really know how SQL Server storage engine works? You would be surprised how many people can&#8217;t answer simple questions about indexes on interviews. Sorry Folks if you can&#8217;t tell me what the difference between and clustered and a non-clustered index your interview score just went down.
That being [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=motherofallgeeks.com&blog=5421765&post=52&subd=motherofallgeeks&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>So let me ask you this&#8230; Do you really know how SQL Server storage engine works? You would be surprised how many people can&#8217;t answer simple questions about indexes on interviews. Sorry Folks if you can&#8217;t tell me what the difference between and clustered and a non-clustered index your interview score just went down.</p>
<p>That being said this is a very simple question I like to ask people on interviews. If your going to rebuild the indexes on a large table which has no Forgein keys. How would you do it and why? You cannot use any  dbcc or online index operations. We are going old school. To keep it simple assume your table is 5GB, has a clustered index and 5 Non-clustered indexes some of which are composite.</p>
<p>When I ask this my goal is to find out if you truly understand how the data is stored. If you do then the answer is quite easy.</p>
<p>I&#8217;ll give you the answer I am looking for first then I will explain why.</p>
<p>&#8220;You would drop the NON-Clustered indexes first. Then drop the Clustered index. Next you would create the CLustered index then the Non-CLustered indexes.&#8221;</p>
<p>Now the why.</p>
<p>A clustered index in the simplest explaination is that it is your data. A classic example of a clustered index is a phone book. Now of course we can get into B-Tree structures but lets save that for another post. That being said If I drop my clustered index first the operation will also drop and recreate my non-clustered indexes and can potentially take alot of time. To speed up the process drop the NC indexes first. (AFTER YOU SCRIPTED them of course)</p>
  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/motherofallgeeks.wordpress.com/52/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/motherofallgeeks.wordpress.com/52/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/motherofallgeeks.wordpress.com/52/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/motherofallgeeks.wordpress.com/52/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/motherofallgeeks.wordpress.com/52/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/motherofallgeeks.wordpress.com/52/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/motherofallgeeks.wordpress.com/52/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/motherofallgeeks.wordpress.com/52/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/motherofallgeeks.wordpress.com/52/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/motherofallgeeks.wordpress.com/52/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=motherofallgeeks.com&blog=5421765&post=52&subd=motherofallgeeks&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://motherofallgeeks.com/2008/12/30/index-this-index-that/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/67278767077faf724f5541cda8b66dfc?s=96&#38;d=identicon" medium="image">
			<media:title type="html">motherofallgeeks</media:title>
		</media:content>
	</item>
		<item>
		<title>I like to Move it Move it</title>
		<link>http://motherofallgeeks.com/2008/12/06/i-like-to-move-it-move-it/</link>
		<comments>http://motherofallgeeks.com/2008/12/06/i-like-to-move-it-move-it/#comments</comments>
		<pubDate>Sat, 06 Dec 2008 03:04:35 +0000</pubDate>
		<dc:creator>motherofallgeeks</dc:creator>
				<category><![CDATA[Maintenance]]></category>
		<category><![CDATA[master]]></category>
		<category><![CDATA[Move Database]]></category>
		<category><![CDATA[Moving Master]]></category>
		<category><![CDATA[Moving System Databases]]></category>
		<category><![CDATA[tempdb]]></category>

		<guid isPermaLink="false">http://motherofallgeeks.com/?p=55</guid>
		<description><![CDATA[I&#8217;m sure of all of us have had to move our databases around from time to time. I just came into a situation where we are doing a POC test for DR mirroring software called Double Take. I had to uninstall and reinstall my SQL destination (or failover machine) because the first install was not [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=motherofallgeeks.com&blog=5421765&post=55&subd=motherofallgeeks&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>I&#8217;m sure of all of us have had to move our databases around from time to time. I just came into a situation where we are doing a POC test for DR mirroring software called Double Take. I had to uninstall and reinstall my SQL destination (or failover machine) because the first install was not done properly.  For example the System databases needed to mimic the source server. So I started along my install and I specified the path for the databases.</p>
<p>After the install was done I checked and I forgot that the install will create an MSSQL directory on top of whatever path you specify in the install. UGH!!!</p>
<p>For example I wanted my system and user databases to go Z:\SQLServer\MSSQL5\DATA  and that is what I told the install so the install put the databased here Z:\SQLServer\MSSQL5\DATA \MSSQL1\DATA. What an annoyance!!!!</p>
<p>Rather then redoing the install I thought I will just move the system databases. Well I haven&#8217;t done this task in SQL 2005 yet and I thought ok I will go and just modify the Startup parameters as I would in SQL 2000. Not the CASE.</p>
<p>Here is what I need to do.</p>
<p>For Non Master databases</p>
<p>ALTER DATABASE model MODIFY FILE ( NAME = modeldev, FILENAME = Z:\SQLServer\MSSQL5\DATA \model.mdf&#8217;)<br />
ALTER DATABASE model MODIFY FILE ( NAME = modellog, FILENAME = Z:\SQLServer\MSSQL5\DATA \ modellog.ldf&#8217;)</p>
<p>Repeat for tempdb and msdb if needed<br />
Stop SQL Server</p>
<p>Move the files</p>
<p>Start SQL Server and the databases will be in the new location and you can delete the old files.</p>
<p>Moving Master is a little trickier</p>
<p>Perform the same ALTER database statement executed above for master</p>
<p>ALTER DATABASE master MODIFY FILE ( NAME = master, FILENAME = x:\newlocation\master.mdf&#8217;)<br />
ALTER DATABASE master MODIFY FILE ( NAME = mastlog, FILENAME = x:\newlocation\mastlog.ldf&#8217;)</p>
<p>Next you need to change the startup parameter in the REGISTRY.  (You can search the registery for ERRORLOG or master.mdf to find the proper key for your server) Edit the values for the new location</p>
<p>Stop the SQL Server Service</p>
<p>Move the Files</p>
<p>Start the SQL Server Service in minimal config mode -f and trace flag -T3608 (prevents auto recovery)</p>
<p>From the command prompt run the following. You need to be in the location of the sqlservr.exe.</p>
<p>net start MSSQLServer /f/T3608</p>
<p>net start MSSQL$InstanceA /f/T3608</p>
<p>Connect VIA Query Analyzer and Alter mssqlsystemresource<br />
ALTER DATABASE mssqlsystemresource MODIFY FILE ( NAME = data, FILENAME = x:\newlocation\mssqlsystemresource.mdf&#8217;)<br />
ALTER DATABASE mssqlsystemresource MODIFY FILE ( NAME = log, FILENAME = x:\newlocation\mssqlsystemresource.ldf&#8217;)</p>
<p>Move the mssqlsystemresource files.<br />
Set mssqlsystemresource database to read only.  <span lang="EN">ALTER DATABASE mssqlsystemresource SET READ_ONLY</span><br />
Stop and Start the SQL Server Service in normal mode.</p>
  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/motherofallgeeks.wordpress.com/55/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/motherofallgeeks.wordpress.com/55/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/motherofallgeeks.wordpress.com/55/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/motherofallgeeks.wordpress.com/55/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/motherofallgeeks.wordpress.com/55/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/motherofallgeeks.wordpress.com/55/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/motherofallgeeks.wordpress.com/55/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/motherofallgeeks.wordpress.com/55/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/motherofallgeeks.wordpress.com/55/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/motherofallgeeks.wordpress.com/55/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=motherofallgeeks.com&blog=5421765&post=55&subd=motherofallgeeks&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://motherofallgeeks.com/2008/12/06/i-like-to-move-it-move-it/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/67278767077faf724f5541cda8b66dfc?s=96&#38;d=identicon" medium="image">
			<media:title type="html">motherofallgeeks</media:title>
		</media:content>
	</item>
		<item>
		<title>Poor performance from Application</title>
		<link>http://motherofallgeeks.com/2008/12/01/poor-performance-from-application/</link>
		<comments>http://motherofallgeeks.com/2008/12/01/poor-performance-from-application/#comments</comments>
		<pubDate>Mon, 01 Dec 2008 02:47:26 +0000</pubDate>
		<dc:creator>motherofallgeeks</dc:creator>
				<category><![CDATA[Performance]]></category>

		<guid isPermaLink="false">http://motherofallgeeks.wordpress.com/?p=42</guid>
		<description><![CDATA[Have you ever come across the senario where a query or stored procedure performs just fine from Query Analyzer but performs terrible from the application? Most of us have. How do you go about troubling shooting this? Here is the senario I ran into and the steps taken.
During the stress testing phase of our application release we [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=motherofallgeeks.com&blog=5421765&post=42&subd=motherofallgeeks&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>Have you ever come across the senario where a query or stored procedure performs just fine from Query Analyzer but performs terrible from the application? Most of us have. How do you go about troubling shooting this? Here is the senario I ran into and the steps taken.</p>
<p>During the stress testing phase of our application release we came to a situation when loadrunner hit two particular tabs in our application and it would report back timeout errors. The web developers were called in as well as the DBA team.  So obviously our first step was to run a trace and find out the exact call to the stored procedure which is taking time. (I assume you already know how to use SQL Profiler if not check it out in books online) . The events I wanted to see for this run of the trace are:</p>
<p>SP:Starting<br />
SP:Completed<br />
SP:StmtStarting<br />
SP:StmtCompleted</p>
<p>The reason I chose these are because my application is stored procedure driven so I want to know which proc and which statement in that proc is taking time. I need to look at the Duration column, Reads and writes as well. Remeber to pay attention to column/event compatibility in your trace. For example if I want to see the duration value populated in my trace. I need to make sure that I am capturing the XXX:completed event. That being said, I reached my goal of getting the statement and the call to the stored procedure which is taking time. I noticed in my trace that the reads were through the roof. (Over three million)</p>
<p>So my next step obviously was to run the call to this stored procedure. This particular stored proc is a read proc. I ran it and of course it came back in 1 second while in the trace it reported 32 seconds. Hmmmmm, intersting.</p>
<p>Now lets examine the procedure and see whats going on and what the tables look like. The stored procedure is pretty straight forward. It&#8217;s a child proc and its parents have most of the logic in it. It&#8217;s checking if data exists in a particular table which has 42 million rows and if it does it returns a flag which is processed by the outer proc. So now my first thought is to check when statistics have last been updated on this table and if there is any index fragmentation.</p>
<p>I ran showcontig (this is a SQL 2000 app) and saw my scan density to be about 30 percent.  So next I planned and rebuilt the indexes on this table. I started to think maybe the fragmentation was negatively affecting the query plan when it was coming from the app. On examination of the query plan when I ran the proc from Query Analyzer I didn&#8217;t see anything negative.</p>
<p>After the index maintenance the test was run again and again the same thing happened but now the issue was sparatic. This makes it even more difficult now because the same call still takes one second for me. So next I started to think that maybe there was some excessive locking or blocking happening on this table. So I had my testor run the test again for a single user to reduce contention. Even when running the test as a single user the issue appeared.  I used the Deadlock even in profiler and that was not it either.</p>
<p>I started to go back to the idea of the query plan. Maybe when the call is coming from the application a different query plan is being used but how do I verify this. SQL Profiler has an event class for the query plan so that was my next step.  I captured the plan during the load test and compared it with the results from my machine. Sure enough when run through the application the query plan being used was scanning the table. You can imaging scanning 42 million rows will take some time but why when I run it from my machine does it do an index seek. I started to think my procedure cache was somehow corrupted so you guessed it. My next step was to clear the procedure cache.</p>
<p>Same results again. Ok I am ready to pull my hair out now. Why is this happening. Back to google and I found a topic called Parameter sniffing. This is something new to me but it was the exact reason for my problem. In a nut shell parameter sniffing is a side effect of parameter use. When you use parameters in your stored procedures SQL Server will generate plans based on common parameter use.  (It&#8217;s a bit more complicated this this. check out his link <a href="http://blogs.msdn.com/queryoptteam/archive/2006/03/31/565991.aspx">http://blogs.msdn.com/queryoptteam/archive/2006/03/31/565991.aspx</a>)  If a atypical call to the proc is made and then a typical call. The typical call will have a plan which is not optimal for that situation.</p>
<p>Now that I know what is happening how do I avoid this? We need to fool the optimizer. Here is some simple code</p>
<p>Create procedure proc_myparasniffer</p>
<p>AS @office varchar(255), @offfice_template varchar(255) =&#8221;Main Street&#8221;</p>
<p>SET @office_template=@office</p>
<p>SELECT A,B,C from Order where OfficeName = @office_template</p>
<p>It&#8217;s pretty easy we are filtering the query on office_Template because @office_template is set with a default value when SQL sniffs the parameters it will always generate the same plan.</p>
<p class="MsoNormal" style="text-align:justify;margin:0;"> </p>
  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/motherofallgeeks.wordpress.com/42/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/motherofallgeeks.wordpress.com/42/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/motherofallgeeks.wordpress.com/42/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/motherofallgeeks.wordpress.com/42/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/motherofallgeeks.wordpress.com/42/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/motherofallgeeks.wordpress.com/42/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/motherofallgeeks.wordpress.com/42/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/motherofallgeeks.wordpress.com/42/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/motherofallgeeks.wordpress.com/42/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/motherofallgeeks.wordpress.com/42/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=motherofallgeeks.com&blog=5421765&post=42&subd=motherofallgeeks&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://motherofallgeeks.com/2008/12/01/poor-performance-from-application/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/67278767077faf724f5541cda8b66dfc?s=96&#38;d=identicon" medium="image">
			<media:title type="html">motherofallgeeks</media:title>
		</media:content>
	</item>
		<item>
		<title>Welcome</title>
		<link>http://motherofallgeeks.com/2008/11/29/welcome/</link>
		<comments>http://motherofallgeeks.com/2008/11/29/welcome/#comments</comments>
		<pubDate>Sat, 29 Nov 2008 02:04:37 +0000</pubDate>
		<dc:creator>motherofallgeeks</dc:creator>
				<category><![CDATA[Welcome]]></category>

		<guid isPermaLink="false">http://motherofallgeeks.wordpress.com/?p=37</guid>
		<description><![CDATA[Welcome to Mother of All Geeks! What you will find on this site are articles related to Microsoft SQL Server topics. My goal is to give back to the technology communitity some of what I have learned over the years. I also hope to learn some from my readers as well.
     [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=motherofallgeeks.com&blog=5421765&post=37&subd=motherofallgeeks&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>Welcome to Mother of All Geeks! What you will find on this site are articles related to Microsoft SQL Server topics. My goal is to give back to the technology communitity some of what I have learned over the years. I also hope to learn some from my readers as well.</p>
  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/motherofallgeeks.wordpress.com/37/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/motherofallgeeks.wordpress.com/37/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/motherofallgeeks.wordpress.com/37/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/motherofallgeeks.wordpress.com/37/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/motherofallgeeks.wordpress.com/37/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/motherofallgeeks.wordpress.com/37/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/motherofallgeeks.wordpress.com/37/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/motherofallgeeks.wordpress.com/37/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/motherofallgeeks.wordpress.com/37/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/motherofallgeeks.wordpress.com/37/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=motherofallgeeks.com&blog=5421765&post=37&subd=motherofallgeeks&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://motherofallgeeks.com/2008/11/29/welcome/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/67278767077faf724f5541cda8b66dfc?s=96&#38;d=identicon" medium="image">
			<media:title type="html">motherofallgeeks</media:title>
		</media:content>
	</item>
	</channel>
</rss>