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

前端 未结 6 973
天涯浪人
天涯浪人 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 20:47

    Query Execution Time:

    DECLARE @EndTime datetime
    DECLARE @StartTime datetime 
    SELECT @StartTime=GETDATE() 
    
    
    ` -- Write Your Query`
    
    SELECT @EndTime=GETDATE()
    --This will return execution time of your query
    SELECT DATEDIFF(MILLISECOND,@StartTime,@EndTime) AS [Duration in millisecs] 
    

    Query Out Put Will be Like:

    enter image description here

    To Optimize Query Cost :

    Click on your SQL Management Studio

    enter image description here

    Run your query and click on Execution plan beside the Messages tab of your query result. you will see like

    enter image description here

    0 讨论(0)
  • 2020-11-29 20:55

    The results of the execution time directly contradict the results of the Query Cost, but I'm having difficulty determining what "Query Cost" actually means.

    Query cost is what optimizer thinks of how long your query will take (relative to total batch time).

    The optimizer tries to choose the optimal query plan by looking at your query and statistics of your data, trying several execution plans and selecting the least costly of them.

    Here you may read in more detail about how does it try to do this.

    As you can see, this may differ significantly of what you actually get.

    The only real query perfomance metric is, of course, how long does the query actually take.

    0 讨论(0)
  • 2020-11-29 21:01

    The profiler trace puts it into perspective.

    • Query A: 1.3 secs CPU, 1.4 secs duration
    • Query B: 2.3 secs CPU, 1.2 secs duration

    Query B is using parallelism: CPU > duration eg the query uses 2 CPUs, average 1.15 secs each

    Query A is probably not: CPU < duration

    This explains cost relative to batch: 17% of the for the simpler, non-parallel query plan.

    The optimiser works out that query B is more expensive and will benefit from parallelism, even though it takes extra effort to do so.

    Remember though, that query B uses 100% of 2 CPUS (so 50% for 4 CPUs) for one second or so. Query A uses 100% of a single CPU for 1.5 seconds.

    The peak for query A is lower, at the expense of increased duration. With one user, who cares? With 100, perhaps it makes a difference...

    0 讨论(0)
  • 2020-11-29 21:06
    SET STATISTICS TIME ON
    
    SELECT * 
    
    FROM Production.ProductCostHistory
    WHERE StandardCost < 500.00;
    
    SET STATISTICS TIME OFF;
    

    And see the message tab it will look like this:

    SQL Server Execution Times:
    
       CPU time = 0 ms,  elapsed time = 10 ms.
    
    (778 row(s) affected)
    
    SQL Server parse and compile time: 
    
       CPU time = 0 ms, elapsed time = 0 ms.
    
    0 讨论(0)
  • 2020-11-29 21:07

    Use SET STATISTICS TIME ON

    above your query.

    Below near result tab you can see a message tab. There you can see the time.

    0 讨论(0)
  • 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

    0 讨论(0)
提交回复
热议问题