There’s a Bug in sys.dm_exec_query_plan_stats.


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

ALTER DATABASE SCOPED CONFIGURATION SET LAST_QUERY_PLAN_STATS = ON;

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:

SET STATISTICS TIME ON; SELECT TOP 101 value FROM GENERATE_SERIES(-10000000, 10000000) ORDER BY 1 DESC; SET STATISTICS TIME OFF;

That query goes parallel,