【问题标题】:list of tables without indexes in sql 2008sql 2008中没有索引的表列表
【发布时间】:2009-09-02 19:05:01
【问题描述】:

如何在我的 SQL 2008 数据库中列出没有索引的表?

编辑
我想要 Schema 名称和 Table 名称。

【问题讨论】:

    标签: sql-server tsql sql-server-2008


    【解决方案1】:

    这应该涵盖您正在寻找的内容。即是堆(没有聚集索引)并且没有任何非聚集索引的表。它使用新的系统。 2005/2008 年使用的表对象。

    此外,您可能想要查找具有聚集索引但没有非聚集索引的表(这是我已注释掉的语句的第二部分。

    SELECT 
         schemaname = OBJECT_SCHEMA_NAME(o.object_id)
        ,tablename = o.NAME
    FROM sys.objects o
    INNER JOIN sys.indexes i ON i.OBJECT_ID = o.OBJECT_ID
    -- tables that are heaps without any nonclustered indexes
    WHERE (
            o.type = 'U'
            AND o.OBJECT_ID NOT IN (
                SELECT OBJECT_ID
                FROM sys.indexes
                WHERE index_id > 0
                )
            )
            --    OR
            -- table that have a clustered index without any nonclustered indexes
            --(o.type='U' 
            --        AND o.OBJECT_ID NOT IN (
            --    SELECT OBJECT_ID 
            --        FROM sys.indexes 
            --        WHERE index_id>1))  
    

    【讨论】:

      【解决方案2】:

      这是一个例子:

      select SCHEMA_NAME(schema_id), name from sys.tables 
      where OBJECTPROPERTY(object_id, 'IsIndexed')= 0
      

      【讨论】:

        【解决方案3】:

        除了@Philip Fourie 的建议之外,您可能还想考虑创建哪些索引。

        一旦您访问了您的数据,SQL Server 2008 就会跟踪它认为索引有用的地方(它将这些称为“缺失的索引”。有很多新的动态托管视图可以显示这些缺少索引和一些关于它们的信息。

        来自MSSQlTips

        • sys.dm_db_missing_index_details - 返回有关缺失索引的详细信息
        • sys.dm_db_missing_index_group_stats - 返回有关缺失索引组的摘要信息
        • sys.dm_db_missing_index_groups - 返回有关缺失索引的特定组的信息
        • sys.dm_db_missing_index_columns(index_handle) - 返回有关索引缺失的数据库表列的信息。这是一个函数,需要传递 index_handle。

        【讨论】:

          【解决方案4】:
          select shema = s.name, table_name = o.name
          from sys.objects o 
          join sys.schemas s on o.schema_id = s.schema_id
          where type = 'U'
          and not exists (select i.index_id 
                          from sys.indexes i 
                          where i.type <> 0 --ignore default heap index row
                          and o.object_id = i.object_id )
          

          编辑:
          我已根据要求更新了 SQL 以包含架构名称。 (注意我必须使用 sys.objects 而不是 sysobjects 来满足 SQL 2005 中引入的模式)

          目录表记录在 SQL Server 文档中,请参阅 this link.
          This FAQ 包含更多示例,可能也很有用。

          请注意,这些是系统表,可以在 SQL Server 版本之间更改,在可能的情况下,请使用名为 Information Schema Views 的与系统表无关的视图。

          【讨论】:

          • 如何获取这些对象的架构?
          • 它们在 2005/2008 年作为“兼容性”视图仍然有效。我已经发布了我自己的版本,它使用 2005/2008 系统对象
          • @Raj More,我已按要求使用架构名称更新了我的答案
          • @Nick Kavadias,我同意“兼容性”视图仍然有效,但不包含 Raj 所需的信息(架构详细信息)。
          【解决方案5】:

          这段代码给出了所有表的索引的所有细节:

          SELECT 
              sch.name AS [Schema],
              obj.name AS TableName,
              indx.name AS IndexName,
              CASE 
                      WHEN indx.type_desc = 'HEAP' THEN 'N/A'
                      ELSE indx.type_desc 
                  END AS IndexType
          FROM sys.objects obj
          JOIN sys.indexes indx ON indx.object_id = obj.object_id
          JOIN sys.schemas AS sch ON sch.schema_id = obj.schema_id
          WHERE 
              obj.type = 'U'
          ORDER BY 
              obj.name
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2012-10-03
            • 1970-01-01
            • 2013-11-20
            • 1970-01-01
            • 2014-04-12
            • 1970-01-01
            相关资源
            最近更新 更多