SQL Server:应用填充因子小于100%后,预计会有多少索引增长

时间:2017-06-22 17:30:05

标签: sql-server indexing fillfactor

我在数据库中有大约250 GB的索引,填充因子为100%。为了提高性能,我希望将填充因子降低到95%。

因此,在将填充因子应用于索引之前,我想知道在应用95%的填充因子后索引大小会增加多少。

我期待的是,指数总量的5%会增长是否正确?

1 个答案:

答案 0 :(得分:0)

昨晚我在考虑这个问题,从逻辑上来说这是有意义的,所以我决定测试一下:

- 创建测试表

CREATE TABLE TableIndexTest (id INT, id2 INT, var VARCHAR(100))

- 填充一些有意义的数据

DECLARE @num     INT = 1000000
DECLARE @var     VARCHAR(100) = 'ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZ'
DECLARE @num2    INT = 1000000
DECLARE @iter    INT = 1000000
DECLARE @num_curr INT = 1
WHILE (@num_curr <= @num)
BEGIN
    INSERT TableIndexTest (id, id2, var) SELECT @num_curr, @num_curr, @var
    SELECT @num_curr = @num_curr + 1
END

- 使用fillfactor

创建一些索引
CREATE NONCLUSTERED INDEX ix_test_fillfactor100 ON TableIndexTest ([id], [id2], [var])
CREATE NONCLUSTERED INDEX ix_test_fillfactor90 ON TableIndexTest ([id], [id2], [var]) WITH (FILLFACTOR = 90)
CREATE NONCLUSTERED INDEX ix_test_fillfactor80 ON TableIndexTest ([id], [id2], [var]) WITH (FILLFACTOR = 80)
CREATE NONCLUSTERED INDEX ix_test_fillfactor70 ON TableIndexTest ([id], [id2], [var]) WITH (FILLFACTOR = 70)
CREATE NONCLUSTERED INDEX ix_test_fillfactor60 ON TableIndexTest ([id], [id2], [var]) WITH (FILLFACTOR = 60)
CREATE NONCLUSTERED INDEX ix_test_fillfactor50 ON TableIndexTest ([id], [id2], [var]) WITH (FILLFACTOR = 50)

- 检查索引大小

SELECT
    i.name AS IndexName,
    SUM(s.used_page_count) * 8 AS IndexSizeKB
FROM sys.dm_db_partition_stats AS s
JOIN sys.indexes AS i ON s.[object_id] = i.[object_id] AND s.index_id = i.index_id
WHERE s.[object_id] = OBJECT_ID('TableIndexTest')
GROUP BY i.name
ORDER BY SUM(s.used_page_count) * 8;

预期的结果是FillFactor设置为50的索引将是没有设置fillfactor的索引大小的一半(服务器默认值为0,因此基本上为100)。

结果:

    IndexName              IndexSizeKB
ix_test_fillfactor100         52008
ix_test_fillfactor90          57176
ix_test_fillfactor80          64480
ix_test_fillfactor70          73952
ix_test_fillfactor60          85752
ix_test_fillfactor50          103328

所以FillFactor 50 * 2 = 104,016,当预期为103,328时,但考虑到索引的最后一页可能只有1行,我认为它非常接近所以你可以安全地使用该计算