Una frammentazione del database si verifica quando l'ordine logico del l'indice del database non corrisponde l'allocazione fisica dei dati sul disco . Come i cambiamenti di dati all'interno del database , le tabelle di indice del database diventano sparsi in tutto il disco. Aumenta il tempo necessario per accedere ai dati , con conseguente riduzione delle prestazioni delle applicazioni . I comandi della console di database MSSQL consentono di ricostruire o deframmentare gli indici . È necessario utilizzare l'istruzione DBCC DBEREINDEX di deframmentare un indice di database MSSQL . Istruzioni 
 1 
 Fare clic sul pulsante "Start " di Windows e selezionare " Tutti i programmi ". Clicca per aprire The Sims 2 
 Inserire le seguenti istruzioni SQL nella finestra della console " Console di gestione di SQL . " : 
 
 SET NOCOUNT ON ; 
 
 DECLARE @ nometabella varchar ( 255) ; 
 
 DECLARE @ execstr varchar ( 400 ) ; 
 
 DECLARE @ objectid int ; 
 
 DECLARE @ IndexID int ; 
 
 DECLARE @ frag decimale ; 
 
 DECLARE @ maxfrag decimale ; 
 
 SELECT @ maxfrag = 30.0 ; ' . ' 
 
 DICHIARARE tabelle CURSOR FOR 
 
 SELECT + TABLE_SCHEMA + NOME_TABELLA 
 
 DA INFORMATION_SCHEMA.TABLES 
 
 DOVE TABLE_TYPE = ' tabella di base' ; 
 
 CREATE TABLE # FragList ( 
 
 NomeOggetto char ( 255 ) , < br 
 ObjectId > int , Stati 
 IndexName char ( 255 ) , Stati 
 IndexID int , Stati 
 Lvl int , brevi 
 CountPages int , Stati 
 countRows int , Stati 
 MinRecSize int , Stati 
 MaxRecSize int , Stati 
 AvgRecSize int , Stati 
 ForRecCount int , brevi 
 Estensioni int , brevi 
 ExtentSwitches int , brevi 
 AvgFreeBytes int , Stati 
 AvgPageDensity int , Stati 
 ScanDensity decimale , Stati 
 BestCount int , Stati < p > ActualCount int , Stati 
 LogicalFrag decimale , Stati 
 ExtentFrag decimale) ; 
 
 tavoli aperti ; 
 
 ANDARE A PRENDERE DOPO 
 
 dalle tabelle 
 
 INTO @ nome_tabella ; 
 
 MENTRE @ @ FETCH_STATUS = 0 
 
 BEGIN 
 
 INSERT INTO # FragList 
 
 EXEC ( ' DBCC SHOWCONTIG ( '' ' + @ nometabella + ''' ) per 
 CON FAST, TABLERESULTS , ALL_INDEXES , NO_INFOMSGS '); 
 
 ANDARE A PRENDERE DOPO 
 
 dalle tabelle 
 
 INTO @ nometabella ; 
 
 END; 
 
 tavoli stretti ; 
 
 DEALLOCATE tabelle ; 
 
 DICHIARARE indici del cursore per i 
 
 Seleziona NomeOggetto , ObjectId , IndexID , LogicalFrag 
 
 FROM # FragList 
 
 DOVE LogicalFrag > = @ maxfrag 
 
 E INDEXPROPERTY ( ObjectId , IndexName , ' IndexDepth ') > 0 ; 
 
 indici aperti; 
 
 ANDARE A PRENDERE DOPO 
 
 da indici 
 
 INTO nometabella @ , @ objectid , IndexID @ , @ frag ; 
 
 MENTRE @ @ FETCH_STATUS = 0 
 
 BEGIN 
 
 PRINT ' esecuzione di DBCC INDEXDEFRAG ( 0 , ' + RTRIM ( @ nometabella ) + ' , Stati 
 ' + RTRIM ( @ IndexID ) + ' ) - frammentazione attualmente '
 
 + RTRIM ( CONVERTITO ( varchar ( 15 ) , @ frag ) ) + '% '; 
 
 SELECT @ execstr =' DBCC INDEXDEFRAG ( 0 , '+ RTRIM ( @ ObjectID ) + ' , Stati 
 ' + RTRIM ( @ IndexID ) + ' )'; 
 
 EXEC ( @ execstr ) ; 
 
 ANDARE A PRENDERE DOPO 
 
 indici 
 
 INTO nometabella @ , @ objectid , IndexID @ , @ frag ; 
 
 END; 
 indici CHIUDI 
 ; 
 
 DEALLOCATE indici ; 
 < p > DROP TABLE # FragList ; . 
 
 GO 
 3 
 Fare clic su " File" e selezionare "Esegui " per deframmentare l'indice del database MSSQL