Advertisement
Guest User

Script para melhoramneto do desempenho

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