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)
 sql optimization

Author  Topic 

arkiboys
Master Smack Fu Yak Hacker

1433 Posts

Posted - 2010-12-15 : 02:16:14
Incorrect post.
So sorry...

chadmat
The Chadinator

1974 Posts

Posted - 2010-12-15 : 03:20:36
About how many rows are being inserted into each table variable? You should really be using temp tables unless that number is very close to 1.

-Chad
Go to Top of Page

arkiboys
Master Smack Fu Yak Hacker

1433 Posts

Posted - 2010-12-15 : 04:01:59
About over 700 records in each table variable.
I have used temp tables instead but did not make a difference.
May be somehow I should reduce the number of table variables?
Thanks
Go to Top of Page

Sachin.Nand

2937 Posts

Posted - 2010-12-15 : 04:48:27
You need to split the whole query and check which part of the query is taking the most time.What about indexes?
Do you have any index on CaptureKeyID and CaptureDataStatusEnum ?

PBUH

Go to Top of Page

Ifor
Aged Yak Warrior

700 Posts

Posted - 2010-12-15 : 06:43:32
I am not convinced that some of the logic makes much sense but you should be able to cut down on the number of reads by doing something like:

;WITH cte1
AS
(
SELECT ck.CaptureKeyID, ck.CaptureKeyName
,MIN(CASE WHEN CaptureDataStatusEnum = 3 THEN TransformDateTime END) AS minTransformDateTime
,MAX(CASE WHEN CaptureDataStatusEnum = 3 THEN TransformDateTime END) AS TransformDateTime
,SUM(CASE WHEN CaptureDataStatusEnum = 3 THEN 1 ELSE 0 END) AS num
,MAX(CaptureDateTime) AS CaptureDateTime
FROM CaptureKey AS ck
JOIN CaptureData AS cd
ON ck.CaptureKeyID = cd.CaptureKeyID
GROUP BY ck.CaptureKeyID, ck.CaptureKeyName
)
,cte2
AS
(
SELECT CaptureKeyID, CaptureKeyName
,TransformDateTime
,1.0 * DATEDIFF(hh,minTransformDateTime, TransformDateTime)/num AS avgdur
,CASE
WHEN (DATEDIFF(hh, TransformDateTime, GETDATE())) > (3 * (1.0 * DATEDIFF(hh, minTransformDateTime, TransformDateTime)/num))
THEN 'Failed'
ELSE 'Passed'
END AS StatusMessage
,CaptureDateTime
FROM cte1
WHERE TransformDateTime IS NOT NULL
)
SELECT
CaptureKeyID
,CaptureKeyName
,CaptureDateTime
,TransformDateTime
--,AvgDur
,CASE
WHEN StatusMessage = 'Failed' AND (DATEDIFF(hh, CaptureDateTime , GETDATE())) > (3 * AvgDur)
THEN 'true'
WHEN StatusMessage = 'Failed'
THEN 'false'
END AS WebSiteWatcherFailure
,CASE
WHEN StatusMessage = 'Failed' AND (DATEDIFF(hh, CaptureDateTime , GETDATE())) > (3 * AvgDur)
THEN 'false'
WHEN StatusMessage = 'Failed'
THEN 'true'
END AS TransformerFailure
,CASE
WHEN StatusMessage = 'Failed' AND (DATEDIFF(hh, CaptureDateTime , GETDATE())) > (3 * AvgDur)
THEN 'WebSite Watcher Down'
WHEN StatusMessage = 'Failed'
THEN 'Transform Problem'
END AS TransformerFailure
FROM cte2
WHERE StatusMessage = 'Failed'

Go to Top of Page
   

- Advertisement -