Measuring Query Performance : “Execution Plan Query Cost” vs “Time Taken”

前端 未结 6 986
天涯浪人
天涯浪人 2020-11-29 20:10

I\'m trying to determine the relative performance of two different queries and have two ways of measuring this available to me:
1. Run both and time each query
2. Ru

6条回答
  •  忘掉有多难
    2020-11-29 21:13

    I understand it’s an old question – however I would like to add an example where cost is same but one query is better than the other.

    As you observed in the question, % shown in execution plan is not the only yardstick for determining best query. In the following example, I have two queries doing the same task. Execution Plan shows both are equally good (50% each). Now I executed the queries with SET STATISTICS IO ON which shows clear differences.

    In the following example, the query 1 uses seek whereas Query 2 uses scan on the table LWManifestOrderLineItems. When we actually checks the execution time however it is find that Query 2 works better.

    Also read When is a Seek not a Seek? by Paul White

    QUERY

    ---Preparation---------------
    -----------------------------
    DBCC FREEPROCCACHE
    GO
    DBCC DROPCLEANBUFFERS
    GO
    
    SET STATISTICS IO ON  --IO
    SET STATISTICS TIME ON
    
    --------Queries---------------
    ------------------------------
    
    SELECT LW.Manifest,LW.OrderID,COUNT(DISTINCT LineItemID)
    FROM LWManifestOrderLineItems LW
    INNER JOIN ManifestContainers MC
        ON MC.Manifest = LW.Manifest
    GROUP BY LW.Manifest,LW.OrderID
    ORDER BY COUNT(DISTINCT LineItemID) DESC  
    
    SELECT LW.Manifest,LW.OrderID,COUNT( LineItemID) LineCount
    FROM LWManifestOrderLineItems LW
    WHERE LW.Manifest IN (SELECT Manifest FROM ManifestContainers)
    GROUP BY LW.Manifest,LW.OrderID
    ORDER BY COUNT( LineItemID) DESC  
    

    Statistics IO

    Execution Plan

提交回复
热议问题