sql server View index fragmentation size, and periodically rebuild the index

 

View fragmentation function to use dbcc showcontig

Code:

Copy the code
- into the current library
 use DB_Name - create a variable you want to view the table DECLARE @table_id int the SET @table_id = object_id ( ' TableName ' ) - execute dbcc SHOWCONTIG ( @table_id)
Copy the code

Return result:

These examples are relatively large table .. a real physical space occupied by the table 24 there are G (index data 20G + 2G +) ..

The following is a Glossary:

DBCC SHOWCONTIG is a fragment of information data and indexes of the specified table.

Explained as follows:

Page Scanned- of scanned pages: If you know the approximate size of rows and number of rows in the table or in the index, then you can estimate the number of pages in the index. Look at the number of pages scanned, significantly higher than the number of pages if your estimate, indicating the presence of internal fragmentation. 

Extents number of extents Scanned- scanning: 8 divided by the number of pages scanned, rounded to the next highest value. This value should be returned and the number of scanning DBCC SHOWCONTIG same extent. If the number of DBCC SHOWCONTIG returned high, indicating the presence of external fragmentation. How much depends on the severity of the debris just displayed value is higher than the estimated value. 

Extent number of switches Switches- extents: This number should be equal to the number of scan expansion extents minus 1. Then there is high external fragmentation. 

Avg. Pages per Extent-每个扩展盘区上的平均页数:该数是扫描页数除以扫描扩展盘区数,一般是8。小于8说明有外部碎片。 

Scan Density [Best Count:Actual Count]-扫描密度[最佳值:实际值]:DBCC SHOWCONTIG返回最有用的一个百分比。这是扩展盘区的最佳值和实际值的比率。该百分比应该尽可能靠近100%。低了则说明有外部碎片。

Logical Scan Fragmentation-逻辑扫描碎片:无序页的百分比。该百分比应该在0%到10%之间,高了则说明有外部碎片。 

Extent Scan Fragmentation-扩展盘区扫描碎片:无序扩展盘区在扫描索引叶级页中所占的百分比。该百分比应该是0%,高了则说明有外部碎片。 

Avg. Bytes Free per Page-每页上的平均可用字节数:所扫描的页上的平均可用字节数。越高说明有内部碎片,不过在你用这个数字决定是否有内部碎片之前,应该考虑fill factor(填充因子)。 

Avg. Page Density (full)-平均页密度(完整):每页上的平均可用字节数的百分比的相反数。低的百分比说明有内部碎片

 

通过sql server 代理创建定时任务定期来重建索引

 

  1.  
    DECLARE @TABLENAME NVARCHAR(MAX)
  2.  
    IF(CURSOR_STATUS('global','TABLE_CURSOR')<>'-3')
  3.  
    BEGIN
  4.  
    CLOSE TABLE_CURSOR
  5.  
    DEALLOCATE TABLE_CURSOR
  6.  
    END
  7.  
    DECLARE TABLE_CURSOR CURSOR FOR SELECT NAME FROM SYS.objects WHERE TYPE = 'U' AND name LIKE 'T_HIS%' ORDER BY modify_date DESC
  8.  
    OPEN TABLE_CURSOR
  9.  
    FETCH NEXT FROM TABLE_CURSOR INTO @TABLENAME
  10.  
    WHILE (@@FETCH_STATUS = 0)
  11.  
    BEGIN
  12.  
    dbcc dbreindex (@TABLENAME, '',80)
  13.  
    FETCH NEXT FROM TABLE_CURSOR INTO @TABLENAME
  14.  
    END
  15.  
    CLOSE TABLE_CURSOR
  16.  
    DEALLOCATE TABLE_CURSOR

 

 dbcc dbreindex ([customer],'',90)

The first parameter is the name of the table to rebuild the index, and the second parameter specifies the index name, it means all empty, the third parameter called the fill factor is the degree of data populate the index page of 0 indicates the previous value, It indicates that each index page 100 are filled, then the highest query efficiency, but will move the other index index is inserted, may be set according to actual situation.

Guess you like

Origin www.cnblogs.com/bluedy1229/p/11647379.html