Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

SQL Server Error 1522: Fix Index Build Sort Failure

· 5 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

SQL Server error 1522 occurs when a sort operation during an index build cannot complete because the target filegroup or partition is out of space or the sort work files cannot be created. The fix is to free space on the drive holding the sort temporary files (usually the database's default filegroup or the drive specified in the index build), move the index to a filegroup with adequate space, or reduce the sort size by building the index with fewer resources. First, check free disk space on the drive where the database files and tempdb reside.

What You'll Need

  • SQL Server Management Studio (SSMS) or Azure Data Studio connected to the instance with sysadmin or db_owner rights.
  • Access to the server's file system or the ability to run T-SQL queries against sys.database_files and sys.master_files.
  • Knowledge of the index being built and its target filegroup.
  • Permission to alter database files or create new filegroups (if needed).

Step-by-Step

  1. Identify the failing index and its target filegroup. Run the following query to see which filegroup the index will be placed on (if not specified, it goes to the default filegroup):
    SELECT i.name AS index_name, fg.name AS filegroup_name, f.name AS file_name, f.physical_name, f.size/128.0 AS size_mb, f.growth, f.is_percent_growth
    FROM sys.indexes i
    JOIN sys.filegroups fg ON i.data_space_id = fg.data_space_id
    JOIN sys.database_files f ON f.data_space_id = fg.data_space_id
    WHERE i.name = 'YourIndexName';
    Replace 'YourIndexName' with the actual index name. If the index is being created on a partition scheme, check the partition's filegroup instead.
  2. Check free disk space on the drive(s) hosting the filegroup and tempdb. Use Windows Explorer or a command like dir to see free space. The sort operation requires temporary space roughly equal to the size of the index being built. Ensure at least 1.5 times the estimated index size is free.
  3. If space is low, free up space. Delete unnecessary files, shrink other databases (carefully), or move backups to another drive. Do not shrink the database that is being indexed unless absolutely necessary, as it can cause fragmentation.
  4. If the filegroup is full, add a new data file to it. Use T-SQL:
    ALTER DATABASE YourDatabase
    ADD FILE (NAME = N'NewDataFile', FILENAME = N'C:\Path\NewDataFile.ndf', SIZE = 10GB, FILEGROWTH = 1GB)
    TO FILEGROUP YourFileGroup;
    Adjust path, size, and growth. Ensure the drive has enough space. Alternatively, create a new filegroup on a drive with more space and move the index there.
  5. Consider building the index into a different filegroup with more space. Use the ON clause:
    CREATE INDEX IX_YourIndex ON dbo.YourTable (Column1) ON [NewFileGroup];
    This requires the filegroup to exist and have space.
  6. Reduce the sort size by using SORT_IN_TEMPDB = ON. This moves the sort operation to tempdb, which may have more space. Ensure tempdb has sufficient free space (at least the size of the index).
    CREATE INDEX IX_YourIndex ON dbo.YourTable (Column1) WITH (SORT_IN_TEMPDB = ON);
    If tempdb is on a different drive with more space, this can resolve the error.
  7. If the index build is part of a larger operation (e.g., ALTER TABLE ADD CONSTRAINT), specify the filegroup and sort options. For example:
    ALTER TABLE dbo.YourTable ADD CONSTRAINT PK_YourTable PRIMARY KEY (ID) WITH (SORT_IN_TEMPDB = ON) ON [NewFileGroup];
  8. Retry the index build. After ensuring adequate space and correct filegroup, rerun the original CREATE INDEX or ALTER TABLE statement. Monitor disk space during the operation.

Common Mistakes

  • Ignoring tempdb space. Even if the target filegroup has space, the sort may use tempdb if SORT_IN_TEMPDB is ON or if the index build spills to tempdb. Always check tempdb free space.
  • Shrinking the database unnecessarily. Shrinking can cause fragmentation and may not free enough space. Prefer adding files or moving indexes.
  • Assuming the default filegroup has space. If the index is created without an ON clause, it goes to the default filegroup. Check which filegroup is default and its free space.
  • Forgetting to account for index size growth. The sort operation needs temporary space roughly equal to the final index size. Ensure at least 1.5x free space.
  • Using SORT_IN_TEMPDB without checking tempdb capacity. This can move the problem to tempdb if it's also full.

Tips

  • Estimate index size before building using sp_estimate_data_compression_savings or by checking existing index sizes.
  • Build large indexes during maintenance windows to avoid impacting production.
  • Use ONLINE = ON (Enterprise Edition) to reduce blocking, but note it still requires sort space.
  • Monitor disk space during index builds with Performance Monitor or extended events.
  • Consider partitioning large indexes across multiple filegroups to distribute space requirements.

Troubleshooting

If error 1522 persists after freeing space, check the SQL Server error log for additional details. The error message often includes the file name and drive where the sort failed. Verify that the SQL Server service account has write permissions to the sort directory (usually the default data directory or tempdb location). If using SORT_IN_TEMPDB, ensure tempdb has autogrowth enabled and the drive has space. If the index is being built on a partitioned table, ensure all partitions' filegroups have space. In some cases, the error may be caused by a file size limit (e.g., FAT32 4GB limit) or a disk quota; check the file system. If the index build is part of a transaction that also includes other operations, consider breaking it into smaller steps. Finally, if the issue is due to insufficient memory, increasing the memory grant or using MAXDOP can help, but error 1522 is specifically about disk space.

Frequently Asked Questions

What does error 1522 mean exactly?

Error 1522 indicates that SQL Server could not complete a sort operation because it ran out of space in the target filegroup or could not create sort work files. It is a disk space issue, not a memory issue.

Can I fix error 1522 without adding disk space?

Yes, if you can move the index to a filegroup with more space or use SORT_IN_TEMPDB to leverage tempdb space. Otherwise, you must free up space on the existing drive.

Does SORT_IN_TEMPDB always prevent error 1522?

No, it only helps if tempdb has enough free space. If tempdb is also full, the error will occur there.

How much free space do I need for an index build?

As a rule of thumb, at least 1.5 times the estimated size of the final index. For partitioned indexes, calculate per partition.

Can error 1522 occur during online index rebuilds?

Yes, online index rebuilds still require sort space, either in the user database or tempdb. The same space considerations apply.

What if the error mentions a specific file name?

That file is where the sort tried to write. Check the drive hosting that file for free space and permissions.