﻿<?xml version='1.0' encoding='UTF-8'?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>SQLServerCentral / SQL Server 2005 / SQL Server 2005 Performance Tuning  / Query Slow Performance / Latest Posts</title><generator>InstantForum.NET v2.9.0</generator><description>SQLServerCentral</description><link>http://www.sqlservercentral.com/Forums/</link><webMaster>notifications@sqlservercentral.com</webMaster><lastBuildDate>Sat, 25 May 2013 04:52:06 GMT</lastBuildDate><ttl>20</ttl><item><title>RE: Query Slow Performance</title><link>http://www.sqlservercentral.com/Forums/Topic814074-360-1.aspx</link><description>[quote][b]sindbad7000 (11/10/2009)[/b][hr]Thanks alot it works... now it retrieves the 5200 records in only one second...thanks alot again i appreciate that alotbut please may i ask about the tools you used??[/quote]I looked at the actual query plan you kindly uploaded.  The stand-out features were the clustered index scan of the TransactionDetail table and the table scan of the temporary table.  Full scans on the inner side of a loop join are rarely good news - especially when the outer input has many rows, and so does the table being scanned.  If you hadn't been able to create the new indexes, I would have suggested forcing a HASH or MERGE join for those parts of the plan.  Not as optimal as the index by any means, but at least TransactionDetail would only have been scanned once.I wrote the index definition by inspection: clearly the lookup was on the ID (which I mistakenly assumed was UNIQUE).  Adding the included columns wasn't required, by they were listed as output columns in the plan so I thought I'd add them for neatness, and to avoid a key lookup.I could have saved myself 2 minutes' work by looking at the 'missing index' information in the plan - but the way forward was pretty clear anyway.As [i]TheSQLGuru[/i] mentioned, it's most just a question of experience :-)Paul</description><pubDate>Tue, 10 Nov 2009 14:27:14 GMT</pubDate><dc:creator>Paul White</dc:creator></item><item><title>RE: Query Slow Performance</title><link>http://www.sqlservercentral.com/Forums/Topic814074-360-1.aspx</link><description>In terms of the tools that can be used to identify this missing index yourself, have a look at the execution plan that you posted to the forum.At the top you'll see a section of text starting with 'Missing Index' - if you right-click on the text and select 'Missing Index Details...' the index creation script will open in a new &amp;#119;indow.The suggestions that are offered are not always optimal, but they are often a good starting point.Other than reviewing and selectively implementing the suggestions that SQL Server offers, it's usually a case of learning to interpret execution plans and then how to optimise queries and your database design to help ensure that SQL Server chooses an optimal plan. This ebook is a good starting point if you're interested in learning more:http://www.red-gate.com/specials/Grant.htm?utm_source=simpletalk?utm_medium=email?utm_content=Grant080527?utm_campaign=sqltoolbeltChris</description><pubDate>Tue, 10 Nov 2009 08:11:06 GMT</pubDate><dc:creator>Chris Howarth-536003</dc:creator></item><item><title>RE: Query Slow Performance</title><link>http://www.sqlservercentral.com/Forums/Topic814074-360-1.aspx</link><description>[quote]but please may i ask about the tools you used?? [/quote]   In this particular case I think Chris mentioned that the query plan xml actually contains missing index information.  But in general performance tuning is a HUGE topic that takes lots of study and lots of experience to become good at.  Find some books, blogs, articles, classes and start learning.  Hire a tuning consultant to mentor you while reviewing your system for you.  </description><pubDate>Tue, 10 Nov 2009 08:02:21 GMT</pubDate><dc:creator>TheSQLGuru</dc:creator></item><item><title>RE: Query Slow Performance</title><link>http://www.sqlservercentral.com/Forums/Topic814074-360-1.aspx</link><description>Paul's SuggestionCREATE NONCLUSTERED INDEX [UQ dbo.TransactionDetail TransactionId (AccountId, Direction, Amount)]ON [dbo].[TransactionDetail] (TransactionId ASC)INCLUDE (AccountId, Direction, Amount)WITH (SORT_IN_TEMPDB = ON, MAXDOP = 1, ONLINE = OFF);but i removed the UNIQUE from the index because this field 'TransactionId' is not UNIQUE</description><pubDate>Tue, 10 Nov 2009 07:34:03 GMT</pubDate><dc:creator>sindbad7000</dc:creator></item><item><title>RE: Query Slow Performance</title><link>http://www.sqlservercentral.com/Forums/Topic814074-360-1.aspx</link><description>[quote][b]sindbad7000 (11/10/2009)[/b][hr]Thanks alot it works... now it retrieves the 5200 records in only one second...thanks alot again i appreciate that alotbut please may i ask about the tools you used??[/quote]Which suggestion did you choose to implement?Chris</description><pubDate>Tue, 10 Nov 2009 07:28:21 GMT</pubDate><dc:creator>Chris Howarth-536003</dc:creator></item><item><title>RE: Query Slow Performance</title><link>http://www.sqlservercentral.com/Forums/Topic814074-360-1.aspx</link><description>Thanks alot it works... now it retrieves the 5200 records in only one second...thanks alot again i appreciate that alotbut please may i ask about the tools you used??</description><pubDate>Tue, 10 Nov 2009 06:47:36 GMT</pubDate><dc:creator>sindbad7000</dc:creator></item><item><title>RE: Query Slow Performance</title><link>http://www.sqlservercentral.com/Forums/Topic814074-360-1.aspx</link><description>Paul's suggestion is good, and is consistent with the missing index that was suggested by SQL Server in the execution plan that you provided.If you almost always return rows from dbo.TransactionDetail by looking up the TransactionID column then you may wish to consider changing PK_TransactionDetail to be NONCLUSTERED, then creating a CLUSTERED index on the TransactionID column. This change would help you minimise additional disk space requirements and would also likely improve performance of other queries (those based on looking-up based on the TransactionID column). This would be done instead of creating the 'missing index' suggested previously.Chris</description><pubDate>Tue, 10 Nov 2009 05:58:18 GMT</pubDate><dc:creator>Chris Howarth-536003</dc:creator></item><item><title>RE: Query Slow Performance</title><link>http://www.sqlservercentral.com/Forums/Topic814074-360-1.aspx</link><description>A full optimization will take a few minutes, but the main problem seems to be a missing index, something like:[code]CREATE UNIQUE NONCLUSTERED INDEX [UQ dbo.TransactionDetail TransactionId (AccountId, Direction, Amount)]ON [dbo].[TransactionDetail] (TransactionId ASC)INCLUDE (AccountId, Direction, Amount)WITH (SORT_IN_TEMPDB = ON, MAXDOP = 1, ONLINE = OFF);[/code]and change the temporary table creation to:[code]CREATE TABLE #Accounts (AccountNumber nvarchar(20) PRIMARY KEY);[/code]</description><pubDate>Tue, 10 Nov 2009 02:45:11 GMT</pubDate><dc:creator>Paul White</dc:creator></item><item><title>RE: Query Slow Performance</title><link>http://www.sqlservercentral.com/Forums/Topic814074-360-1.aspx</link><description>When i removed the Order By clause the time decreased from "1 minute and 48 seconds"  to "2 seconds only".....but i really need this Order By Clause</description><pubDate>Tue, 10 Nov 2009 02:37:08 GMT</pubDate><dc:creator>sindbad7000</dc:creator></item><item><title>RE: Query Slow Performance</title><link>http://www.sqlservercentral.com/Forums/Topic814074-360-1.aspx</link><description>attached the updated plan and a txt file that Contains : the 4 tables creation, the stored proc. and the function i used in the stored proc.</description><pubDate>Tue, 10 Nov 2009 02:22:16 GMT</pubDate><dc:creator>sindbad7000</dc:creator></item><item><title>RE: Query Slow Performance</title><link>http://www.sqlservercentral.com/Forums/Topic814074-360-1.aspx</link><description>Yes, the posted execution plan just shows the plan for:DECLARE @ISLEAF BIT SELECT @IsLeaf = IsLeafAccount FROM Account WHERE AccountNum = @AccountNum;...which isn't really the performance-critical bit :-PPlease do take the time to post the plan for the final SELECT - the cause of the slowness is almost certain to be obvious from it, thanks.By the way...SET NOCOUNT [b]OFF[/b]?I think you would benefit from reading Erland Sommarskog's work on the subject: [url]http://www.sommarskog.se/dyn-search-2005.html[/url].Paul</description><pubDate>Tue, 10 Nov 2009 02:15:42 GMT</pubDate><dc:creator>Paul White</dc:creator></item><item><title>RE: Query Slow Performance</title><link>http://www.sqlservercentral.com/Forums/Topic814074-360-1.aspx</link><description>Have a look at this link  [url]http://sqlinthewild.co.za/index.php/2009/03/19/catch-all-queries/[/url].Also please post the DDL (indexes as well), when you say 700 accounts records is that in the #accounts table ?Your query plan is not complete either.</description><pubDate>Mon, 09 Nov 2009 11:36:08 GMT</pubDate><dc:creator>Dave Ballantyne</dc:creator></item><item><title>RE: Query Slow Performance</title><link>http://www.sqlservercentral.com/Forums/Topic814074-360-1.aspx</link><description>Here Is My SQL Query : ALTER PROCEDURE [dbo].[usp_rpt_AccountStatement] 	@dateFrom smalldatetime = null, 	@dateTo smalldatetime = null,	@AccountNum nvarchar(20) = null,	@ComponentNumber nvarchar(20) = NULLASBEGIN	SET NOCOUNT OFF;	CREATE TABLE #Accounts (AccountNumber nvarchar(20))		DECLARE @ISLEAF BIT 	SELECT @IsLeaf = IsLeafAccount FROM Account WHERE AccountNum = @AccountNum	IF @IsLeaf = 0		BEGIN INSERT INTO #Accounts(AccountNumber) SELECT AccountNum FROM dbo.fnGetAccountChildren(@AccountNum) END	ELSE 		BEGIN INSERT INTO #Accounts(AccountNumber) VALUES (@AccountNum) END 	SELECT    TransactionDetail.Direction * TransactionDetail.Amount AS Debit,                       ABS((TransactionDetail.Direction - 1) * TransactionDetail.Amount) AS Credit, [Transaction].TransactionDate, [Transaction].Description_En, [Transaction].Description_Ar,					Component.ComponentId,Component.ComponentName_En,Component.ComponentName_Ar							FROM         [Transaction] INNER JOIN TransactionDetail ON [Transaction].TransactionId = TransactionDetail.TransactionId 							INNER JOIN Account ON TransactionDetail.AccountId = Account.AccountId							INNER JOIN [Component] ON ([Transaction].[ComponentId] = [Component].[ComponentNumber]) 	WHERE 		((@datefrom IS NULL) OR (@dateTo IS NOT NULL) OR ([Transaction].TransactionDate &amp;gt;= @datefrom) ) 	AND ((@dateto IS NULL) OR(@dateFrom IS NOT NULL) OR ([Transaction].TransactionDate &amp;lt;= @dateto) )	AND ((@dateFrom IS NULL) OR (@dateTo IS NULL) OR ([Transaction].TransactionDate BETWEEN @datefrom AND @dateto))		AND Account.AccountNum IN (SELECT AccountNumber COLLATE database_default FROM #Accounts)	AND ((@ComponentNumber IS NULL) OR ([Transaction].[ComponentId] = @componentNumber))	ORDER BY [Transaction].[TransactionDate]ENDi ran it now it Retrieved 5200 rows in 1.48 minuteThe Transaction Table has 17720 records, The TransactionDetail table has 36346 records, The Account table has 718 records and finally the Component table has 110 records.</description><pubDate>Mon, 09 Nov 2009 10:09:17 GMT</pubDate><dc:creator>sindbad7000</dc:creator></item><item><title>RE: Query Slow Performance</title><link>http://www.sqlservercentral.com/Forums/Topic814074-360-1.aspx</link><description>That kind of query should return much quicker.  As the others said though, post more info and we can help more effiiciently.</description><pubDate>Thu, 05 Nov 2009 22:22:54 GMT</pubDate><dc:creator>SQLRNNR</dc:creator></item><item><title>RE: Query Slow Performance</title><link>http://www.sqlservercentral.com/Forums/Topic814074-360-1.aspx</link><description>[quote][b]sindbad7000 (11/5/2009)[/b][hr]please i am using sql server 2005, i wrote a simple query that joins only 4 tables. 2 of them have thousands of records. the query returns 4500 records in 1 minute and 30 seconds...is that acceptable??[/quote]Nope... not acceptable.  But can't help because there's not enough info.  Please see the second link in my signature line below to get better help.</description><pubDate>Thu, 05 Nov 2009 22:05:11 GMT</pubDate><dc:creator>Jeff Moden</dc:creator></item><item><title>RE: Query Slow Performance</title><link>http://www.sqlservercentral.com/Forums/Topic814074-360-1.aspx</link><description>post the query and the query plan, and we can take a look</description><pubDate>Thu, 05 Nov 2009 02:30:02 GMT</pubDate><dc:creator>Silverfox</dc:creator></item><item><title>RE: Query Slow Performance</title><link>http://www.sqlservercentral.com/Forums/Topic814074-360-1.aspx</link><description>How does the query plan look ?Is it using indexes ?</description><pubDate>Thu, 05 Nov 2009 02:29:17 GMT</pubDate><dc:creator>Dave Ballantyne</dc:creator></item><item><title>Query Slow Performance</title><link>http://www.sqlservercentral.com/Forums/Topic814074-360-1.aspx</link><description>please i am using sql server 2005, i wrote a simple query that joins only 4 tables. 2 of them have thousands of records. the query returns 4500 records in 1 minute and 30 seconds...is that acceptable??</description><pubDate>Thu, 05 Nov 2009 02:13:42 GMT</pubDate><dc:creator>sindbad7000</dc:creator></item></channel></rss>