How to measure the server execution time of SQL

博雄 胡 785 Reputation points
2026-10-21T10:31:37.69+00:00

I want to know how long it takes for the server to complete the SQL after it is sent to the server, so that I can determine whether the time consumed when the program executes it is mainly in SQL Server, the network, or the backend/frontend program.

SQL Server | SQL Server Transact-SQL
0 comments No comments

Answer accepted by question author
Erland Sommarskog 138.5K Reputation points MVP Volunteer Moderator
2026-10-21T10:44:49.4566667+00:00

For quick thing, the easiest to do is:

SET STATISTICS TIME ON
-- Statement goes here
SET STATISTICS TIME OFF

However, this is only convenient if you have a single statement, since this gives you data after each statement. On the other hand, you get both duration and CPU time.

For multiple statements, for instance a stored procedure, what I typically do is:

DECLARE @d2 datetime2(3) = sysdatetime()
EXEC some_sp
PRINT concat('It took ', datediff(ms, @d2, sysdatetime()), ' ms.'

If you want also CPU or you want timings per statement to see which statement that took most time etc, you can use Lee Tudors sp_sqltrace which I host on my web site.

Was this answer helpful?

1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. AI answer

    2026-10-21T10:31:55.7133333+00:00

    To measure only the time spent on SQL Server after the query reaches the server, use SET STATISTICS TIME ON when running the query. This reports server-side CPU time and elapsed time, which helps separate SQL Server execution from network latency or application processing time.

    SET STATISTICS TIME ON
    <YourQuery>
    SET STATISTICS TIME OFF
    

    For a fuller picture, also enable SET STATISTICS IO ON if needed:

    SET STATISTICS TIME ON
    SET STATISTICS IO ON
    <YourQuery>
    SET STATISTICS IO OFF
    SET STATISTICS TIME OFF
    

    In SSMS, the reported values appear in Messages. This lets comparison focus on how long the query ran on the server rather than end-to-end program time.

    Another server-focused method is to capture query duration with Extended Events using events such as SQL:StmtCompleted, SQL:BatchCompleted, or RPC:Completed.

    If an actual execution plan can be collected in SSMS:

    1. Turn on Include Actual Execution Plan.
    2. Run the query.
    3. In Execution plan, select the left-most operator.
    4. In Properties, expand QueryTimeStats.
    5. Check ElapsedTime and CpuTime.

    To compare with application-side time:

    • If application time is much higher than SQL Server elapsed time, the extra time is likely in the network or application processing.
    • ODBC applications can also inspect driver metrics:
      • msExecutionTime: total driver processing time, including waiting for server replies.
      • msNetworkServerTime: time spent waiting for replies from the server.

    This separation helps determine whether the delay is mainly in SQL Server execution, network transit, or client/backend/frontend processing.


    AI-generated content may be incorrect. Read our transparency notes for more information.

    Was this answer helpful?

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.