SQL Server Error 1510: Sort Failed Out of Space
In this article
SQL Server error 1510 means a sort operation ran out of room in tempdb. The fix is to free space in tempdb, reduce the size of the sort, and correct the query or configuration that caused it. You do not need to restart SQL Server in most cases; start by checking tempdb free space and the query that triggered the error.
What You'll Need
- Access to SQL Server Management Studio (SSMS) or another client with permission to run diagnostic queries.
- VIEW SERVER STATE permission to query tempdb usage and session details.
- The text of the query or stored procedure that produced the error, if you have it.
- Permission to alter the query, index, or tempdb configuration if changes are needed.
Step-by-Step
- Confirm the error is about tempdb, not the user database. Error 1510 is raised when a sort cannot get enough space or locks in tempdb. Check the full error message and the SQL Server error log for context. If the message names a different database, treat it as a space issue in that database first.
- Check free space in tempdb. Run a query against tempdb to see current usage. A simple check is to open a new query window connected to tempdb and run
EXEC sp_spaceused;. Compare the unallocated space with the size of the sort your query needs. If unallocated space is small, tempdb is the bottleneck. - Identify the session and query that is sorting. Query
sys.dm_exec_requestsjoined tosys.dm_exec_sessionsandsys.dm_exec_sql_textto find long-running or high-grant sessions. Look for requests with a largegranted_query_memoryor a wait type related to tempdb, such asPAGELATCHorRESOURCE_SEMAPHORE. Note the session_id and the SQL text. - Reduce the sort size in the query. The most reliable fix is to make the sort smaller. Add a more selective
WHEREclause, remove unnecessary columns from theSELECTlist, or break a large sort into smaller batches. If the query usesORDER BYon a large result set, consider whether the application really needs every row sorted at once. - Add or fix indexes to avoid the sort. If the query sorts on a column that is not indexed, SQL Server may perform an explicit sort. Create an index on the columns used in the
ORDER BYand join predicates, or update statistics so the optimizer can choose a plan that avoids the sort. Check the actual execution plan for a Sort operator with a large estimated row size. - Free space in tempdb if it is full. If tempdb is full because of other sessions, identify and stop the sessions that are consuming space. You can use
KILLon a session_id after confirming it is safe to terminate. Do not kill system sessions. If tempdb data files are full on disk, free disk space or add a data file to the tempdb filegroup. - Restart SQL Server only as a last resort. Restarting SQL Server recreates tempdb and clears its contents, which resolves the immediate error but does not fix the cause. Before restarting, note the queries and sessions that were using tempdb so you can address them. A restart interrupts all connections and should be done during a maintenance window.
- Prevent recurrence by sizing tempdb correctly. Set tempdb data files to a reasonable initial size and enable file growth with a fixed increment, such as 64 MB or 128 MB, rather than a percentage. Use one data file per CPU core up to eight files, and keep all tempdb files the same size. This reduces allocation contention and gives large sorts room to complete.
- Review the query plan and application logic. If the same query repeatedly causes error 1510, change the application to page results or filter earlier. For reporting queries, schedule them when tempdb usage is low, or move them to a separate reporting instance. Avoid using
SELECT *and unnecessaryDISTINCTorUNIONoperations that force large sorts.
Common Mistakes
- Restarting SQL Server first. A restart clears tempdb but hides the cause. The error will return the next time the same query runs.
- Shrinking tempdb. Shrinking tempdb files causes fragmentation and does not solve the underlying space or query problem. Size tempdb correctly instead.
- Ignoring the query. Error 1510 is often a symptom of a query that sorts too much data. Fixing only the server configuration leaves the bad query in place.
- Killing the wrong session. Terminating a system session or a critical application session can cause data loss or downtime. Confirm the session_id and its query before using
KILL. - Adding tempdb files without checking disk space. Adding a data file on a full disk does not help. Verify free disk space on the volume that hosts tempdb.
Tips
- Monitor tempdb usage regularly with a query against
sys.dm_db_file_space_usageandsys.dm_db_session_space_usageto see which sessions consume the most space. - Use the actual execution plan to find Sort operators with high estimated costs. A warning icon on the Sort operator often indicates insufficient memory or tempdb space.
- Keep tempdb on a fast disk separate from user databases when possible. This reduces I/O contention during large sorts.
- Set
MAXDOPappropriately for reporting queries. A high degree of parallelism can increase tempdb usage for sorts. - Test query changes in a non-production environment before applying them to production.
Troubleshooting
If the error persists after freeing space and reducing the sort, check the following. First, confirm that tempdb has enough disk space on its host volume; a full disk prevents tempdb from growing even if the file has room to grow. Second, check for long-running transactions that hold space in tempdb; query sys.dm_tran_active_transactions and sys.dm_tran_session_transactions to find them. Third, review the query plan for a Sort operator that spills to tempdb; if the estimated memory grant is too low, the query may spill. You can increase the memory grant by updating statistics or rewriting the query to reduce the number of rows sorted. Fourth, if tempdb is configured with a small initial size and percentage growth, change the growth to a fixed increment. Fifth, if the error occurs during index rebuild or DBCC CHECKDB, those operations also use tempdb; schedule them when tempdb usage is low or increase tempdb size beforehand. If none of these steps resolve the error, capture the query text and the tempdb usage at the time of the error and consult a database administrator.
Frequently Asked Questions
What does SQL Server error 1510 mean?
It means a sort operation could not get enough space or locks in tempdb. The sort failed and the query was terminated.
Does restarting SQL Server fix error 1510?
It clears tempdb and may resolve the immediate error, but it does not fix the cause. The error can return if the same query runs again.
How do I check tempdb free space?
Connect to tempdb and run EXEC sp_spaceused;. You can also query sys.dm_db_file_space_usage for detailed usage.
Can I prevent error 1510 by adding more tempdb files?
Adding files can help with allocation contention, but it does not reduce the size of a sort. The query itself must be optimized or the sort must be reduced.
What permissions do I need to diagnose error 1510?
You need VIEW SERVER STATE to query dynamic management views. To kill a session, you need ALTER ANY CONNECTION or membership in sysadmin or processadmin.
Does error 1510 affect only tempdb?
Yes, error 1510 is specific to sort operations in tempdb. Other space errors in user databases have different error numbers.