Results 1 to 8 of 8

Thread: Any way to optimize this query?

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    May 2002
    Posts
    1,602

    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

  2. #2
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: Any way to optimize this query?

    Did you try something like this:

    sql Code:
    1. Select LAL.*
    2. From LineActivityLog LAL
    3. INNER JOIN ( SELECT MAX(LAL1.LineActivityLogID)
    4.                        FROM LineActivityLog LAL1    
    5.                        WHERE LAL1.LineID = @LineID
    6.                         AND LAL1.EndTime = ( SELECT MAX (LAL2.EndTime)
    7.                                                        FROM LineActivityLog LAL2  
    8.                                                        WHERE LAL2.LineID = @LineID)
    9.                   ) x
    10.     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

  3. #3
    PowerPoster
    Join Date
    Feb 2002
    Location
    Canada, Toronto
    Posts
    5,803

    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.

  4. #4
    Wall Poster TysonLPrice's Avatar
    Join Date
    Sep 2002
    Location
    Columbus, Ohio
    Posts
    3,969

    Re: Any way to optimize this query?

    Quote Originally Posted by CVMichael View Post
    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.

  5. #5
    PowerPoster
    Join Date
    Feb 2002
    Location
    Canada, Toronto
    Posts
    5,803

    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.

  6. #6
    Wall Poster TysonLPrice's Avatar
    Join Date
    Sep 2002
    Location
    Columbus, Ohio
    Posts
    3,969

    Re: Any way to optimize this query?

    Quote Originally Posted by CVMichael View Post
    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
    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.

  7. #7
    PowerPoster
    Join Date
    Feb 2002
    Location
    Canada, Toronto
    Posts
    5,803

    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"

  8. #8
    Wall Poster TysonLPrice's Avatar
    Join Date
    Sep 2002
    Location
    Columbus, Ohio
    Posts
    3,969

    Re: Any way to optimize this query?

    Quote Originally Posted by CVMichael View Post
    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
  •  



Click Here to Expand Forum to Full Width