Reorganize and Rebuild Indexes in the Database
Reorganize and Rebuild Indexes in the Database
This topic describes how to reorganize or rebuild a fragmented index in SQL Server
2017 by using SQL Server Management Studio or Transact-SQL. The SQL Server
Database Engine automatically maintains indexes whenever insert, update, or delete
operations are made to the underlying data. Over time these modifications can cause
the information in the index to become scattered in the database (fragmented).
Fragmentation exists when indexes have pages in which the logical ordering, based on
the key value, does not match the physical ordering inside the data file. Heavily
fragmented indexes can degrade query performance and cause your application to
respond slowly.
Detecting Fragmentation
The first step in deciding which defragmentation method to use is to analyze the index
to determine the degree of fragmentation. By using the system function
sys.dm_db_index_physical_stats, you can detect fragmentation in a specific index, all
indexes on a table or indexed view, all indexes in a database, or all indexes in all
databases. For partitioned indexes, sys.dm_db_index_physical_stats also provides
fragmentation information for each partition.
Column Description
avg_fragmentation_in_percent The percent of logical fragmentation (out-of-order pages in the index)
fragment_count The number of fragments (physically consecutive leaf pages) in the index
avg_fragment_size_in_pages Average number of pages in one fragment in an index
After the degree of fragmentation is known, use the following table to determine the
best method to correct the fragmentation.
avg_fragmentation_in_percent
Corrective statement
value
> 5% and < = 30% ALTER INDEX REORGANIZE
> 30% ALTER INDEX REBUILD WITH (ONLINE = ON)*
These values provide a rough guideline for determining the point at which you should
switch between ALTER INDEX REORGANIZE and ALTER INDEX REBUILD. However, the
actual values may vary from case to case. It is important that you experiment to
determine the best threshold for your environment. Very low levels of fragmentation
(less than 5 percent) should not be addressed by either of these commands because
the benefit from removing such a small amount of fragmentation is almost always
vastly outweighed by the cost of reorganizing or rebuilding the index.
Indexes with more than 128 extents are rebuilt in two separate phases: logical and physical. In the logical phase, the
existing allocation units used by the index are marked for deallocation, the data rows are copied and sorted, then moved to
new allocation units created to store the rebuilt index. In the physical phase, the allocation units previously marked for
deallocation are physically dropped in short transactions that happen in the background, and do not require many locks.
Index options cannot be specified when reorganizing an index.
The ALTER INDEX REORGANIZE statement requires the data file containing the index to have space available, because the
operation can only allocate temporary work pages on the same file, not another file within the filegroup. So although the
filegroup might have free pages available, the user can still encounter error 1105: "Could not allocate space for object
<index name>.<table name> in database <database name> because the 'PRIMARY' filegroup is full."
Creating and rebuilding nonaligned indexes on a table with more than 1,000 partitions is possible, but is not supported.
Doing so may cause degraded performance or excessive memory consumption during these operations.
Note: Starting with SQL Server 2012, statistics are not created by scanning all
the rows in the table when a partitioned index is created or rebuilt. Instead, the
query optimizer uses the default sampling algorithm to generate statistics. To
obtain statistics on partitioned indexes by scanning all the rows in the table,
use CREATE STATISTICS or UPDATE STATISTICS with the FULLSCAN clause.
Permissions
Requires ALTER permission on the table or view. User must be a member of the
sysadmin fixed server role or the db_ddladmin and db_owner fixed database roles.
1. In Object Explorer, Expand the database that contains the table on which you want to check an index’s fragmentation.
2. Expand the Tables folder.
3. Expand the table on which you want to check an index’s fragmentation.
4. Expand the Indexes folder.
5. Right-click the index of which you want to check the fragmentation and select Properties.
6. Under Select a page, select Fragmentation.
Page fullness
Indicates average fullness of the index pages, as a percentage. 100% means the
index pages are completely full. 50% means that, on average, each index page is
half full.
Total fragmentation
Depth
Forwarded records
The number of records in a heap that have forward pointers to another data
location. (This state occurs during an update, when there is not enough room to
store the new row in the original location.)
Ghost rows
The number of rows that are marked as deleted but not yet removed. These rows
will be removed by a clean-up thread, when the server is not busy. This value does
not include rows that are being retained due to an outstanding snapshot isolation
transaction.
Index type
The type of index. Possible values are Clustered index, Nonclustered index, and
Primary XML. Tables can also be stored as a heap (without indexes), but then this
Index Properties page cannot be opened.
Leaf-level rows
Pages
Partition ID
The number of ghost records that are being retained due to an outstanding
snapshot isolation transaction.
Transact-SQL
USE AdventureWorks2012;
GO
-- Find the average fragmentation percentage of all indexes
-- in the HumanResources.Employee table.
SELECT a.index_id, name, avg_fragmentation_in_percent
FROM sys.dm_db_index_physical_stats (DB_ID(N'AdventureWorks2012'),
OBJECT_ID(N'HumanResources.Employee'), NULL, NULL, NULL) AS a
JOIN sys.indexes AS b
ON a.object_id = b.object_id AND a.index_id = b.index_id;
GO
The statement above might return a result set similar to the following:
6 row(s) affected)
1. In Object Explorer, Expand the database that contains the table on which you want to reorganize an index.
2. Expand the Tables folder.
3. Expand the table on which you want to reorganize an index.
4. Expand the Indexes folder.
5. Right-click the index you want to reorganize and select Reorganize
Reorganize..
6. In the Reorganize Indexes dialog box, verify that the correct index is in the Indexes to be reorganized grid and click OK
OK..
7. Select the Compact large object column data check box to specify that all pages that contain large object (LOB) data are
also compacted.
8. Click OK
OK..
Transact-SQL
1. In Object Explorer,
Explorer, connect to an instance of Database Engine.
2. On the Standard bar, click New Query.
Query.
3. Copy and paste the following example into the query window and click Execute
Execute..
USE AdventureWorks2012;
GO
-- Reorganize the IX_Employee_OrganizationalLevel_OrganizationalNode
-- index on the HumanResources.Employee table.
1. In Object Explorer, Expand the database that contains the table on which you want to reorganize an index.
2. Expand the Tables folder.
3. Expand the table on which you want to reorganize the indexes.
4. Right-click the Indexes folder and select Reorganize All.
All.
5. In the Reorganize Indexes dialog box, verify that the correct indexes are in the Indexes to be reorganized.
reorganized. To remove an
index from the Indexes to be reorganized grid, select the index and then press the Delete key.
6. Select the Compact large object column data check box to specify that all pages that contain large object (LOB) data are
also compacted.
7. Click OK
OK..
Transact SQL
1. In Object Explorer,
Explorer, connect to an instance of Database Engine.
2. On the Standard bar, click New Query.
Query.
3. Copy and paste the following example into the query window and click Execute
Execute..
USE AdventureWorks2012;
GO
-- Reorganize all indexes on the HumanResources.Employee table.
ALTER INDEX ALL ON HumanResources.Employee
REORGANIZE ;
GO
Rebuild an index
1. In Object Explorer, Expand the database that contains the table on which you want to reorganize an index.
2. Expand the Tables folder.
3. Expand the table on which you want to reorganize an index.
4. Expand the Indexes folder.
5. Right-click the index you want to reorganize and select Rebuild
Rebuild..
6. In the Rebuild Indexes dialog box, verify that the correct index is in the Indexes to be rebuilt grid and click OK
OK..
7. Select the Compact large object column data check box to specify that all pages that contain large object (LOB) data are
also compacted.
8. Click OK.
Transact-SQL
USE AdventureWorks2012;
GO
ALTER INDEX PK_Employee_BusinessEntityID ON HumanResources.Employee
REBUILD;
GO
Transact-SQL
USE AdventureWorks2012;
GO
ALTER INDEX ALL ON Production.Product
REBUILD WITH (FILLFACTOR = 80, SORT_IN_TEMPDB = ON,
STATISTICS_NORECOMPUTE = ON);
GO