File Repair
Database Recovery with Piecemeal Restore in Microsoft SQL Server
July 28, 2026| 7 min read
Summary: SQL Server index fragmentation slows database performance by increasing logical reads and inefficient page access. Reorganize indexes for 5–30% fragmentation and rebuild indexes when fragmentation exceeds 30% to improve query performance and database efficiency. This article explains 2 index maintenance methods—Reorganize and Rebuild, the differences between them, and when and how each method should be used in SQL Server. You'll also learn step-by-step ways to repair corrupt indexes without data loss.
Free Download 100% SecureIndexes are important schema objects of MS SQL database files that enhance the data retrieving operations on a table. In simple words, these objects function like a table of contents of the database. These indexes have B-tree structure (where the letter “B” stands for balanced), which is a multilevel structure that has root, intermediate, and leaf levels. At the leaf level, a "doubly linked list" links the pages. This link is used to scan an index.
There are two types of indexes in SQL Server – clustered and non-clustered.
A clustered index: physically stores the data within the table, defining the logical order of table. A non-clustered index: stores data separately from the table. It has a similar B-tree structure but points to the clustered index for its data.
With time, continuous INSERT, UPDATE, and DELETE operations can fragment the database tables. When fragmentation increases, query performance degrades, and data concurrency and locks increase. Also, the server may choose not to use a fragmented index. Indexes are also prone to corruption, which can generate integrity errors.
Indexes are also prone to corruption. If the indexes get corrupted, you can receive errors. Some common errors are:
Table error: table 'Items' (ID 1954106002). Data row does not have a matching index row in the index 'IX_Advertise_BrandCopy_Price_Stock' (ID 69). Possible missing or invalid keys for the index row matching.
Msg 8955, Level 16, State 1, Line 1
Data row (1:11226494:9) identified by (Id = 11078215) with index values 'AdvertiseFlag = 1 and BrandCopyParentId = 0 and NSFPPrice = 137.50 and NSFPQtyInStock = 0 and Id = 11078215'.
Msg 8951, Level 16, State 1, Line 1
In case of high fragmentation or corruption in indexes, you can reorganize or rebuild the indexes to resolve the issue. In this post, let's see how to reorganize indexes and rebuild indexes in SQL Server, and also understand more about fragmentation.
Fragmentation is a natural phenomenon that happens in a database over time, when rows are continuously inserted, updated, and deleted. It is defined as a condition when data pages are not stored contiguously. In a B-tree index, fragmentation usually occurs when pages, where the logical ordering (within the index, based on the key values of the index), do not match the physical ordering of the index pages.
Example: When rows are added to a table page without enough space, SQL Server automatically performs a page split. It moves some data to a new page to make room. This process changes the indexes, which directly impacts the data continuity and order. However, SQL Server updates the pointer on the both pages (original and new) to maintain the logical sorting order in the index. But it still leads to fragmentation.
There are two types of index fragmentation:
Logical Fragmentation - When the logical order of the pages does not match the physical order. From a performance standpoint, logical fragmentation leads to an increase in the number of physical reads.
Internal Fragmentation - When data pages in the index contain free space. Internal fragmentation leads to an increase in logical reads.
Page Density (Page Fullness) -Page density shows how full each index page is. When it’s low, it means index pages contain too much free space.
Unlike external fragmentation, low page density problems are not fully resolved by index reorganization alone and may require an index rebuild.
You can identify the degree of fragmentation in a specific index or all indexes defined on a table or in a database using the Dynamic Management Function (DMF) - “sys.dm_db_index_physical_stats.”
SELECT
DB_NAME(ips.database_id) AS DatabaseName,
OBJECT_NAME(ips.object_id) AS TableName,
i.name AS IndexName,
ips.index_type_desc,
ips.avg_fragmentation_in_percent,
ips.page_count
FROM sys.dm_db_index_physical_stats
(
DB_ID(),
OBJECT_ID('EmployeeRecords'),
NULL,
NULL,
'DETAILED'
) AS ips
INNER JOIN sys.indexes AS i
ON ips.object_id = i.object_id
AND ips.index_id = i.index_id
ORDER BY ips.avg_fragmentation_in_percent DESC;
GO
To determine index Fragmentation by SSMS Graphical user interface, here are the steps:
According to Microsoft, if the fragmentation is less than 5%, there is no need for defragmentation. If the fragmentation is between 5% and 30%, you should reorganize the indexes. If the fragmentation is greater than 30%, you need to rebuild the indexes.
After getting the fragmentation percentage, you can choose the best method to defragment the indexes. According to Microsoft, ALTER INDEX REORGANIZE is generally preferred over REBUILD unless a rebuild is specifically required because reorganize operations are lightweight, online, and consume fewer resources.
You can rebuild/reorganize indexes using the SQL commands, SSMS, and maintenance plans. Let’s discuss these in detail.
Reorganizing the index improves its structure by moving the pages into a more efficient search order. It also helps the index rows to deallocate unused pages. Swap the remaining pages in small transactions until all the pages are in logical order. The result will create logically ordered pages instead of physically contiguous pages. To reorganize the indexes, you can follow the below cited steps:
You can reorganize the index using the below T-SQL command.
ALTER INDEX IX_EmployeeName
ON EmployeeRecords
REORGANIZE;
GO
In the above command, EmployeeRecords is the table, with the index - IX_EmployeeName
To reorganize ALL INDEXES, run this command:
ALTER INDEX ALL
ON EmployeeRecords
REORGANIZE;
GO
It does not update statistic automatically, so after reorganizing indexes, you have to manually do this, by following command:
UPDATE STATISTICS EmployeeRecords;
GO
You can also reorganize the indexes using the SQL Server Management Studio (SSMS). Follow the below cited steps:
You can also reorganize the index in a maintenance plan. SQL Server offers an option to schedule a maintenance plan that periodically carries out this activity. The maintenance plan will not only defragment the index but also allow you to perform other important operations, such as statistic maintenance and database backup.
The process of rebuilding indexes is different in comparison to reorganizing the indexes. This process builds a new index from scratch and drops the old one. This happens regardless of the fragmentation present in the older index. It requires enough space to build a new index.
You can perform rebuilding of indexes online or offline. The terms Online and Offline indicate whether the index can be kept accessible during the rebuild operation. The Standard version of MS SQL Server supports only the Offline type, while the Enterprise and Developer versions also support the Online type. Let’s see the ways to rebuild the indexes in SQL Server.
If the fragmentation is higher, then you can rebuild the indexes. You can use the DBCC DBREINDEX command to rebuild an index. Here’s how to execute this command:
DBCC DBREINDEX
(
table_name
[ , index_name [ , fillfactor ] ]
)
[ WITH NO_INFOMSGS ]
This command does not support spatial indexes and memory-optimized column store indexes.
DBCC DBREINDEX is deprecated in newer SQL Server versions. Microsoft recommends using ALTER INDEX ... REBUILD instead. Use ALTER INDEX to refresh your index. Example:
ALTER INDEX [PK_Person_BusinessEntityID] ON [Person].[Person]
REBUILD;
Command to Rebuild Index Online to Avoid Downtime
ALTER INDEX [IndexName]
ON [Schema].[Table]
REBUILD WITH (ONLINE = ON);
GO
It allow you continue accessing the table during the rebuild operation. However, availability depends on the SQL Server edition and index type.
To Rebuild All indexes in database use this command:
ALTER INDEX ALL
ON TableName
REBUILD;
GO
You can use the graphical user interface of the SQL Server Management Studio to rebuild the indexes. Here are the steps:
You can also rebuild the index in a maintenance plan. To do so, follow the below steps:
If indexes are corrupted and reorganizing or rebuilding did not help, follow these repair methods:
You can try the DBCC CHECKDB command with 'REPAIR_ALLOW_DATA_LOSS' option to repair the database. Here’s how to use this command:
First, change the mode of the database to SINGLE_USER by using the below command:
Next, run the DBCC CHECKDB command as given below to repair the database:
DBCC CHECKDB (N ’Entity’, REPAIR_ALLOW_DATA_LOSS) WITH ALL_ERRORMSGS, NO_INFOMSGS;
GO
After repair, set the database mode to MULTI_USER by executing the below command:
The above DBCC CHECKDB command can help you resolve minor corruption issues in clustered and non-clustered indexes in SQL database. But it can result in data loss.
If the backup file is available and the indexes in MDF/NDF file is severely corrupted then you can restore them from bak file. It is safe method then DBCC CHECKDB.
If the backup file is not available, you can use Stellar Repair for MS SQL - professional MS SQL repair tool. It can quickly repair database (both MDF and NDF files) with complete integrity and precision. It can recover all the objects, including clustered and non-clustered indexes, triggers, pages, stored procedures, etc., from the corrupt database. It allows you to save all the data recovered from the database to a new SQL database file and in various other formats. It can help you resolve index corruption-related errors in SQL database.
Here are the best practices you should follow while maintaining indexes in SQL Server:
Reorganizing or rebuilding the indexes helps you reduce fragmentation and resolve corruption. In this article, we have explained how to check the fragmentation percentage in indexes and how to rebuild the indexes. If, while or after the defragmenting process, your database gets corrupted and returns errors related to index corruption, then you can use Stellar Repair for MS SQL . This professional tool can repair and restore corrupt indexes from the database with complete precision. The tool can repair databases created in the latest SQL Server 2025 and lower versions. It also helps to restore deleted objects.
SQL Fill factor controls how much space SQL Server leaves free in each index page during rebuild. It helps reduce page splits and fragmentation.