Ver Mensaje Individual
  #1 (permalink)  
Antiguo 13/04/2012, 12:39
espher
 
Fecha de Ingreso: abril-2008
Ubicación: Chihuahua Mexico
Mensajes: 166
Antigüedad: 15 años, 10 meses
Puntos: 15
Aporte reindexar y desfragmentar indices en SQL

Hola hola aqui dejo un aporte este mismo es para reindexar y desfragmentar indices de forma facil dentro de sql solo hagan copiar y pegar y ase su trabajo por si mismo.

Este Procesa todas las tablas de la base de datos actual osea deonde se corre esta tarea.

Código SQL:
Ver original
  1. --Borramos la tabla de LOG
  2. IF EXISTS (SELECT * FROM sysobjects WHERE name = 'fraglist' AND TYPE = 'U')
  3. BEGIN
  4.     DROP TABLE fraglist
  5. END
  6. GO
  7.  
  8. --Declaramos las variables
  9. SET NOCOUNT ON
  10. DECLARE @tablename VARCHAR (128)
  11. DECLARE @execstr       VARCHAR (255)
  12. DECLARE @objectid      INT
  13. DECLARE @indexid       INT
  14. DECLARE @indexname VARCHAR(128)
  15. DECLARE @frag            DECIMAL
  16. DECLARE @maxfrag     DECIMAL
  17.  
  18. -- EStablecemos el porcenataje maximo permitido de fragmentacion.
  19. SELECT @maxfrag = 10.0
  20.  
  21. -- Declaramos el cursos.
  22. DECLARE TABLES CURSOR FOR
  23.    SELECT TABLE_NAME
  24.    FROM INFORMATION_SCHEMA.TABLES
  25.    WHERE TABLE_TYPE = 'BASE TABLE'
  26. --and table_name like 'crmpun_3%'
  27.  
  28. -- Creamos la tabla de LOG.
  29. CREATE TABLE fraglist (
  30.    ObjectName CHAR (255),
  31.    ObjectId INT,
  32.    IndexName CHAR (255),
  33.    IndexId INT,
  34.    Lvl INT,
  35.    CountPages INT,
  36.    CountRows INT,
  37.    MinRecSize INT,
  38.    MaxRecSize INT,
  39.    AvgRecSize INT,
  40.    ForRecCount INT,
  41.    Extents INT,
  42.    ExtentSwitches INT,
  43.    AvgFreeBytes INT,
  44.    AvgPageDensity INT,
  45.    ScanDensity DECIMAL,
  46.    BestCount INT,
  47.    ActualCount INT,
  48.    LogicalFrag DECIMAL,
  49.    ExtentFrag DECIMAL)
  50.  
  51. -- Abrimos el cursor.
  52. OPEN TABLES
  53.  
  54. -- Hacemos un ciclo por todas las tablas de la BD.
  55. FETCH NEXT
  56.    FROM TABLES
  57.    INTO @tablename
  58. WHILE @@FETCH_STATUS = 0
  59. BEGIN
  60.  
  61. -- Se jecuta el comando DBCC SHOWCONTIG command para obtener
  62. -- informacion de fragmentacion de todas los indices de todas las tablas.
  63.    INSERT INTO fraglist
  64.    EXEC ('DBCC SHOWCONTIG (''' + @tablename + ''')
  65.      WITH FAST, TABLERESULTS, ALL_INDEXES, NO_INFOMSGS')
  66.    FETCH NEXT
  67.       FROM TABLES
  68.       INTO @tablename
  69. END
  70.  
  71. -- Cerramos y borramos el cursor.
  72. CLOSE TABLES
  73. DEALLOCATE TABLES
  74.  
  75. -- Declaramos un cursor para obtener una lista de los indices fragmentados.
  76. DECLARE indexes CURSOR FOR
  77.    SELECT ObjectName, ObjectId, IndexId, LogicalFrag, IndexName
  78.    FROM fraglist
  79.    WHERE INDEXPROPERTY (ObjectId, IndexName, 'IndexDepth') > 0
  80. --   WHERE LogicalFrag >= @maxfrag
  81. --      AND INDEXPROPERTY (ObjectId, IndexName, 'IndexDepth') > 0
  82.  
  83. -- Abrimos el cursor.
  84. OPEN indexes
  85.  
  86. -- Hacemos un ciclo por todos los indices.
  87. FETCH NEXT
  88.    FROM indexes
  89.    INTO @tablename, @objectid, @indexid, @frag, @indexname
  90. WHILE @@FETCH_STATUS = 0
  91. BEGIN
  92.    IF @frag >= @maxfrag
  93.    BEGIN
  94.     --Si se paso del 10% de fargmentacion, desfragmentalo
  95.        PRINT 'Ejecutando DBCC INDEXDEFRAG (0, ' + RTRIM(@tablename) + ',
  96.          ' + RTRIM(@indexid) + ') - fragmentation es del : '
  97.            + RTRIM(CONVERT(VARCHAR(15),@frag)) + '%'
  98.        SELECT @execstr = 'DBCC INDEXDEFRAG (0, ' + RTRIM(@objectid) + ',
  99.           ' + RTRIM(@indexid) + ')'
  100.        EXEC (@execstr)
  101.    END
  102.  
  103.    --Reindexamos el indice
  104.    PRINT 'Ejecutando DBCC DBREINDEX (''' + RTRIM(@tablename) + ''', '
  105.       + RTRIM(@indexname) + ', 0)'
  106.    SELECT @execstr = 'DBCC DBREINDEX (''' + RTRIM(@tablename) +  ''', '
  107.         + RTRIM(@indexname) + ', 0 )'
  108.    EXEC (@execstr)
  109.  
  110.    FETCH NEXT
  111.       FROM indexes
  112.       INTO @tablename, @objectid, @indexid, @frag, @indexname
  113. END
  114. -- Cierra y elimina el cursor.
  115. CLOSE indexes
  116. DEALLOCATE indexes
  117.  
  118. GO


mas info o comments o agradecimientos por que no tambien los pueden hacer en mi blog donde este el codigo tambien directo, saludos.

http://www.lovecloud.com.mx/procedim...ces/#more-2689