RESOURCE_SEMAPHORE_QUERY_COMPILE Wait Type in SQL Server

⏳Part of the SQL Server Wait Types Library, every wait type explained.Related deep dive: RESOURCE_SEMAPHORERelated pillar: Performance & Troubleshooting
⚡RESOURCE_SEMAPHORE_QUERY_COMPILE is a query stuck before execution, waiting for a slot to keep compiling because too many memory-hungry compilations are running at once. Short bursts after a restart are normal. Sustained, it means a steady stream of expensive compiles or recompiles. Run the check to see which gateway is full and how fast the server is compiling.

Microsoft describes it as occurring when the number of concurrent query compilations reaches a throttling limit, and says high waits can point at excessive compilations, recompiles or plans that cannot be cached. It is not the runtime memory grant wait; that is RESOURCE_SEMAPHORE, where a compiled plan queues for execution memory.

The throttle is a set of compile memory gateways. The gateways view documents three, small, medium and big, each allowing fewer concurrent compilations than the last, so a compilation that needs a lot of memory has to get through a gate with very few slots. On our lab the small gateway allowed 32, the medium 8 and the big 1.


Is It a Problem?

Short bursts happen after restarts, failovers and deployments, when the plan cache is cold and everything compiles at once. It becomes a real problem when it persists through normal hours, because queries are stalling before they even start running.

Judge it over an interval rather than the cumulative totals. The baseline to monitor is its wait time per interval next to compilations per second; if both rise together, the server is compiling more than it should.


Common Causes

  • Very complex statements that need a lot of memory to compile: huge IN lists, large generated queries, views nested on views.
  • Poor plan reuse, where ad hoc query text that varies slightly compiles again every time.
  • Frequent recompiles from schema changes, statistics updates or RECOMPILE hints.
  • Memory pressure overall, which leaves less room for compilation.

The Check To Run

Rank it against everything else with Get Wait Statistics. While it is happening, see which gateway is full with sys.dm_exec_query_optimizer_memory_gateways, from SQL Server 2016:

SELECT
    pool_id,
    name,
    max_count,
    active_count,
    waiter_count
FROM sys.dm_exec_query_optimizer_memory_gateways
ORDER BY pool_id, max_count DESC;

A gateway with waiter_count above zero is where the queue is; the big gateway allowing a single compile at a time is the usual one. Then check how much the server compiles. These counters are cumulative, so read them twice a minute apart and take the difference:

SELECT
    object_name,
    counter_name,
    cntr_value
FROM sys.dm_os_performance_counters
WHERE counter_name IN (N'Batch Requests/sec', N'SQL Compilations/sec', N'SQL Re-Compilations/sec')
  AND object_name LIKE N'%SQL Statistics%';

Compilations running at a large share of batch requests means plans are not being reused. Both queries need VIEW SERVER STATE.


What To Do

  1. Find the expensive and repeated compiles. Query Store, or the plan cache with plan_generation_num in sys.dm_exec_query_stats, shows the statements that recompile most.
  2. Simplify the worst statements; compile cost grows with complexity, and splitting a giant statement into steps often removes the problem.
  3. Improve plan reuse by parameterising ad hoc patterns. Forced parameterization for one database is an option when the application cannot change.
  4. Check memory overall. If RESOURCE_SEMAPHORE is high at the same time, the instance is short of memory in general, not just at compile.

The RESOURCE_SEMAPHORE Family

These waits are memory and resource gates: a query queuing for workspace memory to run, for memory to compile, or for the mutex that hands both out. The main page is RESOURCE_SEMAPHORE, with two walkthroughs for the runtime grant wait.

  • RESOURCE_SEMAPHOREMemory grants. A compiled query waiting for the workspace memory it needs to run.The main page for this family
  • Troubleshoot RESOURCE_SEMAPHOREStep by step. Finding the queries holding and waiting for grants, and what to change.The walkthrough
  • Below max server memoryThe odd case. Grant waits while SQL Server is nowhere near its memory cap.A case study
  • RESOURCE_SEMAPHORE_QUERY_COMPILECompile memory. A query waiting at a compile memory gateway before it can finish compiling.Worth a look when it climbs. This page.
  • RESOURCE_SEMAPHORE_MUTEXThe gate itself. The mutex around handing out grants and thread reservations.Worth a look when it climbs
  • RESOURCE_SEMAPHORE_SMALL_QUERYSmall query lane. A small query waiting for a grant; documented for SQL Server 2008 R2 only.Older versions only

Where To Go Next

If RESOURCE_SEMAPHORE_QUERY_COMPILE is sitting near the top of your wait stats, these are the next places to look.

Comments

Leave a Reply

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