【问题标题】:How do I write a query to compare the indexes on two SQL Server tables?如何编写查询来比较两个 SQL Server 表上的索引?
【发布时间】:2010-08-18 05:53:23
【问题描述】:

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

【问题讨论】:

  • 我不明白 - 您想在 SQL Server 数据库中查找您有哪些不同的索引?或者什么数据不同??你需要更精确地问......
  • 我的理解是您想比较来自不同 SQL 服务器或数据库的两个表?
  • 继续为 SO 的赞助产品 (stackoverflow.com/questions/tagged/sql-server) 加油。 SQL 比较 (red-gate.com/products/sql_bundles/SQL_Comparison_Bundle.htm) 确实会为这类任务节省金钱和时间。
  • 基本上我正在为 ERP 应用程序进行数据迁移项目。应用程序版本不同,因此我必须在迁移数据之前检查索引结构的差异。
  • 我不想购买 sql compare 产品。

标签: sql sql-server sql-server-2005 sql-server-2008


【解决方案1】:

如果两个数据库在同一台服务器上,那么您可以执行一系列查询,将两个数据库上的 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

【讨论】:

    【解决方案2】:

    sys.indexes 包含有关给定数据库上索引的所有信息。

    【讨论】:

      【解决方案3】:

      如果你想要的只是结构,你可以使用这个procedure 生成脚本。

      然后您可以比较两个数据库之间的文件。

      【讨论】:

        猜你喜欢
        • 2010-11-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-08-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多