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 2000 Forums
 Transact-SQL (2000)
 How do i...

Author  Topic 

jayp369
Starting Member

26 Posts

Posted - 2007-01-22 : 17:12:32
SELECT h.* FROM tHistoryLog h
WHERE
(SELECT COUNT(*) FROM tHistoryLog w WHERE w.EventID = h.EventID
AND h.ApplicationID = w.ApplicationID
AND h.TechnologyID = w.TechnologyID
AND h.MachineName = w.MachineName
AND h.CreateDate <= w.CreateDate ) <=5

I want to get the top 5 records from history for each EventID, ApplicationID, TechnologyID, MachineName
this script too long to run.

Is there an easier way to do this?

Jay

snSQL
Master Smack Fu Yak Hacker

1837 Posts

Posted - 2007-01-22 : 18:55:37
On SQL Server 2000, you're going to have to use either a cursor or a temporary table, or even both. On 2005 with the ROW_NUMBER function or with CTEs you could do it in a single query.

Go to Top of Page

Jeff Moden
Aged Yak Warrior

652 Posts

Posted - 2007-01-22 : 20:38:34
The correlated subquery works very fast (much faster than a cursor or while-loop) but ONLY if you get rid of the triangular join... like this...

SELECT h.*
FROM tHistoryLog h
WHERE h.EventID IN (SELECT TOP 5 w.EventID
FROM tHistoryLog w
WHERE w.EventID = h.EventID
AND w.ApplicationID = h.ApplicationID
AND w.TechnologyID = h.TechnologyID
AND w.MachineName = h.MachineName
ORDER BY w.CreateDate DESC)

The other thing is to make sure that you have proper indexing... proper indexing of something like this will make a half hour run change into a 10 second run. A primary key is usually helpful, as well.

By the way, if the EventID is the PK or a unique index, none of what's been posted will do the trick because of uniqueness.

--Jeff Moden
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2007-01-23 : 00:44:40
Try this and see if it is faster
-- stage the history log
CREATE TABLE #Stage
(
RowID INT IDENTITY PRIMARY KEY,
EventID INT,
ApplicationID INT,
TechnologyID INT,
MachineName VARCHAR(20),
CreateDate DATETIME
)

-- copy the data in the wanted order
INSERT #Stage
(
EventID,
ApplicationID,
TechnologyID,
MachineName,
CreateDate
)
SELECT EventID,
ApplicationID,
TechnologyID,
MachineName,
CreateDate
FROM tHistoryLog
ORDER BY EventID,
ApplicationID,
TechnologyID,
MachineName,
CreateDate DESC

-- create a temporary table for each group
CREATE TABLE #Temp
(
MinRowID INT,
MaxRowID INT
)

-- select each group range
INSERT #Temp
(
MinRowID,
MaxRowID
)
SELECT MIN(RowID) AS MinRowID,
4 + MIN(RowID) AS MaxRowID
FROM #Stage
GROUP BY EventID,
ApplicationID,
TechnologyID,
MachineName

-- show the groups of data
SELECT s.EventID,
s.ApplicationID,
s.TechnologyID,
s.MachineName,
s.CreateDate
FROM #Stage AS s
INNER JOIN #Temp AS t ON t.MinRowID <= s.RowID AND t.MaxRowID >= s.RowID
ORDER BY s.RowID

-- clean up temporary tables
DROP TABLE #Temp
DROP TABLE #Stage


Peter Larsson
Helsingborg, Sweden
Go to Top of Page
   

- Advertisement -