There’s a Bug in sys.dm_exec_query_plan_stats.

SQL Server 2025

When you turn on last actual plans in SQL Server 2019 and newer:

The system function sys.dm_exec_query_plan_stats is supposed to show you the last actual query plan for a query. I’ve had really hit-or-miss luck with this thing, but my latest struggle with it is that two of the numbers are flat out wrong. It mixes up CPU time and elapsed time.

Here’s a simple query to prove it:

That query goes parallel, burns a lot of CPU, and finishes in a few seconds:

CPU time is higher than elapsed time, indicating that the query went parallel across multiple cores. The SET STATISTICS TIME numbers are correct, as are the time numbers in the actual query plan.

But if you check the last “actual” query plan in sys.dm_exec_query_plan_stats:

The numbers are exactly backwards:

QueryTimeStats

The last “actual” plan says the query took 8 seconds (it didn’t) and that it burned less CPU time than elapsed time. The numbers are just transposed.

I’ve filed a bug about this, but this is where it gets tricky as a script author. Sure, for now, I can simply reverse the numbers in my First Responder Kit scripts, fixing Microsoft’s error, but … now I gotta watch every upcoming set of Cumulative Update notes, reading to see if they fixed the bug, then go back to any relevant scripts, put in build number checking, and flip the math for the corrected builds.

Previous Post
Building My Dev/Prod Demon Hunters Session, Part 2: The Tactics

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.