SQL Server Error 1501: Sort Failure Fix Guide
In this article
SQL Server error 1501 (“Sort failure”) usually means a query that needs to sort data ran out of room to do it. The fix is not to call support first; it is to find the query, give it more memory or tempdb space, or change the query so it sorts less data. In most cases the error appears as a message such as “SQL Server error 1501: Sort failure” or a related sort warning in the SQL Server error log or application log. The steps below walk through identifying the cause and resolving it without guesswork.
What You'll Need
- Access to SQL Server Management Studio (SSMS) or another client that can run T-SQL.
- Permission to view the SQL Server error log and run diagnostic queries (VIEW SERVER STATE is typically required).
- Permission to change server memory settings or tempdb configuration, or a database administrator who can.
- The text of the query or application action that triggered the error, if you have it.
Step-by-Step
- Capture the exact error and the query. Look in the SQL Server error log (in SSMS, Object Explorer > Management > SQL Server Logs) and in the Windows Application log. Note the time and the database. If the error came from an application, ask for the query text or reproduce the action while a trace or Extended Events session is running. The goal is to know which statement failed, not just that a sort failed.
- Check whether the query is doing a large sort. Run the query in SSMS with “Include Actual Execution Plan” enabled (Query > Include Actual Execution Plan). Look for a Sort operator with a high estimated or actual row count. A sort that spills to tempdb or that needs more memory than is granted is the usual trigger. If the plan shows a warning icon on the Sort, hover it to read the warning text.
- Look at tempdb free space and configuration. Run
SELECT name, size/128.0 AS size_mb, FILEPROPERTY(name,'SpaceUsed')/128.0 AS used_mb FROM tempdb.sys.database_files;to see tempdb data file sizes and usage. If tempdb is full or its files are very small, sorts that spill to disk can fail. Add a data file or increase the file size if your storage allows it, and confirm tempdb has enough disk space on its volume. - Check server memory and the memory grant. Run
SELECT counter_name, cntr_value FROM sys.dm_os_performance_counters WHERE object_name LIKE '%Memory Manager%' AND counter_name IN ('Total Server Memory (KB)','Target Server Memory (KB)');to compare total and target memory. If total is well below target, SQL Server is under memory pressure. Also check whether “max server memory” is set too low for the workload (right-click the server in Object Explorer > Properties > Memory). Raise it only if the operating system has memory to spare; leave room for Windows and other services. - Reduce the sort the query needs. If the query sorts a large result set, add or adjust an index so the data arrives pre-sorted, remove unnecessary ORDER BY columns, or add a WHERE clause to cut the row count before the sort. For a query that must sort a lot of data, consider whether the application really needs every row sorted in SQL Server or whether it can sort a smaller set after filtering.
- Give the query a larger memory grant if appropriate. If the plan shows a memory grant warning, the query may need more memory than the optimizer estimated. You can test with a query hint such as
OPTION (MIN_GRANT_PERCENT = 10)orOPTION (MAX_GRANT_PERCENT = 25)on the specific statement, but only after confirming the server has free memory. Hints are a workaround, not a substitute for fixing estimates or indexing. - Update statistics and consider the cardinality estimator. Out-of-date statistics can cause the optimizer to underestimate rows and grant too little memory. Run
UPDATE STATISTICSon the affected tables, or let auto-update statistics run. If the database is on an older compatibility level, test whether the current cardinality estimator changes the plan; do this on a test copy first. - Retest and monitor. Run the query again and confirm the error is gone. Watch the error log and tempdb usage for a day or two. If the error returns under load, the cause is likely concurrency: several large sorts competing for the same memory or tempdb space.
Common Mistakes
- Calling technical support before capturing the failing query. Without the statement, support can only guess.
- Setting “max server memory” to a very high value and starving Windows. Leave headroom for the OS and other processes.
- Adding tempdb files without checking disk space, which just moves the failure.
- Applying query hints permanently to every query. Hints fix one statement; they can hurt others.
- Ignoring statistics. A stale statistic is a common reason for a bad memory grant and a sort failure.
Tips
- Use Extended Events or Query Store to catch the failing query and its plan automatically, rather than waiting for a user to report it.
- Keep tempdb on fast storage and sized appropriately for the workload; sorts that spill are sensitive to tempdb speed and free space.
- Review the plan for a Sort operator that receives far more rows than it estimated. That mismatch is the signal to update statistics or rewrite the query.
- If the error only happens at month-end or during batch jobs, check whether those jobs run at the same time and compete for memory.
Troubleshooting
If the error persists after the steps above, work through these checks in order. First, confirm the error is really 1501 and not a different sort-related message; the number and text in the log matter. Second, check whether tempdb is full at the moment of failure by monitoring free space during the failing job. Third, look for memory pressure from other queries: a large report running at the same time can consume the grant your query needs. Fourth, test the query on a restored copy of the database with the same compatibility level, so you can change indexes or hints without affecting production. Fifth, if the query is generated by an application you do not control, ask the vendor whether a newer build changes the query; do not modify vendor code without support. If none of this resolves it, collect the error log entries, the actual execution plan, and the tempdb and memory readings, then contact technical support with those files.
Frequently Asked Questions
What does SQL Server error 1501 mean?
It indicates a sort operation failed, typically because the query did not have enough memory or tempdb space to complete the sort. The exact wording can vary by version.
Is error 1501 caused by a corrupt database?
Not usually. It is most often a resource or query-plan issue. Run DBCC CHECKDB only if you have other signs of corruption, such as consistency errors in the log.
Can I fix it by restarting SQL Server?
A restart clears tempdb and memory state, so the error may disappear temporarily. If it returns, the underlying cause, such as a large sort or low memory, is still present.
Does adding more RAM always fix error 1501?
No. More memory helps if the server is genuinely short, but a query that sorts millions of rows or spills to a full tempdb can still fail. Fix the query or tempdb as well.
Should I contact technical support immediately?
Only after you have captured the failing query, the execution plan, and the error log entries. Support needs those details to diagnose the sort failure.
Will changing the compatibility level help?
It can change the execution plan and memory grant, so it may help or hurt. Test on a copy of the database before changing production.