Wednesday, June 12, 2013

Rebuild Vs Reorganize Index (and Update Statistics)

Does rebuilding the clustered index rebuild all the other indexes on the table? What happens to the statistics on that column?

I decided to create a table and rebuild it to see what happened to the index.Here is what I did.

  • Create Test table,create index and insert records

DROP TABLE [dbo].[Index_table]
GO
CREATE TABLE [dbo].[Index_table](
[COL1] [int] NOT NULL,
[COL2] [int] NOT NULL,
[COL3] [int] NOT NULL
)
GO
CREATE CLUSTERED INDEX   [CIX]     ON [dbo].[Index_table](COL1)
CREATE NONCLUSTERED INDEX  [NCIX1]   ON [dbo].[Index_table](COL2)
CREATE NONCLUSTERED INDEX  [NCIX2]   ON [dbo].[Index_table](COL3)
GO
SET NOCOUNT ON
GO
Insert into Index_table
SELECT ROUND(RAND() * 100,4,0) ,ROUND(RAND() * 100,3,0),ROUND(RAND() * 100,2,0)
GO 50
DROP VIEW [dbo].[Index_table_stats]
GO


  • Create a view to query the DMVs after each operation

CREATE View Index_table_stats
as
select OBJECT_NAME(ius.object_id) as 'Table Name',idx.name as 'Index Name',last_system_scan  as 'Rebuild Time',system_scans as 'Rebuild count',idx.type_desc,STATS_DATE(ius.OBJECT_ID, ius.index_id) AS 'Stats Updated'
FROM sys.dm_db_index_usage_stats ius join sys.indexes idx
ON OBJECT_NAME(ius.object_id) = OBJECT_NAME(idx.object_id) and ius.index_id = idx.index_id
WHERE OBJECT_NAME(ius.object_id) = 'Index_table'


  • Run the below queries and to check the index properties and rebuild the column


select * from Index_table_stats order by type_desc
ALTER INDEX CIX on dbo.Index_table REBUILD
select * from Index_table_stats order by type_desc


As seen from the result,rebuilding the clustered index just rebuilds that specific index as indicated by the "Rebuild Time" column.Every rebuild also updates the statistics for that column with a fullscan.
I did notice one weird behavior in SQL Server 2008 R2. When the index is rebuilt the very first time,the "Rebuild count" is displayed as 2.I am not sure why the index is rebuilt twice.

Does rebuilding the non clustered index rebuild just that index?What happens to the statistics on that column?

ALTER INDEX NCIX1 on dbo.Index_table REBUILD
ALTER INDEX NCIX2 on dbo.Index_table REBUILD
select * from Index_table_stats order by type_desc


As evident from the result,for every index that is rebuilt,statistics are also updated with a fullscan

Does the Rebuild ALL syntax rebuild all indexes? 

ALTER INDEX ALL on dbo.Index_table REBUILD
select * from Index_table_stats order by type_desc


Rebuild All would rebuild and update statistics with fullscan all the indexes on that table

Does reorganizing a clustered or non clustered index reorganize the rest of the indexes? What happens to the statistics on that column?

It would be easier if you drop and recreate the table and reorg the index as you could see null values,though this step is not mandatory.

ALTER INDEX CIX on dbo.Index_table REORGANIZE
ALTER INDEX NCIX1 on dbo.Index_table REORGANIZE
ALTER INDEX NCIX2 on dbo.Index_table REORGANIZE
ALTER INDEX ALL on dbo.Index_table REORGANIZE
select * from Index_table_stats order by type_desc


REORGANIZE affects just the index on which the operation is performed (BOL).I was not able to find a DMV\column that dispalys the time REORGANIZE was last performed.
Unlike rebuild,reorg does NOT update statistics on any of the indexes as evident from the "Stats Updated" column.

No comments:

Post a Comment