Entrez les instructions SQL suivantes dans la fenêtre de la console:
SET NOCOUNT ON-
DECLAREtablename varchar (255) -
DECLAREexecstr varchar (400) -
DECLAREobjectid int
DECLAREindexid int
DECLAREfrag decimal-
DECLAREmaxfrag décimal;
maxfrag SELECT = 30,0;
DECLARE tables CURSOR FOR
TABLE_SCHEMA SELECT + '.' + TABLE_NAME
DE INFORMATION_SCHEMA.TABLES
Où base de table 'TABLE_TYPE =;
CREATE TABLE #fraglist (
ObjectName char (255),
ObjectId int,
IndexName char (255),
IndexId int,
Lvl int,
CountPages int,
CountRows int,
MinRecSize int,
MaxRecSize int,
AvgRecSize int,
ForRecCount int,
Étendues int,
ExtentSwitches int,
AvgFreeBytes int,
AvgPageDensity int,
ScanDensity décimal,
BestCount int,
ActualCount int,
LogicalFrag décimal,
ExtentFrag décimal);
Tables ouvertes;
FETCH PROCHAINE
À partir de tables
ENtablename;
PENDANT@@FETCH_STATUS = 0
COMMENCER
INSERT INTO #fraglist
EXEC ('DBCC SHOWCONTIG (' '' +tablename + '' ')
AVEC FAST, TABLERESULTS, ALL_INDEXES, NO_INFOMSGS ') -
FETCH PROCHAINE
À partir de tables
EN @ tablename-
FIN;
Tableaux- FERMER
DEALLOCATE tables;
DECLARE indices CURSOR FOR
SELECT ObjectName, ObjectId, IndexId, LogicalFrag
DE #fraglist
OÙ LogicalFrag> =maxfrag
ET INDEXPROPERTY (ObjectId, IndexName, 'IndexDepth')> 0-
Indices ouverte;
FETCH PROCHAINE
À partir des indices
ENtablename,objectid,indexid,frag;
PENDANT@@FETCH_STATUS = 0
COMMENCER
PRINT 'exécution DBCC INDEXDEFRAG (0,' + RTRIM (tablename) + ',
'+ RTRIM (indexid) +') - la fragmentation actuellement '
+ RTRIM (convert (varchar (15), @ frag)) + '%' -
execstr SELECT = 'DBCC INDEXDEFRAG (0,' + RTRIM (objectid) + ',
'+ RTRIM (indexid) +') -
EXEC (execstr);
FETCH PROCHAINE
À partir des indices
ENtablename,objectid,indexid, @ fragmentation
FIN;
Indexes- FERMER
DEALLOCATE indices;
DROP TABLE # fraglist-
GO