【轉】SQL SERVER擷取索引指令碼

來源:互聯網
上載者:User

標籤:jna   path   esc   for   fill   substring   factor   sts   _id   

關於如何擷取索引指令碼的語句很多,上次在項目中需要去查詢並擷取索引指令碼,所以寫了一個簡單的查詢語句來進行擷取。

 

WITH    idxcol
          AS ( SELECT
                i.object_id ,
                i.index_id ,
                OBJECT_NAME(i.object_id) AS objname ,
                i.name AS idxname ,
                ocol.name AS colname ,
                i.type AS idxtype ,
                i.type_desc AS idxtypedesc ,
                i.is_unique ,
                i.is_primary_key ,
                i.is_unique_constraint ,
                i.fill_factor ,
                icol.key_ordinal AS idxcoloder ,
                icol.is_descending_key ,
                icol.is_included_column ,
                pt.row_count ,
                pt.used_page_count * 8 *1024.0 / POWER(1024, 2) AS [usedrowpage_mb] ,
                pt.reserved_page_count * 8 *1024.0 / POWER(1024, 2) AS [allrowpage_MB]--,  
  --* 
               FROM
                sys.indexes i ,
                sys.index_columns icol ,
                sys.columns ocol ,
                sys.dm_db_partition_stats pt
               WHERE
                i.object_id = icol.object_id
                AND i.index_id = icol.index_id
                AND icol.object_id = ocol.object_id
                AND icol.column_id = ocol.column_id
                AND i.object_id = pt.object_id
                AND i.index_id = pt.index_id
                AND EXISTS ( SELECT
                                1
                             FROM
                                sys.objects o
                             WHERE
                                o.object_id = i.object_id
    
                             AND o.type = ‘U‘ ))

  SELECT
    * ,
    N‘CREATE ‘ + t.idxtypedesc COLLATE Latin1_General_CI_AS_KS_WS + 
 N‘ INDEX ‘ + t.idxname COLLATE Latin1_General_CI_AS_KS_WS + 
 N‘ ON ‘ + t.objname COLLATE Latin1_General_CI_AS_KS_WS +
 N‘(‘ + CASE WHEN t.colsinc IS NULL THEN 
  t.cols COLLATE Latin1_General_CI_AS_KS_WS 
 ELSE 
 --REPLACE(cols,t.colsinc,‘‘) COLLATE Latin1_General_CI_AS_KS_WS 
 SUBSTRING(cols,LEN(colsinc)+2,LEN(cols)-LEN(colsinc)) 
 END 
 + N‘)‘+CASE WHEN t.colsinc IS NOT NULL THEN ‘ INCLUDE(‘+t.colsinc+‘)‘ ELSE ‘ ‘ END 
  FROM
    ( SELECT 
  DISTINCT
        object_id ,
        index_id ,
        objname ,
        idxname ,
        idxtypedesc ,
        CASE WHEN is_primary_key = 1 THEN ‘prmiary key‘
             ELSE CASE WHEN is_unique_constraint = 1 THEN ‘unique constraint‘
                       ELSE CASE WHEN is_unique = 1 THEN ‘Unique ‘
                                 ELSE ‘‘
                            END + idxtypedesc
                  END
        END AS typedesc ,
        STUFF(( SELECT
                    ‘,‘ + colname + CASE WHEN is_descending_key = 1 THEN ‘ desc‘
                                         ELSE ‘‘
                                    END
                FROM
                    idxcol
                WHERE
                    object_id = c.object_id
                    AND index_id = c.index_id
                ORDER BY
                    idxcoloder
              FOR
                XML PATH(‘‘) ), 1, 1, ‘‘) AS cols ,
        STUFF(( SELECT
                    ‘,‘ + colname
                FROM
                    idxcol
                WHERE
                    object_id = c.object_id
                    AND index_id = c.index_id
                    AND is_included_column = 1
                ORDER BY
                    idxcoloder
              FOR
                XML PATH(‘‘) ), 1, 1, ‘‘) AS colsinc ,
        row_count ,
        [allrowpage_MB] ,
        [usedrowpage_mb] ,
        [allrowpage_MB] - [usedrowpage_mb] AS unusedrowpage_mb
      FROM
        idxcol c ) AS t

【轉】SQL SERVER擷取索引指令碼

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.