<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://sqlservernation.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Tip of the Week : SQL Server, poor performance</title><link>http://sqlservernation.com/blogs/tipweek/archive/tags/SQL+Server/poor+performance/default.aspx</link><description>Tags: SQL Server, poor performance</description><dc:language>en</dc:language><generator>CommunityServer 2008.5 SP2 (Build: 40407.4157)</generator><item><title>Why is this query slow: 1</title><link>http://sqlservernation.com/blogs/tipweek/archive/2009/10/24/why-is-this-query-slow-1.aspx</link><pubDate>Sat, 24 Oct 2009 13:41:00 GMT</pubDate><guid isPermaLink="false">a3dbd76b-19b9-441f-b40a-d79e8f433d35:309</guid><dc:creator>TimChapman</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://sqlservernation.com/blogs/tipweek/rsscomments.aspx?PostID=309</wfw:commentRss><comments>http://sqlservernation.com/blogs/tipweek/archive/2009/10/24/why-is-this-query-slow-1.aspx#comments</comments><description>&lt;p&gt;Since this is the first part of this series, I&amp;#39;ll give a little background as to what I want to accomplish.&amp;nbsp; At some point, you&amp;#39;re going to run into a situation where a query on your SQL Server system is &amp;quot;slow&amp;quot;.&amp;nbsp; Slowness is usually ambiguous, and almost always relates to a persons reference.&amp;nbsp; However, when we hear that something is slow or broken we need to check it out and see what may be the cause.&amp;nbsp; So, my goal is to introduce simple to increasingly complex queries and show you the steps I like to take to diagnose what may be causing them to be &amp;quot;slow&amp;quot;.&lt;/p&gt;
&lt;p&gt;Statement 1:&lt;/p&gt;
&lt;p&gt;SELECT *&lt;br /&gt;FROM TableA a&lt;br /&gt;JOIN TableB b ON a.PrimaryKeyField = b.ForeignKeyField&lt;br /&gt;WHERE b.LookupValue = 3&lt;/p&gt;
&lt;p&gt;The first thing I like to look for are the simple things.&amp;nbsp; 9 times out of 10 something simple is being missed and causing poor performance.&amp;nbsp; So, in the query above I first want to look for any columns that may need to be indexed.&amp;nbsp; My TableA table has a clustered index (default) on the Primary Key field (named PrimaryKeyField), so single record lookups should perform seek operations.&amp;nbsp; Next I want to look at the ForeignKeyField in TableB.&amp;nbsp; Since I am joining on this field back to the PrimaryKeyField in TableA I want to make sure this field has a nonclustered index on it to facilitate faster lookup operations.&amp;nbsp; If there is indeed a foreign key constraint defined between these two tables based on this relationship, the nonclustered index on the ForeignKeyField in TableB becomes even more important.&amp;nbsp; SQL Server has to perform a lookup check to make sure the constraint is satistified anytime a record is inserted (or the ForeignKeyField is updated) in the table, so if SQL Server can use an index to perform the seek it is all the better.&amp;nbsp; &lt;/p&gt;
&lt;p&gt;Next I want to check the criteria in the WHERE statement.&amp;nbsp; &lt;strong&gt;Note&lt;/strong&gt;:&amp;nbsp; The criteria in the JOIN operations can be thought of as &amp;quot;criteria&amp;quot;, so anytime you&amp;#39;re considering criteria you should also be considering indexes to quickly satisfy the criteria.&amp;nbsp; Because I am doing a lookup on the LookupValue field, I&amp;#39;ll want to see if an index on this field makes sense.&amp;nbsp; Some indexes make sense, some do not....more on that later.&amp;nbsp; &lt;/p&gt;
&lt;p&gt;I also want to remove the SELECT * portion of the query and replace it with a specified field list.&amp;nbsp; Bringing back all of the fields involved almost always results in a slower query, and can end up breaking your data layer or interface code if you&amp;#39;re not careful.&lt;/p&gt;
&lt;p&gt;Lastly, I want to make sure my table references are prefixed with the schema that the table belongs to.&amp;nbsp; I like to do this so that SQL Server doesn&amp;#39;t have to do the determination for me.&lt;/p&gt;
&lt;p&gt;I plan on posting more of these in the future.&amp;nbsp; If you like them and/or have any suggestions that you would like for me to go over, please let me know!&lt;/p&gt;
&lt;p&gt;Tim&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://sqlservernation.com/aggbug.aspx?PostID=309" width="1" height="1"&gt;</description><category domain="http://sqlservernation.com/blogs/tipweek/archive/tags/JOIN/default.aspx">JOIN</category><category domain="http://sqlservernation.com/blogs/tipweek/archive/tags/SQL+Server/default.aspx">SQL Server</category><category domain="http://sqlservernation.com/blogs/tipweek/archive/tags/performance/default.aspx">performance</category><category domain="http://sqlservernation.com/blogs/tipweek/archive/tags/default/default.aspx">default</category><category domain="http://sqlservernation.com/blogs/tipweek/archive/tags/index+tuning/default.aspx">index tuning</category><category domain="http://sqlservernation.com/blogs/tipweek/archive/tags/nonclustered+index/default.aspx">nonclustered index</category><category domain="http://sqlservernation.com/blogs/tipweek/archive/tags/primary+key/default.aspx">primary key</category><category domain="http://sqlservernation.com/blogs/tipweek/archive/tags/WHERE/default.aspx">WHERE</category><category domain="http://sqlservernation.com/blogs/tipweek/archive/tags/clustered+index/default.aspx">clustered index</category><category domain="http://sqlservernation.com/blogs/tipweek/archive/tags/foreign+key/default.aspx">foreign key</category><category domain="http://sqlservernation.com/blogs/tipweek/archive/tags/poor+performance/default.aspx">poor performance</category><category domain="http://sqlservernation.com/blogs/tipweek/archive/tags/criteria/default.aspx">criteria</category><category domain="http://sqlservernation.com/blogs/tipweek/archive/tags/fast+query/default.aspx">fast query</category></item></channel></rss>