如何编写查询来比较两个 SQL Server 表上的索引?

Gai*_*ter 4 sql sql-server sql-server-2005 sql-server-2008

我必须编写一个 SQL 脚本来比较 SQL Server 上两个表的索引之间的差异。如何通过 SQL 查询获取表上索引的结构?

Con*_*lls 5

如果两个数据库在同一台服务器上,那么您可以执行一系列查询,将两个数据库上的 sys.indexes 和 sys.index_columns 表进行外部连接。您还需要查看 sys.index_columns 以检查列是否相同(还要检查相同的顺序 - 这会影响查询计划)。

如果两个数据库在不同的服务器上,您需要将 sys.indexes 和 sys.index_columns 的内容复制到另一台服务器上,并对您的表副本执行类似的查询。

此类查询的示例可能如下所示(如果您想查看单个表,请将适当的数据库替换为 FOO 和 BAR,并为各个查询添加适当的过滤器):

select *
  from
       (select s1.name           as SchemaName
              ,t1.name           as TableName
              ,c1.name           as ColumnName
              ,i1.name           as IndexName
              ,i1.index_id
              ,c1.column_id
              ,c1.system_type_id
              ,c1.user_type_id
              ,ty1.name          as ColumnType
              ,c1.collation_name  -- Note this is nullable
              ,c1.is_nullable
              ,c1.max_length
              ,c1.[precision]
              ,c1.scale
              ,ic1.index_column_id
              ,ic1.key_ordinal
              ,ic1.partition_ordinal
              ,ic1.is_descending_key
              ,ic1.is_included_column
          from [FOO].sys.schemas s1
          join [FOO].sys.tables t1
            on t1.schema_id = s1.schema_id
          join [FOO].sys.columns c1
            on t1.object_id = c1.object_id
          join [FOO].sys.types ty1
            on ty1.system_type_id = c1.system_type_id
           and ty1.user_type_id = c1.user_type_id
          join [FOO].sys.index_columns ic1
            on ic1.object_id = c1.object_id
           and ic1.column_id = c1.column_id
          join [FOO].sys.indexes i1
            on i1.object_id = ic1.object_id
           and i1.index_id = ic1.index_id) r1
  full outer join
       (select s1.name           as SchemaName
              ,t1.name           as TableName
              ,c1.name           as ColumnName
              ,i1.name           as IndexName
              ,i1.index_id
              ,c1.column_id
              ,c1.system_type_id
              ,c1.user_type_id
              ,ty1.name          as ColumnType
              ,c1.collation_name  -- Note this is nullable
              ,c1.is_nullable
              ,c1.max_length
              ,c1.[precision]
              ,c1.scale
              ,ic1.index_column_id
              ,ic1.key_ordinal
              ,ic1.partition_ordinal
              ,ic1.is_descending_key
              ,ic1.is_included_column
          from [BAR].sys.schemas s1
          join [BAR].sys.tables t1
            on t1.schema_id = s1.schema_id
          join [BAR].sys.columns c1
            on t1.object_id = c1.object_id
          join [BAR].sys.types ty1
            on ty1.system_type_id = c1.system_type_id
           and ty1.user_type_id = c1.user_type_id
          join [BAR].sys.index_columns ic1
            on ic1.object_id = c1.object_id
           and ic1.column_id = c1.column_id
          join [BAR].sys.indexes i1
            on i1.object_id = ic1.object_id
           and i1.index_id = ic1.index_id) r2 
    on r1.SchemaName = r2.SchemaName
   and r1.TableName = r2.TableName
   and r1.ColumnName = r2.ColumnName
   and r1.IndexName = r2.IndexName
Run Code Online (Sandbox Code Playgroud)