Lightweight, standard, last plan: the query profiling family in SQL Server
"Turn on lightweight profiling." "It's been on since 2019." "I mean the last actual plan." "That needs trace flag 7412." "No, 2451." Conversations about actual execution plans often go like this. That isn't the participants' fault. Over ten years, Microsoft has added many small features that build on each other and have very similar names. This page sorts them out: what each switch does, since when it exists, who uses it and where it gets expensive.
In short
- There are only two mechanisms. Standard profiling counts a lot and costs a lot. Lightweight profiling counts less, mainly rows per operator, and costs little. All other names are switches, events or views on these two.
- From SQL Server 2019 on, lightweight profiling is always on. That is why you can see there, without any preparation, what a running query is doing right now. Turning it off saves little and takes this view away.
- The "last actual plan" is a switch of its own:
LAST_QUERY_PLAN_STATS, off by default. It builds on lightweight profiling and did not cost anything noticeable in our lab. - It gets expensive with Extended Events sessions that contain plan events, even with "lightweight" in the name and even with a filter. More on this in the article on the XE plan trap.
- No actual plan from lightweight profiling contains times per operator. If you want to know where the time goes, you need standard profiling for a single execution or Query Store for the history.
The family tree
Standard profiling: the "real" actual plan
Standard profiling captures rows, CPU and I/O per operator. This is the plan SSMS shows with "Include Actual Execution Plan". It exists in every supported version and is turned on in two ways:
- for one session:
SET STATISTICS XML ON,SET STATISTICS PROFILE ON, the SSMS buttons "Include Actual Execution Plan" and "Include Live Query Statistics" - for the whole server: the Extended Event
query_post_execution_showplan(SQL Server 2012 and later) or the trace event "Showplan XML Statistics Profile"
For one session this is uncritical, because it only affects your own queries. Server-wide, according to Microsoft, the infrastructure is attached to "every query that is running on the server". In a test of its own, Microsoft reported up to roughly 75% overhead for standard profiling (SQL Server Team Blog).
Lightweight profiling v1: SQL Server 2014 SP2 and 2016
The first lightweight variant collects rows and I/O per operator, but no CPU time. It is turned on via the Extended Event query_thread_profile while the session is running. What for: sys.dm_exec_query_profiles shows the progress of running queries without them having been started with SET STATISTICS. Microsoft: considerably less overhead, but it still affects server performance.
Lightweight profiling v2: SQL Server 2016 SP1 and 2017
According to Microsoft, the revised version has "minimal overhead". Microsoft gives about 1.5 to 2% for it in a TPC-C-like test (SQL Server Team Blog). New are:
- Trace flag 7412 turns on v2 globally.
sys.dm_exec_query_statistics_xmlreturns the plan of a running query with the counters so far, from another session.- The hint
QUERY_PLAN_PROFILEwith the eventquery_plan_profile(2016 SP2 CU3 and 2017 CU11 and later): only marked queries deliver their actual plan to Extended Events. According to Microsoft, this doesn't burden the rest of the workload. query_post_execution_plan_profile(2017 CU14 and later): an event with the actual plan based on lightweight profiling, for all queries.
For 2016 and 2017, Microsoft refers to KB 4078596, a possible access violation with dm_exec_query_statistics_xml.
In the lab on SQL Server 2017: trace flag 7412 cost measurably, but little, more with queries that have many operators. A session with query_post_execution_plan_profile, by contrast, made every execution several times more expensive, even though its filter captured nothing. That stayed the same when the queries came in over real client connections, and it showed the same way on 2019, 2022 and 2025.
Lightweight profiling v3: SQL Server 2019 and later
- On by default, for every query. Can be turned off per database with
ALTER DATABASE SCOPED CONFIGURATION SET LIGHTWEIGHT_QUERY_PROFILING = OFF. Trace flag 7412 no longer has any effect. - New: the last actual plan. With
LAST_QUERY_PLAN_STATS = ON(per database) or trace flag 2451 (global), SQL Server keeps the counters of the last execution for every plan in the cache. It can be retrieved viasys.dm_exec_query_plan_stats. The option is off by default.
In the lab:
- Lightweight profiling v3 cost little, most visibly with very short queries and with plans that have many operators; with batch mode nothing noticeable. In our view, turning it off still isn't worth it: you lose the view of running queries and the last plan.
LAST_QUERY_PLAN_STATSadded a smaller extra on top. With fully loaded cores, it was barely measurable on 2022. On 2019 and 2025 the measurements varied more and individual query shapes came out somewhat higher; in no case did the option noticeably affect operations.- Without lightweight profiling,
LAST_QUERY_PLAN_STATSreturns no runtime values, so the option builds on v3. Trace flag 2451 works even when the database option is off. - Switching the database option clears the plan cache of that database.
SQL Server 2022: permissions, parameters, Query Store hints
- Permissions: from 2022 on,
VIEW SERVER PERFORMANCE STATEis enough forsys.dm_exec_query_plan_statsinstead ofVIEW SERVER STATE. FORCE_SHOWPLAN_RUNTIME_PARAMETER_COLLECTION: this database option adds the runtime values of the parameters to lightweight plans. Microsoft itself warns of additional, possibly significant CPU and memory overhead and recommends it only for a limited time. In the lab, it cost noticeably with short queries. The runtime values still did not show up in the last actual plan (dm_exec_query_plan_stats).- Query Store hints let you attach
USE HINT(...)to a single query without changing the code. In the lab, this also worked withQUERY_PLAN_PROFILE, without any plan guide: the hint was accepted viasys.sp_query_store_set_hints, every execution of the marked query delivered aquery_plan_profileevent, other queries none. The plan in the event contained the rows per operator and, unlike the last plan from the cache, the runtime values of the parameters as well. The rest of the workload was unaffected, but the marked query itself became several times more expensive on every execution, just as with the XE plan events. This is a tool for a short time window or for rare, expensive queries, not for a query that runs thousands of times per second. Tested on SQL Server 2022 and 2025.
SQL Server 2025: no new generation
Microsoft doesn't describe a new profiling generation. The what's-new pages for 2022 and 2025 don't mention lightweight profiling. From 2025 on, Query Store hints can also be set on secondary replicas of an availability group. In the lab, 2025 behaved like 2019 and 2022 in content and cost, with one exception in the time values of the last plan (see below).
What is in which plan?
| Actual plan (standard profiling) | In-flight plan | Last plan | |
|---|---|---|---|
| When | one execution that was started with profiling | while the query is running, only with profiling (see below) | after the last execution, as long as the plan is in the cache |
| Rows per operator | yes | progress so far | yes |
| CPU and time per operator | yes | only if the query runs with standard profiling | no |
| Spill warning | yes | yes, as soon as the spill has happened | yes |
| Granted and used memory | yes | yes, peak so far | yes, from the last execution |
| Runtime values of the parameters | yes | no in the lab, not even with the switches meant for this (see below) | no, in the lab not even with FORCE_SHOWPLAN_RUNTIME_PARAMETER_COLLECTION |
Overall time (QueryTimeStats) | yes | time so far of the running statement | unreliable, see below |
| Simple (trivial) plan | complete | complete | only the top operator |
The in-flight plan is a snapshot. Microsoft describes it as the plan of a query that is currently running, with "partial statistics"; Microsoft does not document what exactly it contains. So we looked in the lab on SQL Server 2017, 2019, 2022 and 2025, with a query whose sort spills to tempdb and with a simple plan in a loop. The result:
- Without profiling there is no in-flight plan. The function then returns nothing. From SQL Server 2019 on, lightweight profiling is on by default; on 2017 you need trace flag 7412 or a query that itself runs with standard profiling. Once the query has finished, the function returns nothing either.
- Rows, spill warning, memory and overall time are in the plan while it is still running, in all four versions. The spill warning appears as soon as the sort has spilled, not only at the end. The overall time refers to the statement that is running, not to the whole procedure.
- CPU and time per operator are only there if the query itself runs with standard profiling, for example with
SET STATISTICS XML ON. Under lightweight profiling they are missing entirely from 2019 on; on 2017 the fields are in the plan but contain only zeros. - Simple plans are complete in the in-flight plan, unlike in the last plan from the cache.
- Parameter values: Microsoft removed them from the in-flight plan because of a possible access violation (2017 CU26, 2019 CU12). They are supposed to come back with trace flag 2446 from 2017 CU31 and 2019 CU19 on, and with
FORCE_SHOWPLAN_RUNTIME_PARAMETER_COLLECTIONfrom 2022 on, each with additional overhead according to Microsoft (Learn). In our lab, the parameter values still did not appear, neither for a stored procedure nor forsp_executesql, with neither of the two switches and in no version. The plan only ever showed the value it was compiled with. If you need the runtime values, you will find them in the actual plan of a single execution or via the hintQUERY_PLAN_PROFILE(see above).
The time values in the last plan are unusable in every version, just wrong in different ways. In our lab, SQL Server 2019 and 2022 showed roughly the compile time on the first execution and zero afterwards. SQL Server 2025 shows execution times, but with CPU time and elapsed time swapped as soon as the two differ, for example with parallel plans. Brent Ozar reported this in April 2026 (blog post), and Microsoft has announced a fix. So take the time of a query from sys.dm_exec_query_stats or Query Store.
Which tool uses what?
| Tool | Path | Mechanism |
|---|---|---|
| SSMS "Include Actual Execution Plan" | SET STATISTICS XML for your own session | Standard |
| SSMS "Include Live Query Statistics" | standard profiling for your own session | Standard |
| SSMS Activity Monitor, live plans | dm_exec_query_profiles | Lightweight, if active |
sp_WhoIsActive with @get_plans = 2 | dm_exec_query_statistics_xml | Lightweight, or standard if active for the query |
| sp_BlitzCache | dm_exec_query_plan_stats, if available and richer than the cached plan | Lightweight (last plan) |
| sp_Blitz | reports LAST_QUERY_PLAN_STATS = ON as a deviation from the default | – |
| Query Store | estimated plans and runtime statistics over time, no rows per operator | none |
Tools as of: source code read on Nov 26, 2026.
Check: what is turned on on my server?
/* PSG · Profiling family · Query 1: Which profiling switches are active? Shows LIGHTWEIGHT_QUERY_PROFILING, LAST_QUERY_PLAN_STATS and (2022 and later) FORCE_SHOWPLAN_RUNTIME_PARAMETER_COLLECTION per database, plus the global trace flags 7412 and 2451. Read-only, changes nothing. Runs across all databases that are online; databases without access are skipped. XE sessions with plan events are shown by query 1 in the article on the XE plan trap. SQL Server 2019 and later. Permissions: VIEW SERVER STATE, from SQL Server 2022 on also VIEW SERVER PERFORMANCE STATE (tested on 2019 through 2025). A database only appears if the login has access to it. OPTION (MAXDOP 1): the diagnostic query should not tie up parallel workers. */SET NOCOUNT ON;DECLARE @settings TABLE (database_name sysname, setting sysname, value sql_variant);DECLARE @db sysname, @sql nvarchar(max);DECLARE dbs CURSOR LOCAL FAST_FORWARD FOR SELECT name FROM sys.databases WHERE state_desc = N'ONLINE' AND HAS_DBACCESS(name) = 1 AND database_id > 4 ORDER BY name;OPEN dbs;FETCH NEXT FROM dbs INTO @db;WHILE @@FETCH_STATUS = 0BEGIN SET @sql = N'SELECT ' + QUOTENAME(@db, N'''') + N', name, value FROM ' + QUOTENAME(@db) + N'.sys.database_scoped_configurations WHERE name IN (N''LIGHTWEIGHT_QUERY_PROFILING'', N''LAST_QUERY_PLAN_STATS'', N''FORCE_SHOWPLAN_RUNTIME_PARAMETER_COLLECTION'') OPTION (MAXDOP 1);'; INSERT @settings EXEC (@sql); FETCH NEXT FROM dbs INTO @db;END;CLOSE dbs; DEALLOCATE dbs; SELECT database_name, setting, value, CASE WHEN setting = N'LIGHTWEIGHT_QUERY_PROFILING' AND value = 0 THEN N'deviates: off' WHEN setting = N'LAST_QUERY_PLAN_STATS' AND value = 1 THEN N'deviates: on' WHEN setting = N'FORCE_SHOWPLAN_RUNTIME_PARAMETER_COLLECTION' AND value = 1 THEN N'on: only useful for a limited time' ELSE N'default' END AS assessmentFROM @settingsORDER BY CASE WHEN value = CASE setting WHEN N'LIGHTWEIGHT_QUERY_PROFILING' THEN 1 ELSE 0 END THEN 1 ELSE 0 END, database_name, settingOPTION (MAXDOP 1); DBCC TRACESTATUS (7412, 2451) WITH NO_INFOMSGS;Sample output from our lab (SQL Server 2022, a test database with the last plan turned on):
| database_name | setting | value | assessment |
|---|---|---|---|
| LqpLab | LAST_QUERY_PLAN_STATS | 1 | deviates: on |
| LqpLab | FORCE_SHOWPLAN_RUNTIME_PARAMETER_COLLECTION | 0 | default |
| LqpLab | LIGHTWEIGHT_QUERY_PROFILING | 1 | default |
Trace flags 7412 and 2451 were not set (Status, Global and Session all 0). Deviations from the default come first. On 2019, the row for FORCE_SHOWPLAN_RUNTIME_PARAMETER_COLLECTION is missing, since the option only exists from 2022 on.
Which path for which question?
- "What is the query doing right now?"
sys.dm_exec_query_statistics_xmlor sp_WhoIsActive with@get_plans = 2. From 2019 on without preparation. - "How did the last execution go?" Turn on
LAST_QUERY_PLAN_STATS, thensys.dm_exec_query_plan_stats, targeted at individual plans and not at the whole cache. The article The last actual plan shows queries and limits. - "Where does the time go in this one plan?" Run the query once with
SET STATISTICS XML ONor the SSMS button. That is standard profiling, but only for your own session. - "What happened yesterday at 2 p.m.?" Query Store. It keeps plans and runtime statistics over time, even when the plan cache has long forgotten. Our product PSG QX analyzes Query Store and explains plan changes and outliers, without any additional capture on the server.
- Not: a server-wide XE session with
query_post_execution_showplanorquery_post_execution_plan_profileas a permanent solution.
If your server works at its CPU limit or you want to change profiling switches on a production system, a second look beforehand is worthwhile. Precisely because the names are so similar, it is easy to flip the wrong switch.
How we tested
SQL Server 2017, 2019, 2022 and 2025 in Linux containers with eight cores; we did not measure separately on Windows. Synthetic data without any customer connection. Five query shapes: point lookup, OLTP join, range aggregation in row mode, wide plan with many operators, batch mode. Each with a single session and with fully loaded cores, switches in alternating order, five repetitions. We read the plan contents per version and switch from the XML and compared them with sys.dm_exec_query_stats. For the in-flight plan, we started two queries on a connection of their own and retrieved their plan several times during execution from a second session: with lightweight profiling, with standard profiling, with each of the two switches for parameter values and with no profiling at all.
Sources
- Microsoft Learn: Query profiling infrastructure, sys.dm_exec_query_plan_stats, sys.dm_exec_query_profiles, sys.dm_exec_query_statistics_xml, ALTER DATABASE SCOPED CONFIGURATION, Query hints, Trace flags, Query Store hints
- SQL Server Team: Developers Choice: Query progress – anytime, anywhere
- Pedro Lopes, 2019: What if the Actual Execution Plan was always available for any query?
- Grant Fritchey, September 9, 2019: Get the Last Actual Plan With sys.dm_exec_query_plan_stats
- Brent Ozar, April 21, 2026: There's a Bug in sys.dm_exec_query_plan_stats