HestonJames
Programmer
Morning Chaps,
As ever I'm after a little advice on the optimal way to achieve a task. I have the following query at the moment:
Now this works an absolute charm and pulls records from the ThinkTank table along with the last entry for each ThinkTank from the RemoteAddress table. This query is very slick and runs in about 0.01 of a seconds which is great.
Now, I want to pull the last records for each ThinkTank from another table also, which has substantially more data in it (millions of rows) and this has a bit more of a hit on performance, here is my query:
Now this little puppy runs at 35 seconds which isn't much use to me ;-) My question is, before I start to play with indexing again (I have loads of other queries which work off that table which I don't want to upset by changing indexes) is this the most efficient way of doing about what I'm doing?
Many thanks guys, appreciate the help.
Heston
As ever I'm after a little advice on the optimal way to achieve a task. I have the following query at the moment:
Code:
SELECT thinktank.thinktank_ID
, ThinkTank.Name
, ThinkTank.Installed
, RemoteAddress.Address
, RemoteAddress.DateTime
FROM ThinkTank
INNER
JOIN ( SELECT ThinkTank_ID
, MAX(DateTime) AS max_date
FROM RemoteAddress
GROUP
BY ThinkTank_ID ) AS m
ON m.ThinkTank_ID = ThinkTank.ThinkTank_ID
INNER
JOIN RemoteAddress
ON RemoteAddress.ThinkTank_ID = m.ThinkTank_ID
AND RemoteAddress.DateTime = m.max_date
ORDER BY ThinkTank.Installed DESC,
ThinkTank.Name
Now this works an absolute charm and pulls records from the ThinkTank table along with the last entry for each ThinkTank from the RemoteAddress table. This query is very slick and runs in about 0.01 of a seconds which is great.
Now, I want to pull the last records for each ThinkTank from another table also, which has substantially more data in it (millions of rows) and this has a bit more of a hit on performance, here is my query:
Code:
SELECT thinktank.thinktank_ID
, ThinkTank.Name
, ThinkTank.Installed
, RemoteAddress.Address
, RemoteAddress.DateTime
[COLOR=red]
, l.LastLog
[/color]
FROM ThinkTank
INNER
JOIN ( SELECT ThinkTank_ID
, MAX(DateTime) AS max_date
FROM RemoteAddress
GROUP
BY ThinkTank_ID ) AS m
ON m.ThinkTank_ID = ThinkTank.ThinkTank_ID
INNER
JOIN RemoteAddress
ON RemoteAddress.ThinkTank_ID = m.ThinkTank_ID
AND RemoteAddress.DateTime = m.max_date
[COLOR=red]
INNER JOIN (
Select ThinkTank_ID,
MAX(LogDateTime) As LastLog
From LoggingModel.dbo.MessageLog
Group By ThinkTank_ID
) As l
ON l.ThinkTank_ID = ThinkTank.ThinkTank_ID
[/color]
ORDER BY ThinkTank.Installed DESC,
ThinkTank.Name
Now this little puppy runs at 35 seconds which isn't much use to me ;-) My question is, before I start to play with indexing again (I have loads of other queries which work off that table which I don't want to upset by changing indexes) is this the most efficient way of doing about what I'm doing?
Many thanks guys, appreciate the help.
Heston