Fixing SQL Server Error 367: "Hint Valid Only With Memory" in Queries
In this article
If you're encountering SQL Server error 367—*"The hint 'OPTION (FAST n)' is valid only with memory optimizations enabled"*—then the issue is clear: your query relies on a hint like OPTION (FAST n) or OPTION (RECOMPILE), but your query plan isn't using in-memory processing. This doesn’t mean the hint is broken; it means your workload isn’t configured for memory-optimized execution. The fix lies in adjusting query execution settings, stats updates, or even the server’s memory allocation. Below is a step-by-step guide to resolve it.
What You'll Need
- A SQL Server instance with at least SQL Server Management Studio (SSMS) or SQL Server Data Tools (SSDT) installed.
- Administrative permissions to modify SQL Server settings.
- Knowledge of your query’s memory allocation behavior (if no stats are up to date, this error often appears).
Step-by-Step
- Check Query Execution Plan for Memory Usage
Open the query in SSMS and right-click the plan to ensure the issue isn’t a misconfigured plan. Look for operators like "Hash Join" or "Nested Loops" marked with a red exclamation point—this indicates the query isn’t using memory optimizations.
If the plan shows "In-Memory" operators, the issue may lie elsewhere (e.g., missing statistics). If not, proceed to the next step.
- Update Statistics for Tables Used in the Query
Run the following command to refresh statistics for all tables involved in the query:
EXEC sp_updatestatsIf you’re working with a large dataset, update statistics for specific tables using:EXEC sp_updatestats @statistics_type = 'Detailed', @include_system_objects = 0, @object_id = OBJECT_ID('TableName')Repeat until the error disappears or the plan updates correctly.
- Enable Query Store (If Available)
If Query Store is enabled, check its settings for "In-Memory Execution" in the query execution plan. Enable it in SQL Server Configuration Manager:
- Open SQL Server Configuration Manager.
- Ensure Query Store is enabled (if not, right-click and enable it).
Restart the SQL Server service after enabling Query Store.
- Adjust Query Optimizer Hints or Use In-Memory Tables
If the hint in your query (e.g., `OPTION (FAST n)`) still fails, replace it with an alternative:
- Use `OPTION (OPTIMIZE FOR UNKNOWN)` to force a plan that accounts for all possible input sizes.
- Convert a table to an in-memory optimized table (IMT) using:
ALTER TABLE TableName ADD CONSTRAINT [PK] PRIMARY KEY CLUSTERED NONCLUSTERED INDEX ON [PrimaryKeyColumn] WITH (ALLOW_ROW_LOCKS = OFF, ALLOW_PAGE_LOCKS = OFF);Ensure the table is marked as in-memory optimized in the Extended Properties.
- Check for Memory-Gated Queries
If the query is still failing, it may be memory-gated by SQL Server. Run this query to check memory usage:
SELECT TOP 1000 * FROM sys.dm_exec_query_memory_grams QP WITH (NOLOCK) JOIN sys.dm_exec_query_plan QP_PLAN ON QP.plan_handle = QP_PLAN.plan_handle WHERE QP_PLAN.is_memory_gated = 1;If memory gating is active, try:
- Increasing the memory allocation for the query via a hint (e.g., `OPTION (MAXDOP 4, MEMORY_GROWTH 256)`).
- Setting `OPTION (RECOMPILE)` to bypass cached plans.
- Restart SQL Server Service
If all else fails, restart the SQL Server service to clear any lingering cached plans:
- Open Services.msc.
- Right-click "SQL Server (Your Instance)" and select "Restart."
Common Mistakes
- Assuming the hint is invalid: The error isn’t a syntax error—it’s a runtime issue. The query has no in-memory plan, so hints like `OPTION (FAST n)` don’t apply.
- Skipping statistics updates: Without up-to-date statistics, SQL Server defaults to using disk-based execution paths, bypassing in-memory optimizations.
- Ignoring memory gating: Even if the plan looks correct, memory constraints may block execution. Always check `sys.dm_exec_query_memory_grams` for gating issues.
Tips
- Use Query Store for Consistency: Enable Query Store to track execution plans and detect drift from memory-optimized queries.
- Test with `OPTION (OPTIMIZE FOR UNKNOWN)`: This hint forces a plan that works across varying input sizes, reducing the risk of memory mismatches.
- Avoid OPTION (FAST n) on Large Tables: This hint assumes the query will use in-memory buffer space, which may not hold for large datasets. Use `OPTION (RECOMPILE)` or IMTs instead.
Troubleshooting
If the error persists after these steps, the issue may be deeper. Here’s a troubleshooting path:
- Verify SQL Server Version
Error 367 is common in SQL Server 2019+ with in-memory optimizations enabled. Check your version with:
SELECT @@VERSION;If you’re on an older version, in-memory optimizations may not be fully supported. Update SQL Server if needed. - Check In-Memory Optimized Tables
If you’re using IMTs, ensure they’re properly configured with `ALLOW_ROW_LOCKS = OFF` and `ALLOW_PAGE_LOCKS = OFF`. Run:
SELECT name, is_memory_optimized FROM sys.tables WHERE is_memory_optimized = 1;to verify. - Review SQL Server Configuration
Check the `sqlserver.memory_optimized_tables` registry key (or equivalent in newer versions) to ensure in-memory table support is enabled. If disabled, SQL Server defaults to disk-based execution.
- Isolate the Query
If the query still fails, split it into smaller parts. Test each step individually to identify where memory optimizations break. Use a temporary table or IMT to test subqueries.
Frequently Asked Questions
Can I fix this error without restarting SQL Server?
Yes. Start with updating statistics or adjusting query hints. Only restart if other methods fail.
Why does my query work in one session but not another?
This often happens due to statistics drift, query cache inconsistencies, or memory gating. Refresh stats or check Query Store for differences in execution plans.
What if my query is using a stored procedure?
Run `EXEC sp_helptext @ProcName` to check for hints inside the procedure. If the stored procedure uses `OPTION (FAST n)`, modify it or replace it with a version that supports in-memory plans.
How do I force SQL Server to ignore memory optimizations?
Use `OPTION (NO_INCREMENTAL)` or disable in-memory tables temporarily for testing. Avoid this unless necessary, as it may degrade performance.
What’s the difference between OPTION (FAST n) and OPTION (RECOMPILE)?
`OPTION (FAST n)` assumes the query will execute in memory, while `OPTION (RECOMPILE)` forces a fresh compilation (useful for dynamic workloads). Use `RECOMPILE` if `FAST n` fails.
Can I fix this error without admin rights?
No. You’ll need admin access to update statistics, restart services, or modify in-memory table configurations.