Guest User

Desfragmentar índices

a guest
Dec 28th, 2016
331
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*Perform a 'USE <database name>' to select the database in which to run the script.*/
  2. -- Declare variables
  3. SET NOCOUNT ON;
  4. DECLARE @tablename VARCHAR(255);
  5. DECLARE @execstr   VARCHAR(400);
  6. DECLARE @objectid  INT;
  7. DECLARE @indexid   INT;
  8. DECLARE @frag      DECIMAL;
  9. DECLARE @maxfrag   DECIMAL;
  10.  
  11. -- Decide on the maximum fragmentation to allow for.
  12. SELECT @maxfrag = 10.0;
  13.  
  14. -- Declare a cursor.
  15. DECLARE TABLES CURSOR FOR
  16.    SELECT TABLE_SCHEMA + '.' + TABLE_NAME
  17.    FROM INFORMATION_SCHEMA.TABLES
  18.    WHERE TABLE_TYPE = 'BASE TABLE';
  19.  
  20. -- Create the table.
  21. CREATE TABLE #fraglist (
  22.    ObjectName CHAR(255),
  23.    ObjectId INT,
  24.    IndexName CHAR(255),
  25.    IndexId INT,
  26.    Lvl INT,
  27.    CountPages INT,
  28.    CountRows INT,
  29.    MinRecSize INT,
  30.    MaxRecSize INT,
  31.    AvgRecSize INT,
  32.    ForRecCount INT,
  33.    Extents INT,
  34.    ExtentSwitches INT,
  35.    AvgFreeBytes INT,
  36.    AvgPageDensity INT,
  37.    ScanDensity DECIMAL,
  38.    BestCount INT,
  39.    ActualCount INT,
  40.    LogicalFrag DECIMAL,
  41.    ExtentFrag DECIMAL);
  42.  
  43. -- Open the cursor.
  44. OPEN TABLES;
  45.  
  46. -- Loop through all the tables in the database.
  47. FETCH NEXT
  48.    FROM TABLES
  49.    INTO @tablename;
  50.  
  51. WHILE @@FETCH_STATUS = 0
  52. BEGIN
  53. -- Do the showcontig of all indexes of the table
  54.    INSERT INTO #fraglist
  55.    EXEC ('DBCC SHOWCONTIG (''' + @tablename + ''')
  56.      WITH FAST, TABLERESULTS, ALL_INDEXES, NO_INFOMSGS');
  57.    FETCH NEXT
  58.       FROM TABLES
  59.       INTO @tablename;
  60. END;
  61.  
  62. -- Close and deallocate the cursor.
  63. CLOSE TABLES;
  64. DEALLOCATE TABLES;
  65.  
  66. -- Declare the cursor for the list of indexes to be defragged.
  67. DECLARE indexes CURSOR FOR
  68.    SELECT ObjectName, ObjectId, IndexId, LogicalFrag
  69.    FROM #fraglist
  70.    WHERE LogicalFrag >= @maxfrag
  71.       AND INDEXPROPERTY (ObjectId, IndexName, 'IndexDepth') > 0;
  72.  
  73. -- Open the cursor.
  74. OPEN indexes;
  75.  
  76. -- Loop through the indexes.
  77. FETCH NEXT
  78.    FROM indexes
  79.    INTO @tablename, @objectid, @indexid, @frag;
  80.  
  81. WHILE @@FETCH_STATUS = 0
  82. BEGIN
  83.    PRINT 'Executing DBCC INDEXDEFRAG (0, ' + RTRIM(@tablename) + ',' + RTRIM(@indexid) + ') - fragmentation currently ' + RTRIM(CONVERT(VARCHAR(15),@frag)) + '%';
  84.    SELECT @execstr = 'DBCC INDEXDEFRAG (0, ' + RTRIM(@objectid) + ', ' + RTRIM(@indexid) + ')';
  85.    EXEC (@execstr);
  86.  
  87.    FETCH NEXT
  88.       FROM indexes
  89.       INTO @tablename, @objectid, @indexid, @frag;
  90. END;
  91.  
  92. -- Close and deallocate the cursor.
  93. CLOSE indexes;
  94. DEALLOCATE indexes;
  95.  
  96. -- Delete the temporary table.
  97. DROP TABLE #fraglist;
  98. GO
RAW Paste Data