// SQL Server error reference

SQL Server error 8645: Timeout waiting for memory resources

A query waited too long for a memory grant to run its sorts or hashes and gave up. Too many large queries are competing for workspace memory.

Error number
8645
Severity
17
Category
Memory, log and storage
Error message
A timeout occurred while waiting for memory resources to execute the query in resource pool '%ls' (%ld). Rerun the query.

What it means

Before running, queries that sort or hash ask for a memory grant. If not enough memory is free, they wait in a queue (the RESOURCE_SEMAPHORE wait type). When the wait exceeds the query's grant timeout, it fails with 8645. The usual root cause is a few queries asking for far more memory than they need.

Common causes

  • Several concurrent queries with large memory grants, often from bad cardinality estimates.
  • Missing indexes that force large sorts or hash joins.
  • A low max server memory setting or a Resource Governor limit.

How to fix it

  1. See who is waiting for and who is holding memory grants.

    SELECT session_id, requested_memory_kb, granted_memory_kb, wait_time_ms, query_cost
    FROM sys.dm_exec_query_memory_grants ORDER BY requested_memory_kb DESC;
  2. Tune the queries with the biggest grants: add indexes that remove sorts, update statistics, avoid SELECT * with wide columns in sorted results.

  3. Cap per-query grants with a hint as a stopgap (SQL Server 2012 SP3 and later).

    SELECT * FROM dbo.BigTable ORDER BY SomeColumn OPTION (MAX_GRANT_PERCENT = 10);

Related errors

More memory, log and storage errors

The code and message text are from the Microsoft Database Engine events and errors reference. Browse the full SQL Server error list or search every database error.

// fewer errors, faster fixes

Work on SQL Server in SQLly

A native SQL client for macOS, Windows and Linux with schema-aware IntelliSense, guardrails for risky statements and clear error messages, for SQL Server and the other databases you use.