我有一个带有 Binary(128) 列的表。我选择了 Binary(128) 而不是 Varbinary(128),因此它会保留所有 128 个字节并在更新二进制值时防止页面拆分。出于测试目的,我的索引上的 FILLFACTOR 为 100。
我发现即使将列设置为 Binary(128) 值,在对该列执行更新时仍会导致页面拆分。
有人知道为什么吗?
这是一些要测试的代码...
-- Create working dataset
Create Table TestTable (tID Int Identity, TestBinary Binary(128));
Create Clustered Index hmmmm On TestTable (TestBinary) With (FillFactor=100);
With recur As
(
Select Convert(Binary(128),0x01) As val, 1 As incr
Union All
Select Convert(Binary(128),incr + 0x01), incr + 1
From recur
Where incr < 100
)
Insert TestTable (TestBinary)
Select Convert(Binary(128),Convert(Int,r.val) + Convert(Int,r2.val) + Convert(Int,r3.val)) As TestBinary
From recur r
Cross Join recur r2
Cross Join recur r3;
-- Rebuild Index as needed while testing
Alter Index [hmmmm] On [dbo].[TestTable] Rebuild
-- Check index fragmentation
SELECT Db_Name() As DatabaseName,
o.id,
s.name,
o.name,
page_count,
record_count,
index_type_desc,
index_id,
index_depth,
index_level,
avg_fragmentation_in_percent,
fragment_count,
avg_fragment_size_in_pages,
avg_page_space_used_in_percent
From sys.dm_db_index_physical_stats (DB_ID(), Object_ID('dbo.TestTable'), NULL , NULL, 'Detailed') n
Join sysobjects o
On n.object_id = o.id
Join sys.schemas s
On o.uid = s.schema_id;
-- Update the records
Update t
Set TestBinary = Convert(Binary(128),(Convert(Int,TestBinary) + 10000))
From TestTable t
如果您使用 100 的 FILLFACTOR 执行大型更新,则会导致严重的碎片,但如果我的 FILLFACTOR 为 90,一切都很好。我认为非 var 数据类型应该保留内存,所以你不会遇到这个问题。存储膨胀从何而来?
谢谢!