Thursday, 27 February 2014

Sql server rebuild index

When rebuild a index sql server lock the table to prevent database to any modification. Prevent this situation you can use "ONLINE" option. Index rebuild in three step. I am mention below

1. Preparation phase.
2. Build   
3. Final 
In  first phase collect all system metadata to create new structure of empty index. It is snapshot of table and row to provide transaction level read consistency and blocked all DML operation for short time.

Second phase all database record is scanned , merged, sorted and inserted into table.

And final phase, all uncommitted update transactions must be completed and all data modification is blocked for very short time until this is completed and system metadata is update and replace source object by the target.

You can use ALTER statement for rebuilding, but you need to pass index name one by one. So you can use below statement for update all indexes at a time

DECLARE @Database VARCHAR(255)   
DECLARE @Table VARCHAR(255)  
DECLARE @cmd NVARCHAR(500)  
DECLARE @fillfactor INT 

SET @fillfactor = 90 

DECLARE DatabaseCursor CURSOR FOR  
SELECT name FROM MASTER.dbo.sysdatabases   
WHERE name = 'TestTable'
ORDER BY 1  

OPEN DatabaseCursor  

FETCH NEXT FROM DatabaseCursor INTO @Database  
WHILE @@FETCH_STATUS = 0  
BEGIN  

   SET @cmd = 'DECLARE TableCursor CURSOR FOR SELECT ''['' + table_catalog + ''].['' + table_schema + ''].['' + 
  table_name + '']'' as tableName FROM ' + @Database + '.INFORMATION_SCHEMA.TABLES 
  WHERE table_type = ''BASE TABLE'''   

   -- create table cursor  
   EXEC (@cmd)  
   OPEN TableCursor   

   FETCH NEXT FROM TableCursor INTO @Table   
   WHILE @@FETCH_STATUS = 0   
   BEGIN   

    IF (@@MICROSOFTVERSION / POWER(2, 24) >= 9)
       BEGIN
           -- SQL 2005 or higher command 
           SET @cmd = 'ALTER INDEX ALL ON ' + @Table + ' REBUILD WITH (FILLFACTOR = ' +CONVERT(VARCHAR(3),@fillfactor) + ')' 
           EXEC (@cmd) 
       END
    ELSE
    BEGIN
          -- SQL 2000 command 
          DBCC DBREINDEX(@Table,' ',@fillfactor)  
       END

    FETCH NEXT FROM TableCursor INTO @Table   
   END   

   CLOSE TableCursor   
   DEALLOCATE TableCursor  

   FETCH NEXT FROM DatabaseCursor INTO @Database  
END  
CLOSE DatabaseCursor   

DEALLOCATE DatabaseCursor

Check the result after index rebuild








No comments:

Post a Comment