简体   繁体   English

SQL Server 2005-奇数聚集索引大小

[英]SQL Server 2005 - odd clustered index size

Existing table structure 现有表结构

CREATE TABLE [MYTABLE](
    [ROW1] [numeric](18, 0) NOT NULL,
    [ROW2] [numeric](18, 0) NOT NULL,
    [ROW3] [numeric](18, 0) NOT NULL,
    [ROW4] [numeric](18, 0) NULL,
    CONSTRAINT [MYTABLE_PK] PRIMARY KEY CLUSTERED ([ROW1] ASC, [ROW2] ASC, [ROW3] ASC)
)

This table has 2 non-clustered indexes, and the following stats: 该表具有2个非聚集索引以及以下统计信息:

RowCount:    5260744
Data Space:  229.609 MB
Index Space: 432.125 MB

I wanted to reduce the size of the indexes, and use a surrogate primary key as the clustered index, instead of the natural composite key. 我想减少索引的大小,并使用代理主键作为聚簇索引,而不是自然组合键。

New table structure 新表结构

CREATE TABLE [dbo].[TEST_RUN_INFO](
    [ROW1] [numeric](18, 0) NOT NULL,
    [ROW2] [numeric](18, 0) NOT NULL,
    [ROW3] [numeric](18, 0) NOT NULL,
    [ROW4] [numeric](18, 0) NULL,
    [ID] [int] IDENTITY(1,1) NOT NULL,
    CONSTRAINT [MYTABLE_PK] PRIMARY KEY CLUSTERED ([ID] ASC)
)

Still with only 2 non-clustered indexes, here's the new stats: 仍然只有2个非聚集索引,这是新的统计信息:

RowCount:    5260744
Data Space:  249.117 MB
Index Space: 470.867 MB

Question

Can someone account for how a clustered index using 3 NUMERIC(18,0) columns is smaller than a clustered index using a single INT column? 有人可以解释使用3个NUMERIC(18,0)列的聚集索引比使用单个INT列的聚集索引小吗?

I rebuilt the indexes before and after the changes, and the fill factor is set to 0 for both structures. 我在更改前后重建了索引,并且两种结构的填充因子均设置为0。

The two non-clustered indexes are the same, and were not changed to include the new ID column. 这两个非聚集索引相同,并且未更改为包括新的ID列。

sys.dm_db_index_physical_stats sys.dm_db_index_physical_stats

Stats taken with the ID column 使用ID列获取的统计信息

Composite clustered index 复合聚类索引

INDEX   TYPE            DEPTH   LEVEL   PAGECOUNT   RECORDCOUNT RECORDSIZE  
1       CLUSTERED       3       0       31884       5260744     47
1       CLUSTERED       3       1       143         31884       34
1       CLUSTERED       3       2       1           143         34
5       NONCLUSTERED    3       0       27404       5260744     40
5       NONCLUSTERED    3       1       167         27404       46
5       NONCLUSTERED    3       2       1           167         46
6       NONCLUSTERED    3       0       27400       5260744     40
6       NONCLUSTERED    3       1       164         27400       46
6       NONCLUSTERED    3       2       1           164         46

INT clustered index INT聚集索引

INDEX   TYPE            DEPTH   LEVEL   PAGECOUNT   RECORDCOUNT RECORDSIZE  
1       CLUSTERED       3       0       31887       5260744     47
1       CLUSTERED       3       1       54          31887       11
1       CLUSTERED       3       2       1           54          11
5       NONCLUSTERED    4       0       29893       5260744     44
5       NONCLUSTERED    4       1       198         29893       50
5       NONCLUSTERED    4       2       3           198         50
5       NONCLUSTERED    4       3       1           3           50
6       NONCLUSTERED    4       0       29891       5260744     44
6       NONCLUSTERED    4       1       193         29891       50
6       NONCLUSTERED    4       2       2           193         50
6       NONCLUSTERED    4       3       1           2           50

The clustered index leaf pages include all the columns of the table (not just the key columns). 聚簇索引叶页面包括表的所有列(而不仅仅是键列)。 By adding a surrogate primary key you have just increased the length of all rows in the leaf pages by 4 bytes. 通过添加代理主键,您刚刚将叶子页中所有行的长度增加了4个字节。 Multiply that out by 5,260,744 rows and that equals an additional 20 MB to store the ID column. 将其乘以5,260,744行,即等于另外20 MB以存储ID列。

The key is narrower however so you may well have fewer non leaf level pages (use sys.dm_db_index_physical_stats to see this) and as the clustered index key is used as the row locator in the non clustered indexes this can make those smaller (but less covering) too. 但是,键较窄,因此您可能会拥有较少的非叶级页面(请使用sys.dm_db_index_physical_stats进行查看),并且由于将聚簇索引键用作非聚簇索引中的行定位符,因此可以使那些变小(但覆盖范围较小) )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM