Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Performance Headache

Author  Topic 

Sachin.Nand

2937 Posts

Posted - 2009-10-06 : 03:30:24
Hi

I am posting a part of my stored procedure that is causin a real performance headache.Here is the query.

declare @Siteid as VARCHAR(MAX)
declare @Processid VARCHAR(MAX)
declare @StartDate DATETIME
declare @EndDate DATETIME

set @Siteid='0,60,61,62,63,53,64,65,66,67,78,68,69,70,54,71,72,47,73,74,75,89,57,76,58,77,79,80,81,86,82,83,46,84,85,56,88,90,87,59,94,91,92,93'
set @Processid='4'
set @StartDate='01-oct-2008'
set @EndDate='01-oct-2009'

DECLARE @Sql VARCHAR(MAX), @Str VARCHAR(MAX),@Sqlmax VARCHAR(MAX)
DECLARE @x XML
SET @x='<i>'+REPLACE(@SiteId,',','</i><i>')+'</i>'
DECLARE @tblsites AS TABLE(siteid INT)
INSERT INTO @tblsites
SELECT x.i.value('.','varchar(50)')AS SiteID FROM @x.nodes('//i')x(i)

SET @x=''
SET @x='<i>'+REPLACE(@ProcessId,',','</i><i>')+'</i>'
DECLARE @tblprocess AS TABLE(processid INT)
INSERT INTO @tblprocess
SELECT x.i.value('.','varchar(50)')AS ProcessId FROM @x.nodes('//i')x(i)

SELECT


IRM.ReadingDateTime,
Instruments.InstrumentID,
Instruments.InstrumentName,
Instruments.InstrumentOrder,
Units.DisplayUnit AS 'UnitName',
DisplayFormula,
SUM(CASE WHEN Reading>LowThreshold AND (TYPE=3 OR TYPE=33 OR TYPE=333) THEN 1 ELSE 0 END)OVER(PARTITION BY lines.LineID,DATEPART(yy,ReadingDateTime))AS InstCnt,
Sites.SiteName,
Lines.LineID,
Lines.LineName,
CAST(IRD.Reading AS DECIMAL(18,4)) AS Reading,[TYPE],LowThreshold
INTO #TempReadings
FROM InstrumentReadingDetails IRD
INNER JOIN InstrumentReadingMaster IRM
ON IRM.InstrumentReadingMasterID=IRD.InstrumentReadingMasterID
INNER JOIN Instruments
ON Instruments.InstrumentID=IRD.InstrumentID
INNER JOIN Lines
ON Lines.LineID =Instruments.LineID
INNER JOIN POS
ON POS.POSID=LINEs.POSID
INNER JOIN Sites
ON POS.SiteID = Sites.SiteID
INNER JOIN Units
ON Units.UnitID=Instruments.UnitId
INNER JOIN Process
ON Lines.ProcessId=Process.ProcessId
INNER JOIN @tblsites tblsites ON tblsites.SiteID=Sites.SiteID
INNER JOIN @tblprocess tblprocess ON tblprocess.processid=Process.ProcessId
WHERE Lines.IsActive=1 AND Instruments.IsActive=1
AND IRM.ReadingDateTime >=@StartDate AND
IRM.ReadingDateTime <=@EndDate
GROUP BY

IRM.ReadingDateTime,
Instruments.InstrumentID,
Instruments.InstrumentName,
IRD.Reading,Instruments.InstrumentOrder,
Units.DisplayUnit,DisplayFormula,[TYPE],LowThreshold,
SiteName,
Lines.LineID,
Lines.LineName
ORDER BY IRM.ReadingDateTime


There is an Non clustered covering index on InstrumentReadingDetails
on columns InstrumentId,InstrumentReadingMasterID & reading.But there is not much improvement.There are about 1.5 million records that are that satisfy the condition.
Really appreciate any insight.


PBUH

AndrewMurphy
Master Smack Fu Yak Hacker

2916 Posts

Posted - 2009-10-06 : 04:02:48
Posting the execution plan would be of benefit to those not familiar with your full setup.
Go to Top of Page

Sachin.Nand

2937 Posts

Posted - 2009-10-06 : 05:13:02
quote:
Originally posted by AndrewMurphy

Posting the execution plan would be of benefit to those not familiar with your full setup.



Thanks for the reply.I have uploaded the execution plan here.

http://rapidshare.com/files/289311033/ExecPlan.sqlplan.html

PBUH
Go to Top of Page

swatip
Starting Member

8 Posts

Posted - 2009-10-06 : 06:50:12
Hi,
Just check once again the covered nonclustered index on InstrumentReadingDetails. For the covered index the columns which are used for the sorting,grouping or for the filtering purpose should be part of the index key and rest columns should be included columns.Check the same for the other tables which are included in the query.
Go to Top of Page

Sachin.Nand

2937 Posts

Posted - 2009-10-07 : 08:49:09
Hi,
I tried all kind of indexes but cud not see any performance difference.
But if I use just one column name in the select clause keeping all the columns in the group by clause as it is there is huge performance difference almost 50%.
Dont know why that is :( & how can I improvise on that?

PBUH
Go to Top of Page
   

- Advertisement -