Rebuild / reorganize de índice
SQL ServerManutençãoComando
-- Rebuild (high fragmentation > 30%). ONLINE requires Enterprise:
ALTER INDEX IX_Customer_Email ON dbo.Customer
REBUILD WITH (ONLINE = ON, MAXDOP = 4, SORT_IN_TEMPDB = ON);
-- All indexes of a table:
ALTER INDEX ALL ON dbo.Customer REBUILD WITH (FILLFACTOR = 90);
-- Reorganize (moderate fragmentation 5-30%, always online, can be stopped):
ALTER INDEX IX_Customer_Email ON dbo.Customer REORGANIZE;O que faz
REBUILD recria o índice do zero (elimina a fragmentação por completo, atualiza as estatísticas); REORGANIZE compacta o nível folha no lugar (leve, sem bloqueio).
Quando usar
REORGANIZE para fragmentação de 5-30%, REBUILD acima de 30%. Em janelas de manutenção para tabelas grandes.
Atenção
ONLINE = ON só está disponível no Enterprise/Developer; use OFF no Standard (a tabela fica bloqueada). REBUILD gera muito log e precisa de espaço. MAXDOP e SORT_IN_TEMPDB afetam o uso de recursos.