使用 *BOTH* 堆和聚集索引修复 SQL 表

Pet*_*erX -3 sql-server clustered-index heap sql-server-2014

不知何故,我们有一个 SQL Server 表,表上既有HEAP 索引,也有 CLUSTERED 索引。有没有办法来解决这个问题?例如,通过对象 ID 删除 HEAP 索引?

如果我们删除聚集索引,它将创建第二个 HEAP,当重新创建聚集索引时,它将被删除。这个僵尸 HEAP 索引将保留。

在此处输入图片说明

Joh*_* N. 7

抱歉,表中的最后一个条目是针对不同的对象/表的。452964740 != 562309263. 所以你在同一个表中没有堆和聚集索引。

解释

堆是没有聚集索引的表。

参考:堆(没有聚集索引的表)

所以你不能在一个表中同时拥有一个和一个聚集索引

系统索引

sys.indexes 的定义如下:

Column name         Data type           Description
------------------- ------------------- ---------------------------------------------------------------------------
object_id           int                 ID of the object to which this index belongs.  
name                sysname             Name of the index. name is unique only within the object.  NULL = Heap  
index_id            int                 ID of the index. index_id is unique only within the object. 
                                        0 = Heap; 
                                        1 = Clustered index; 
                                        > 1 = Nonclustered index   
type                tinyint             Type of index:
                                        0 = Heap
                                        1 = Clustered
                                        2 = Nonclustered
                                        3 = XML
                                        4 = Spatial
                                        5 = Clustered xVelocity memory optimized columnstore index (Reserved for future use.)  
                                        6 = Nonclustered columnstore index  
type_desc            nvarchar(60)       Description of index type:
                                        HEAP
                                        CLUSTERED
                                        NONCLUSTERED
                                        XML
                                        SPATIAL
                                        CLUSTERED COLUMNSTORE (Reserved for future use.)
                                        NONCLUSTERED COLUMNSTORE  
is_unique            bit                1 = Index is unique.
                                        0 = Index is not unique.  
data_space_id        int                ID of the data space for this index. Data space is either a filegroup or partition scheme.
                                        0 = object_id is a table-valued function.  
ignore_dup_key       bit                1 = IGNORE_DUP_KEY is ON.
                                        0 = IGNORE_DUP_KEY is OFF.  
is_primary_key        bit               1 = Index is part of a PRIMARY KEY constraint.   
is_unique_constraint  bit               1 = Index is part of a UNIQUE constraint.  
fill_factor           tinyint           > 0 = FILLFACTOR percentage used when the index was created or rebuilt.  
                                        0 = Default value
is_padded             bit               1 = PADINDEX is ON.
                                        0 = PADINDEX is OFF.
is_disabled           bit               1 = Index is disabled.
                                        0 = Index is not disabled.
is_hypothetical       bit               1 = Index is hypothetical and cannot be used directly as a data access path. Hypothetical indexes hold column-level statistics.  
                                        0 = Index is not hypothetical.
allow_row_locks       bit               1 = Index allows row locks.
                                        0 = Index does not allow row locks.
allow_page_locks      bit               1 = Index allows page locks.
                                        0 = Index does not allow page locks.
has_filter            bit               1 = Index has a filter and only contains rows that satisfy the filter definition.
                                        0 = Index does not have a filter.
filter_definition     nvarchar(max)     Expression for the subset of rows included in the filtered index.
                                        NULL for heap or non-filtered index.
Run Code Online (Sandbox Code Playgroud)

如果您像这样加入sys.indexes,您将获得更好的结果sys.objects

select so.name, si.* from sys.indexes as si 
    join sys.objects as so 
        on si.object_id = so.object_id
Run Code Online (Sandbox Code Playgroud)

您可以在此语句的基础上构建并连接sys.schemas以检索表的(模式-)所有者等。

参考资料: