• Junglee_George (7/28/2015)


    I have these SQL statements inside a WHILE loop :

    SET @StartDate1 = (SELECT MIN(A.startdate) FROM #Activities2 AS A WHERE activityIncId = @ActId )

    SET @EndDate1 = (SELECT MAX(A.enddate) FROM #Activities2 AS A WHERE activityIncId = @ActId)

    I am changing it to:

    SELECT @StartDate1 = MIN(A.startdate), @EndDate1 = MAX(A.enddate) FROM #Activities2 AS A WHERE activityIncId = @ActId

    Will this have any improvement on the performance point of view?

    Oddly enough I suspect that this one will be counter to first blush thoughts.

    If you are optimized for this (i.e. not partitioned table with some form of index that allows a seek to get both the min and max, having 2 queries or one query will be an exceptionally small difference (just the "overhead", if you will, of the 2 executions).

    However, if you are NOT optimized (i.e. you are doing a table scan for both operations) then combining them holds the chance of a 50% improvement in that it is possible that both the min and max can be acquired with a single scan with the second query.

    Best,
    Kevin G. Boles
    SQL Server Consultant
    SQL MVP 2007-2012
    TheSQLGuru on googles mail service