Do triggers compile each time? We are troubleshooting a server that has high CPU utilization. After finding that the queries weren't really causing it, we started looking into compilations.

Performance Monitor is showing less than 50 Compilations/sec and less than 15 Recompilations/sec.

After running an XE session looking for compilations, we are seeing thousands of compilations per second.

This system is using triggers to audit changes. Most of the compilations are due to triggers. The triggers reference sys.dm_tran_active_transactions.

Our first thought was that maybe referencing a DMV in a trigger would cause it to compile each time, or maybe just this specific DMV would cause it. So I started testing that theory. It does compile each time, but I hadn't checked if a trigger compiles each time it is triggered when it doesn't reference the DMV and instead hardcodes a value. It was still compiling each time it got triggered. Dropping the trigger stops the compiles.


We are using sqlserver.query_pre_execution_showplan in an XE session to track the compilations. Why is there a discrepancy between that and the PerfMon counter?
Is it normal that you get a compilation event each time a trigger runs?


Repro script:

CREATE TABLE t1 (transaction_id int, Column2 varchar(100));
CREATE TABLE t2 (Column1 varchar(max), Column2 varchar(100));
GO

CREATE TRIGGER t2_ins
ON t2
AFTER INSERT
AS

INSERT INTO t1
SELECT (SELECT TOP 1 transaction_id FROM sys.dm_tran_active_transactions), Column2
FROM inserted;
GO

--Both of these show compilation events
INSERT INTO t2 VALUES ('row1', 'value1');
INSERT INTO t2 VALUES ('row2', 'value2');
GO

ALTER TRIGGER t2_ins
ON t2
AFTER INSERT
AS

INSERT INTO t1
SELECT 1000, Column2
FROM inserted;
GO

--Both of these show compilation events
INSERT INTO t2 VALUES ('row3', 'value3');
INSERT INTO t2 VALUES ('row4', 'value4');

DROP TRIGGER t2_ins;

--These do not show compilation events
INSERT INTO t2 VALUES ('row5', 'value5');
INSERT INTO t2 VALUES ('row6', 'value6');

DROP TABLE t1, t2;
The XE event being used is leading you incorrectly to think the trigger is actually compiling every execution.  There are two extended events query_pre_execution_showplan and query_post_compilation_showplan that have similar descriptions, but differ by one important word:

query_pre_execution_showplan


  Occurs after a SQL statement is compiled. This event returns an XML
  representation of the estimated query plan that is generated when the
  query is optimized. Using this event can have a significant
  performance overhead so it should only be used when troubleshooting or
  monitoring specific problems for brief periods of time.


query_post_compilation_showplan


  Occurs after a SQL statement is compiled. This event returns an XML
  representation of the estimated query plan that is generated when the
  query is compiled. Using this event can have a significant
  performance overhead so it should only be used when troubleshooting or
  monitoring specific problems for brief periods of time.


The events aren't exactly the same in description and occur at different times from further testing using your repro.  Using a much larger event session definition, it is easy to see where compilations actually are happening.



Here you can see the first compilation happening for the insert statements as prepared plans being auto-parameterized in the green box.  The trigger is compiled in the red box and the plan is inserted into the cache as shown by the sp_cache_insert event.  Then in the orange box the trigger execution gets a cache hit and reuses the trigger plan for the second INSERT statement in the batch, so it's not compiling every execution of the INSERT command and the plan does get reused as you can see with the sp_cache_hit event for the trigger.   

If we run the two INSERT statements individually again after the first execution the trigger doesn't compile again as shown in the events below:



Here the first statement encounters a cache hit for the prepared auto-parameterized version of the statement in cache but a miss for the adhoc batch that was submitted.  The trigger gets a cache hit and doesn't compile again as shown in the red block of events.  The green block of events repeats this behavior for the second INSERT statement run as a separate batch. However, in every case you still see the query_pre_execution_showplan event firing which I can only attribute to the difference in being optimized vs compiled in the event description, but the trigger isn't compiling for every execution as shown by these series of events.