|
-
Jan 5th, 2012, 07:52 AM
#1
Thread Starter
Frenzied Member
Any way to optimize this query?
Code:
SELECT LineActivityLog.*
FROM LineActivityLog
WHERE LineActivityLog.LineActivityLogID =
(SELECT DISTINCT MAX(LineActivityLog.LineActivityLogID)
FROM LineActivityLog
WHERE LineActivityLog.LineID = @LineID AND LineActivityLog.EndTime =
(SELECT DISTINCT MAX(LineActivityLog.EndTime)
FROM LineActivityLog
WHERE LineActivityLog.LineID = @LineID))
It eats a lot of CPU according to our SQL Server logs, and it is run quite frequent. I was thinking maybe we can use CTE instead of distinct max?
we use sql server 2005
/S
-
Jan 5th, 2012, 08:08 AM
#2
Re: Any way to optimize this query?
Did you try something like this:
sql Code:
Select LAL.*
From LineActivityLog LAL
INNER JOIN ( SELECT MAX(LAL1.LineActivityLogID)
FROM LineActivityLog LAL1
WHERE LAL1.LineID = @LineID
AND LAL1.EndTime = ( SELECT MAX (LAL2.EndTime)
FROM LineActivityLog LAL2
WHERE LAL2.LineID = @LineID)
) x
on LAL.LineActivityLogID = x.LineActivityLogID
Also have you looked at the query plan? What does it show? Have you traced the query? Where is the time being spent?
Sometimes the Programmer
Sometimes the DBA
Mazz1
-
Jan 5th, 2012, 09:08 AM
#3
Re: Any way to optimize this query?
Here you go:
Code:
; WITH
aa AS (
SELECT DISTINCT MAX(lal.EndTime) AS MAX_EndTime
FROM LineActivityLog AS lal
WHERE lal.LineID = @LineID
)
, bb AS (
SELECT DISTINCT MAX(lal.LineActivityLogID) AS MAX_LineActivityLogID
FROM LineActivityLog AS lal
INNER JOIN aa ON lal.EndTime = aa.MAX_EndTime
WHERE lal.LineID = @LineID
)
SELECT lal.*
FROM LineActivityLog AS lal
INNER JOIN bb ON lal.LineActivityLogID = bb.MAX_LineActivityLogID
If you don't have an index on "EndTime", try to put one, and see if there is any speed improvement.
Last edited by CVMichael; Jan 5th, 2012 at 09:18 AM.
-
Jan 5th, 2012, 12:15 PM
#4
Re: Any way to optimize this query?
 Originally Posted by CVMichael
Here you go:
Code:
; WITH
aa AS (
SELECT DISTINCT MAX(lal.EndTime) AS MAX_EndTime
FROM LineActivityLog AS lal
WHERE lal.LineID = @LineID
)
, bb AS (
SELECT DISTINCT MAX(lal.LineActivityLogID) AS MAX_LineActivityLogID
FROM LineActivityLog AS lal
INNER JOIN aa ON lal.EndTime = aa.MAX_EndTime
WHERE lal.LineID = @LineID
)
SELECT lal.*
FROM LineActivityLog AS lal
INNER JOIN bb ON lal.LineActivityLogID = bb.MAX_LineActivityLogID
If you don't have an index on "EndTime", try to put one, and see if there is any speed improvement.
Would an index on "endTime" really help since it is in a function. I'm thinking that makes it non-sargable. Just curious what others think.
-
Jan 5th, 2012, 12:18 PM
#5
Re: Any way to optimize this query?
EndTime is a field in the table from the looks of it...
[edit] Or you mean a "Computed Column" ?
You can create an index on those too: http://msdn.microsoft.com/en-us/library/ms189292.aspx
Last edited by CVMichael; Jan 5th, 2012 at 12:27 PM.
-
Jan 5th, 2012, 01:08 PM
#6
Re: Any way to optimize this query?
 Originally Posted by CVMichael
I not an expert but I thought using an index column in a function made it ignore the index or the look up non-sargable.
http://stackoverflow.com/questions/7...ement-sargable
I didn't look at your link because I didn't mean a computed column.
-
Jan 5th, 2012, 01:13 PM
#7
Re: Any way to optimize this query?
I understand that functions are not sargable, and I know that... but the only function I see in the OP's code is the MAX, and that does not interfere with the index since the index is only used in the JOIN "INNER JOIN aa ON lal.EndTime = aa.MAX_EndTime"
-
Jan 5th, 2012, 01:15 PM
#8
Re: Any way to optimize this query?
 Originally Posted by CVMichael
I understand that functions are not sargable, and I know that... but the only function I see in the OP's code is the MAX, and that does not interfere with the index since the index is only used in the JOIN "INNER JOIN aa ON lal.EndTime = aa.MAX_EndTime"
Gotcha...Thanks!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|