CPU and elapsed time with SET STATISTICS TIME

A slow query does not necessarily keep the processor busy for its entire duration. SET STATISTICS TIME separates CPU work from elapsed time and also reports how long parsing and compilation took.

Enable the measurement

The option applies to the current session and writes its output to Messages:

SET STATISTICS TIME ON;

SELECT SUM(CONVERT(DECIMAL(38, 4), p.Total * f.Factor)) AS TotalCalculado
FROM dbo.PedidoLaboratorio AS p
CROSS JOIN
(
    VALUES (1), (2), (3), (4), (5), (6), (7), (8)
) AS f(Factor)
OPTION (MAXDOP 1, RECOMPILE);

SET STATISTICS TIME OFF;

Run the complete block with F5 and open Messages. The Results grid contains the calculated value, while the timings appear under SQL Server parse and compile time and SQL Server Execution Times in Messages.

The numbers change between runs and machines. What is reproducible is their location and the separation between compilation, CPU, and elapsed time, not the exact milliseconds shown in the screenshot.

ON remains active for later statements on the same connection. Run OFF when the measurement is complete so the Messages tab does not mix timings from unrelated queries.

The test query deliberately adds calculations and returns one row. MAXDOP 1 limits this execution to one worker to make the first comparison between CPU and duration easier to read; it is not a general recommendation for production queries.

The output separates compilation from execution

SET STATISTICS TIME messages with compilation, CPU, and elapsed time

  1. 1
    Parsing and compilation

    SQL Server parse and compile time reports work performed before the statement runs. The values in this test were 0 and 1 ms.

  2. 2
    Controlled calculation

    SQL Server Execution Times reports 515 ms of CPU and 572 ms elapsed for the calculation query.

  3. 3
    Controlled wait

    The WAITFOR statement reports 0 ms of CPU and 1010 ms elapsed. The processor was not doing work for most of that wait.

Validation statements and other parts of the batch may also produce groups with values close to zero. Relate each group to the statement that precedes it before interpreting a measurement.

parse and compile time: prepare the strategy

Before executing a query, SQL Server must parse the text and have a plan available. parse time applies to syntactic analysis; compile time includes the work required to compile or optimize the statement.

A value of 0 ms does not prove that no work occurred. The work may be below the counter’s resolution, the statement may be simple, or a reusable strategy may already be available. This test uses RECOMPILE to obtain a controlled compilation, not as a recommendation for every query.

CPU time: accumulated processor work

CPU time adds the time during which the query workers used a processor. It excludes time spent stopped while waiting for data, memory, locks, or other resources.

Here the query is restricted to one worker and reports 515 ms of CPU. In a parallel plan, several workers can consume CPU at the same time. Because their work is added together, total CPU can exceed elapsed time.

elapsed time: time from start to finish

elapsed time measures wall-clock time from the start of execution until it finishes. It includes CPU work and periods when the query does not progress because it is waiting for scheduling, storage, locks, or other resources.

The calculation reports 572 ms elapsed and 515 ms of CPU. That proximity describes this execution, but it does not identify where every millisecond was spent or allow the result to be extrapolated to a different workload.

A wait can take time without consuming CPU

The second statement isolates that difference:

WAITFOR DELAY '00:00:01';

The wait lasts about one second and reports 0 ms of CPU. WAITFOR is used only as a teaching contrast here; it is not an optimization technique or a simulation of every wait a real query can encounter.

A large difference between elapsed time and CPU indicates time when the query was not actively using the processor. Other evidence—such as waits, blocking, reads, and the execution plan—is still required to establish the cause.

One execution does not establish a trend

Duration can change with cache state, compilation, parameters, the selected plan, concurrent activity, and available resources. A first execution provides an observation; it is not enough to declare an improvement or a regression.

A useful comparison preserves the query, parameters, and relevant conditions, repeats the measurement, and records the variation. It also distinguishes server time from any additional time an application may spend transferring and processing results.

What we can conclude

In this test, the calculation used 515 ms of CPU over 572 ms elapsed. The controlled wait used 0 ms of CPU over 1010 ms elapsed. The values show that CPU and duration answer different questions; they do not explain the cause of a performance problem on their own.

A reasonable next check

The next entry builds a repeatable baseline: preserve the test conditions, collect several executions, and compare one variable at a time.

Path index

Execution plans

30 articles in Execution plans
  1. 01What an execution plan is for
  2. 02Estimated plans, actual plans, and Live Query Statistics
  3. 03How an execution plan tree is organized
  4. 04Arrows, properties, and execution counts
  5. 05Operator map: what role each node plays
  6. 06Estimated rows and actual rows
  7. 07Index Seek, Index Scan, and Table Scan
  8. 08Key Lookup and repeated work
  9. 09Filter, Compute Scalar, and Top
  10. 10Nested Loops, Hash Match, and Merge Join
  11. 11Sort, Segment, and Sequence Project
  12. 12Stream Aggregate and Hash Match Aggregate
  13. 13Spool: materialization and reuse
  14. 14Parallelism: distributing, repartitioning, and gathering rows
  15. 15Execution plan warnings that require investigation
  16. 16Actions available in the SSMS graphical plan
  17. 17How Showplan XML is organized
  18. 18Walk through Analyze Actual Execution Plan
  19. 19Reads with SET STATISTICS IO
  20. 20CPU and elapsed time with SET STATISTICS TIME
  21. 21Build a repeatable baseline
  22. 22Save and compare two plans
  23. 23History and regressions with Query Store
  24. 24Turn an Analyze Actual Execution Plan scenario into a hypothesis
  25. 25Read a Missing Index suggestion in an execution plan
  26. 26Understand the CREATE INDEX generated by SSMS
  27. 27Decide, create, and verify an index
  28. 28From a slow query to a testable hypothesis
  29. 29Evaluate a change without optimizing for one execution
  30. 30Guided analysis of a reproducible business query