Rebuild indexes on Filegroup

Vijay Kumar 2,036 Reputation points
2021-03-17T00:37:30.39+00:00

How to rebuild all indexes on all tables in single file group in sql server 2016?

Developer technologies | Transact-SQL
SQL Server | Other
{count} votes

Accepted answer
  1. CathyJi-MSFT 22,396 Reputation points Microsoft External Staff
    2021-03-17T02:42:12.667+00:00

    Hi @Vijay Kumar ,

    Try below T-SQL;

    SELECT  
      
      sd.name AS FileGroupName,  
      
      st.name AS TableName,  
      
      si.name As IndexName,  
      
      sd.name data_space_name,  
      
      'DBCC DBREINDEX("' + SCHEMA_NAME(st.schema_id) + '.' + st.name + '", ' + QUOTENAME(si.name) + ')' AS ReIndexCommand  
      
    FROM sys.indexes si  
      
    INNER JOIN sys.tables st ON si.object_id = st.object_id  
      
    INNER JOIN sys.data_spaces sd ON si.data_space_id = sd.data_space_id  
      
       WHERE sd.name = 'filegroupname'  
    

    Quote from this similar thread REBUILD THE INDEXES BY FILE GROUP .


    If the response is helpful, please click "Accept Answer" and upvote it, thank you.

    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.