DO's&DONT's #7: やらない方がいいこと - インデックス再構築 (REBUILD) 後のデータファイル圧縮 (SHRINK)

Published Oct 19 2021 10:19 PM 1,597 Views
Microsoft

インデックスの再構築 (ALTER INDEX REBUILD または DBCC DBREINDEX) を行う目的が、ページ密度を向上させて、データベースファイル内の使用領域サイズを小さくすることであれば、インデックス再構築後にデータベース圧縮を行っても、その目的は損なわれません。しかし、インデックスを再構築する目的が、論理断片化を解消し、検索パフォーマンスを向上させることであれば、インデックス再構築後にデータベース圧縮を行うと、その目的は達せられなくなります。

 

  • ページ密度 (Page Density) : ページ内でデータが占める割合。100% の場合、それ以上そのページにはデータが入らない、空き領域がない状態。ただし、行サイズとページサイズの関係上、100% になることはほとんどない。(例 : 1 行 100 バイトの場合、1 ページには、8060 バイトのデータが入るため、ギッシリ詰め込んでも 8060%100=60 と 60 バイトの空きは必ずできてしまう。)
  • 論理断片化 (Logical Fragmentation / Logical Scan Fragmentation) 率 : ページの物理的な順番と論理的なリンクが異なる割合。ページ番号はファイルの先頭から順に 0, 1, 2 ... と振られているが、論理的なページのリンクが 81 の次に 48 など、ページ番号順になっていない割合。

 

なぜ?

インデックスを再構築すると、インデックスは再作成され、各インデックスページは FILLFACTOR の設定に従ってデータで埋められます。また、データは、可能な限り物理的な順番に並ぶように配置されます。その結果、ページ密度が高くなり、論理断片化が解消します。
データベースやデータベースファイルの圧縮 (DBCC SHRINKDATABASE や SHRINKFILE) を実行すると、ファイルの後ろの方にあるデータは前方に移動され、ファイルの後方に空き領域が作り出され、その作り出されたファイル後方の空き領域部分を切り捨てることで、データベースファイルサイズが小さくなります。
インデックスを再構築した後にデータベースやデータベースファイルの圧縮を行うと、せっかく物理的に並んだデータが再び移動させられ、物理的に並んだ状態ではなくなってしまいます。つまり、インデックスの再構築により断片化の解消されたインデックスが、データベースの圧縮処理により再び断片化します。

 

 

実際に見てみましょう

----------------------------------------------

-- データベースを作成します。

create database shrinktest go

moematsuo_0-1634706145369.png

 

use shrinktest go

 

 

-- あとでファイルの前方に空きを作るために、ダミーのテーブルを作成します。 -- 1 ページに1 行入る計算です。

create table dbo.dummy (c1 int primary key clustered, c2 nvarchar(4000)) go

declare @i int set @i=0 while (@i<3000) begin   insert into dbo.dummy values (@i, REPLICATE(N'X',4000))   set @i+=1 end

moematsuo_1-1634706145371.png

 

 

-- 断片化を発生させるためのテーブルを作成します。

create table dbo.tab (c1 int primary key clustered, c2 nvarchar(800)) go

moematsuo_2-1634706145371.png

 

-- 偶数キー値のデータを入れます。

declare @i int set @i=0 while (@i<100) begin   insert into dbo.tab values (@i, REPLICATE('X',800))   set @i+=2 end

moematsuo_3-1634706145372.png

 

-- 奇数キー値のデータを入れます。 -- これにより断片化が発生します。

declare @i int set @i=1 while (@i<100) begin   insert into tab values (@i, REPLICATE('X',800))   set @i+=2 end go

moematsuo_4-1634706145372.png

 

 

-- 断片化の状況を見てみましょう

select index_type_desc, alloc_unit_type_desc, index_depth, index_level, avg_fragmentation_in_percent, fragment_count, avg_fragment_size_in_pages, page_count, avg_page_space_used_in_percent from sys.dm_db_index_physical_stats(db_id('shrinktest'),object_id('tab'),NULL, DEFAULT,'DETAILED') go

 

断片化率 63.3333333333333%、ページ密度 66.5678280207561% です。

index_type_ desc

alloc_unit_ type_desc

index_ depth

index_ level

avg_ fragmentation_ in_percent

fragment_ count

avg_ fragment_ size_ in_pages

page_ count

avg_page_space_ used_in_percent

CLUSTERED INDEX

IN_ROW_DATA

2

0

63.3333333333333

20

1.5

30

66.5678280207561

CLUSTERED INDEX

IN_ROW_DATA

2

1

0

1

1

 

 

 

-- インデックスを再構築します。

alter table tab rebuild go

moematsuo_5-1634706145373.png

 

 

-- インデックス再構築後の断片化の状況を見てみましょう。

select index_type_desc, alloc_unit_type_desc, index_depth, index_level, avg_fragmentation_in_percent, fragment_count, avg_fragment_size_in_pages, page_count, avg_page_space_used_in_percent from sys.dm_db_index_physical_stats(db_id('shrinktest'),object_id('tab'),NULL, DEFAULT,'DETAILED') go

 

論理断片化が 15% に低下し、ページ密度が 99.864096861873% まで上昇しました。

index_type_ desc

alloc_unit_ type_desc

index_ depth

index_ level

avg_ fragmentation_ in_percent

fragment_ count

avg_ fragment_ size_ in_pages

page_ count

avg_page_space_ used_in_percent

CLUSTERED INDEX

IN_ROW_DATA

2

0

15

5

4

20

99.864096861873

CLUSTERED INDEX

IN_ROW_DATA

2

1

0

1

1

1

3.18754633061527

 

 

-- ダミーのテーブルを削除して、ファイル内に空き領域を作成します。

drop table dbo.dummy go

moematsuo_6-1634706145374.png

 

 

-- データファイル圧縮を実行します。

dbcc shrinkfile('shrinktest') go

moematsuo_7-1634706145374.png

 

 

-- データファイル圧縮後の断片化の状況を見てみましょう。

select index_type_desc, alloc_unit_type_desc, index_depth, index_level, avg_fragmentation_in_percent, fragment_count, avg_fragment_size_in_pages, page_count, avg_page_space_used_in_percent from sys.dm_db_index_physical_stats(db_id('shrinktest'),object_id('tab'),NULL, DEFAULT,'DETAILED') go

 

ページ密度は変化ありませんが、断片化率が 90% まで上昇してしまいました。インデックス再構築する前よりも断片化が進んでしまったことになります。

index_type_ desc

alloc_unit_ type_desc

index_ depth

index_ level

avg_ fragmentation_ in_percent

fragment_ count

avg_ fragment_ size_ in_pages

page_ count

avg_page_space_ used_in_percent

CLUSTERED INDEX

IN_ROW_DATA

2

0

90

19

1.05263157894737

20

99.864096861873

CLUSTERED INDEX

IN_ROW_DATA

2

1

0

1

1

1

3.18754633061527

 

 

圧縮処理により、ファイルの後ろの方にあるデータは前方に移動され、ファイルの後方に空き領域が作り出されます。

その作り出されたファイル後方の空き領域部分を切り捨てることで、データベースファイルサイズが小さくなります。インデックスを再構築した後にデータベースやデータベースファイルの圧縮を行うと、せっかく物理的に並んだデータが再び移動させられ、物理的に並んだ状態ではなくなってしまいます。

 

 

moematsuo_8-1634706145375.png

 

つまり、インデックスの再構築により断片化の解消されたインデックスが、データベースの圧縮処理により再び断片化します。

 

 

--------------

この記事は以下のBlogの画像のリンク切れを修正し、再掲したものになります。

DO's&DONT's #7: やらない方がいいこと - インデックス再構築 (REBUILD) 後のデータファイル圧縮 (SHRINK) | Microsoft Docs

%3CLINGO-SUB%20id%3D%22lingo-sub-2863833%22%20slang%3D%22ja-JP%22%3EDO's%20DONT's%20%237%3A%20What%20You%20Shouldn't%20Do%20-%20Data%20File%20Compression%20after%20Index%20Rebuild%20(SHRINK)%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2863833%22%20slang%3D%22ja-JP%22%3E%3CP%3E%3CFONT%20size%3D%223%22%3EIf%20the%20purpose%20of%20rebuilding%20the%20index%20(ALTER%20INDEX%20REBUILD%20or%20DBCC%20DBREINDEX)%20is%20to%20increase%20page%20density%20and%20reduce%20the%20size%20of%20the%20space%20used%20in%20the%20database%20file%2C%20database%20compression%20after%20the%20index%20rebuild%20does%20not%20compromise%20the%20purpose.%20However%2C%20if%20the%20purpose%20of%20rebuilding%20an%20index%20is%20to%20eliminate%20logical%20fragmentation%20and%20improve%20search%20performance%2C%20database%20compression%20after%20index%20rebuilding%20will%20prevent%20it%20from%20being%20reached.%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CUL%3E%0A%3CLI%3E%3CFONT%20size%3D%223%22%3EPage%20Density%3A%20The%20percentage%20of%20data%20in%20the%20page.%20At%20100%25%2C%20there%20is%20no%20more%20data%20on%20the%20page%20and%20no%20free%20space.%20However%2C%20in%20relation%20to%20row%20size%20and%20page%20size%2C%20it%20is%20rarely%20100%25.%20(Example%3A%20For%20a%20line%20of%20100%20bytes%2C%20a%20page%20contains%208060%20bytes%20of%20data%2C%20so%20even%20if%20it%20is%20packed%2C%20it%20will%20always%20have%208060%25%20100%3D60%20and%2060%20bytes%20of%20free.)%20)%3C%2FFONT%3E%3C%2FLI%3E%0A%3CLI%3E%3CFONT%20size%3D%223%22%3ELogical%20Fragmentation%20Rate%3A%20The%20percentage%20of%20pages%20with%20different%20physical%20order%20and%20logical%20links.%20Page%20numbers%20are%200%2C%201%2C%202%2C%20from%20the%20beginning%20of%20the%20file%20to%20the%20beginning%20of%20the%20file...%20but%20the%20percentage%20of%20logical%20page%20links%20not%20in%20the%20order%20of%20page%20numbers%2C%20such%20as%2081%20followed%20by%2048.%3C%2FFONT%3E%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId--322920363%22%20id%3D%22toc-hId--322920270%22%3E%3CFONT%20size%3D%225%22%3E%3CSTRONG%3Ewhy%3F%3C%2FSTRONG%3E%3C%2FFONT%3E%3C%2FH2%3E%0A%3CP%3E%3CFONT%20size%3D%223%22%3ERebuilding%20the%20index%20recreates%20the%20index%20and%20fills%20each%20index%20page%20with%20data%20according%20to%20fillfactor%20settings.%20The%20data%20is%20also%20arranged%20in%20the%20physical%20order%20possible.%20The%20result%20is%20higher%20page%20density%20and%20eliminate%20logical%20fragmentation.%20%3C%2FFONT%3E%3CBR%20%2F%3E%20%3CFONT%20size%3D%223%22%3EWhen%20you%20compress%20a%20database%20or%20database%20file%20(DBCC%20SHRINKDATABASE%20or%20SHRINKFILE)%2C%20the%20data%20behind%20the%20file%20is%20moved%20forward%2C%20creating%20free%20space%20behind%20the%20file%2C%20and%20truncating%20the%20free%20space%20behind%20the%20created%20file%20to%20reduce%20the%20database%20file%20size.%20%3C%2FFONT%3E%3CBR%20%2F%3E%20%3CFONT%20size%3D%223%22%3EIf%20you%20compress%20a%20database%20or%20database%20file%20after%20rebuilding%20the%20index%2C%20the%20physically%20aligned%20data%20will%20be%20moved%20again%2C%20and%20it%20will%20not%20be%20physically%20aligned.%20This%20means%20that%20indexes%20that%20have%20been%20de-fragmented%20by%20rebuilding%20the%20indexes%20are%20fragmented%20again%20by%20compressing%20the%20database.%20%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CFONT%20size%3D%225%22%3E%3CSTRONG%3Elet's%20actually%20take%20a%20look%3C%2FSTRONG%3E%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3E%3CFONT%20size%3D%223%22%3E%3CSTRONG%3E----------------------------------------------%3C%2FSTRONG%3E%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3E%3CFONT%20size%3D%223%22%3E--%20Create%20a%20database.%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3E%3CFONT%20size%3D%223%22%3Ecreate%20database%20shrinktest%20go%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3E%3CFONT%20size%3D%223%22%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22moematsuo_0-1634706145369.png%22%20style%3D%22width%3A%20400px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F318605iF3986173CCD23F3A%2Fimage-size%2Fmedium%3Fv%3Dv2%26amp%3Bpx%3D400%22%20role%3D%22button%22%20title%3D%22moematsuo_0-1634706145369.png%22%20alt%3D%22moematsuo_0-1634706145369.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CFONT%20size%3D%223%22%3Euse%20shrinktest%20go%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CFONT%20size%3D%223%22%3E--%20Create%20a%20dummy%20table%20to%20make%20room%20in%20front%20of%20the%20file%20later.%20--%20Calculation%20that%20is%20one%20line%20per%20page.%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3E%3CFONT%20size%3D%223%22%3Ecreate%20table%20dbo.dummy%20(c1%20int%20primary%20key%20clustered%2C%20c2%20nvarchar(4000))%20go%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3E%3CFONT%20size%3D%223%22%3Edeclare%20%40i%20int%20set%20%40i%3D0%20while%20(%40i%26lt%3B3000)%20begin%26nbsp%3B%26nbsp%3B%20insert%20into%20dbo.dummy%20values%20(%40i%2C%20REPLICATE(N'X'%2C4000))%26nbsp%3B%26nbsp%3B%20set%20%40i%2B%3D1%20end%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3E%3CFONT%20size%3D%223%22%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22moematsuo_1-1634706145371.png%22%20style%3D%22width%3A%20400px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F318607iEC434313C920CD8E%2Fimage-size%2Fmedium%3Fv%3Dv2%26amp%3Bpx%3D400%22%20role%3D%22button%22%20title%3D%22moematsuo_1-1634706145371.png%22%20alt%3D%22moematsuo_1-1634706145371.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CFONT%20size%3D%223%22%3E--%20Create%20a%20table%20to%20cause%20fragmentation.%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3E%3CFONT%20size%3D%223%22%3Ecreate%20table%20dbo.tab%20(c1%20int%20primary%20key%20clustered%2C%20c2%20nvarchar(800))%20go%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3E%3CFONT%20size%3D%223%22%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22moematsuo_2-1634706145371.png%22%20style%3D%22width%3A%20400px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F318606iDC756D0941DED61E%2Fimage-size%2Fmedium%3Fv%3Dv2%26amp%3Bpx%3D400%22%20role%3D%22button%22%20title%3D%22moematsuo_2-1634706145371.png%22%20alt%3D%22moematsuo_2-1634706145371.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CFONT%20size%3D%223%22%3E--%20Contains%20data%20for%20even%20key%20values.%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3E%3CFONT%20size%3D%223%22%3Edeclare%20%40i%20int%20set%20%40i%3D0%20while%20(%40i%26lt%3B100)%20begin%26nbsp%3B%26nbsp%3B%20insert%20into%20dbo.tab%20values%20(%40i%2C%20REPLICATE('X'%2C800))%26nbsp%3B%26nbsp%3B%20set%20%40i%2B%3D2%20end%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3E%3CFONT%20size%3D%223%22%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22moematsuo_3-1634706145372.png%22%20style%3D%22width%3A%20400px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F318608i4A733438A0183666%2Fimage-size%2Fmedium%3Fv%3Dv2%26amp%3Bpx%3D400%22%20role%3D%22button%22%20title%3D%22moematsuo_3-1634706145372.png%22%20alt%3D%22moematsuo_3-1634706145372.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CFONT%20size%3D%223%22%3E--%20Contains%20data%20with%20odd%20key%20values.%20--%20This%20causes%20fragmentation.%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3E%3CFONT%20size%3D%223%22%3Edeclare%20%40i%20int%20set%20%40i%3D1%20while%20(%40i%26lt%3B100)%20begin%26nbsp%3B%26nbsp%3B%20insert%20into%20tab%20values%20(%40i%2C%20REPLICATE('X'%2C800))%26nbsp%3B%26nbsp%3B%20set%20%40i%2B%3D2%20end%20go%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3E%3CFONT%20size%3D%223%22%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22moematsuo_4-1634706145372.png%22%20style%3D%22width%3A%20400px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F318609i1044F488B5D6046D%2Fimage-size%2Fmedium%3Fv%3Dv2%26amp%3Bpx%3D400%22%20role%3D%22button%22%20title%3D%22moematsuo_4-1634706145372.png%22%20alt%3D%22moematsuo_4-1634706145372.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CFONT%20size%3D%223%22%3E--%20Let's%20look%20at%20the%20fragmentation%20situation%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3E%3CFONT%20size%3D%223%22%3Eselect%20index_type_desc%2C%20alloc_unit_type_desc%2C%20index_depth%2C%20index_level%2C%20avg_fragmentation_in_percent%2C%20fragment_count%2C%20avg_fragment_size_in_pages%2C%20page_count%2C%20avg_page_space_%20used_in_percent%20from%20sys.dm_db_index_physical_stats(db_id('shrinktest')%2Cobject_id('tab')%2CNULL%2C%20DEFAULT%2C'DETAILED')%20go%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CFONT%20size%3D%223%22%3EThe%20fragmentation%20rate%20is%2063.3333333333333%25%2C%20and%20the%20page%20density%20is%2066.5678280207561%25.%3C%2FFONT%3E%3C%2FP%3E%0A%3CTABLE%3E%0A%3CTBODY%3E%0A%3CTR%3E%0A%3CTD%3E%3CP%3E%3CFONT%20size%3D%223%22%3Eindex_type_%20desc%3C%2FFONT%3E%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%3CP%3E%3CFONT%20size%3D%223%22%3Ealloc_unit_%20type_desc%3C%2FFONT%3E%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%3CP%3E%3CFONT%20size%3D%223%22%3Eindex_%20depth%3C%2FFONT%3E%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%3CP%3E%3CFONT%20size%3D%223%22%3Eindex_%20level%3C%2FFONT%3E%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%3CP%3E%3CFONT%20size%3D%223%22%3Eavg_%20fragmentation_%20in_percent%3C%2FFONT%3E%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%3CP%3E%3CFONT%20size%3D%223%22%3Efragment_%20count%3C%2FFONT%3E%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%3CP%3E%3CFONT%20size%3D%223%22%3Eavg_%20fragment_%20size_%20in_pages%3C%2FFONT%3E%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%3CP%3E%3CFONT%20size%3D%223%22%3Epage_%20count%3C%2FFONT%3E%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%3CP%3E%3CFONT%20size%3D%223%22%3Eavg_page_space_%20used_in_percent%3C%2FFONT%3E%3C%2FP%3E%0A%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3CTR%3E%0A%3CTD%3E%3CP%3E%3CFONT%20size%3D%223%22%3ECLUSTERED%20INDEX%3C%2FFONT%3E%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%3CP%3E%3CFONT%20size%3D%223%22%3EIN_ROW_DATA%3C%2FFONT%3E%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%3CP%3E%3CFONT%20size%3D%223%22%3E2%3C%2FFONT%3E%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%3CP%3E%3CFONT%20size%3D%223%22%3E0%3C%2FFONT%3E%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%3CP%3E%3CFONT%20size%3D%223%22%3E63.3333333333333%3C%2FFONT%3E%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%3CP%3E%3CFONT%20size%3D%223%22%3E20%3C%2FFONT%3E%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%3CP%3E%3CFONT%20size%3D%223%22%3E1.5%3C%2FFONT%3E%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%3CP%3E%3CFONT%20size%3D%223%22%3E30%3C%2FFONT%3E%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%3CP%3E%3CFONT%20size%3D%223%22%3E66.5678280207561%3C%2FFONT%3E%3C%2FP%3E%0A%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3CTR%3E%0A%3CTD%3E%3CP%3E%3CFONT%20size%3D%223%22%3ECLUSTERED%20INDEX%3C%2FFONT%3E%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%3CP%3E%3CFONT%20size%3D%223%22%3EIN_ROW_DATA%3C%2FFONT%3E%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%3CP%3E%3CFONT%20size%3D%223%22%3E2%3C%2FFONT%3E%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%3CP%3E%3CFONT%20size%3D%223%22%3E1%3C%2FFONT%3E%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%3CP%3E%3CFONT%20size%3D%223%22%3E0%3C%2FFONT%3E%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%3CP%3E%3CFONT%20size%3D%223%22%3E1%3C%2FFONT%3E%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%3CP%3E%3CFONT%20size%3D%223%22%3E1%3C%2FFONT%3E%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3C%2FTBODY%3E%0A%3C%2FTABLE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CFONT%20size%3D%223%22%3E--%20Rebuild%20the%20index.%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3E%3CFONT%20size%3D%223%22%3Ealter%20table%20tab%20rebuild%20go%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3E%3CFONT%20size%3D%223%22%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22moematsuo_5-1634706145373.png%22%20style%3D%22width%3A%20400px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F318610i5BA0AA3226A78F22%2Fimage-size%2Fmedium%3Fv%3Dv2%26amp%3Bpx%3D400%22%20role%3D%22button%22%20title%3D%22moematsuo_5-1634706145373.png%22%20alt%3D%22moematsuo_5-1634706145373.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CFONT%20size%3D%223%22%3E--%20Let's%20look%20at%20the%20state%20of%20fragmentation%20after%20index%20rebuilding.%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3E%3CFONT%20size%3D%223%22%3Eselect%20index_type_desc%2C%20alloc_unit_type_desc%2C%20index_depth%2C%20index_level%2C%20avg_fragmentation_in_percent%2C%20fragment_count%2C%20avg_fragment_size_in_pages%2C%20page_count%2C%20avg_page_space_%20used_in_percent%20from%20sys.dm_db_index_physical_stats(db_id('shrinktest')%2Cobject_id('tab')%2CNULL%2C%20DEFAULT%2C'DETAILED')%20go%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CFONT%20size%3D%223%22%3ELogical%20fragmentation%20has%20been%20reduced%20to%2015%25%2C%20and%20page%20density%20has%20increased%20to%2099.864096861873%25.%3C%2FFONT%3E%3C%2FP%3E%0A%3CTABLE%3E%0A%3CTBODY%3E%0A%3CTR%3E%0A%3CTD%3E%3CP%3E%3CFONT%20size%3D%223%22%3Eindex_type_%20desc%3C%2FFONT%3E%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%3CP%3E%3CFONT%20size%3D%223%22%3Ealloc_unit_%20type_desc%3C%2FFONT%3E%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%3CP%3E%3CFONT%20size%3D%223%22%3Eindex_%20depth%3C%2FFONT%3E%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%3CP%3E%3CFONT%20size%3D%223%22%3Eindex_%20level%3C%2FFONT%3E%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%3CP%3E%3CFONT%20size%3D%223%22%3Eavg_%20fragmentation_%20in_percent%3C%2FFONT%3E%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%3CP%3E%3CFONT%20size%3D%223%22%3Efragment_%20count%3C%2FFONT%3E%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%3CP%3E%3CFONT%20size%3D%223%22%3Eavg_%20fragment_%20size_%20in_pages%3C%2FFONT%3E%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%3CP%3E%3CFONT%20size%3D%223%22%3Epage_%20count%3C%2FFONT%3E%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%3CP%3E%3CFONT%20size%3D%223%22%3Eavg_page_space_%20used_in_percent%3C%2FFONT%3E%3C%2FP%3E%0A%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3CTR%3E%0A%3CTD%3E%3CP%3E%3CFONT%20size%3D%223%22%3ECLUSTERED%20INDEX%3C%2FFONT%3E%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%3CP%3E%3CFONT%20size%3D%223%22%3EIN_ROW_DATA%3C%2FFONT%3E%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%3CP%3E%3CFONT%20size%3D%223%22%3E2%3C%2FFONT%3E%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%3CP%3E%3CFONT%20size%3D%223%22%3E0%3C%2FFONT%3E%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%3CP%3E%3CFONT%20size%3D%223%22%3E15%3C%2FFONT%3E%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%3CP%3E%3CFONT%20size%3D%223%22%3E5%3C%2FFONT%3E%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%3CP%3E%3CFONT%20size%3D%223%22%3E4%3C%2FFONT%3E%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%3CP%3E%3CFONT%20size%3D%223%22%3E20%3C%2FFONT%3E%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%3CP%3E%3CFONT%20size%3D%223%22%3E99.864096861873%3C%2FFONT%3E%3C%2FP%3E%0A%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3CTR%3E%0A%3CTD%3E%3CP%3E%3CFONT%20size%3D%223%22%3ECLUSTERED%20INDEX%3C%2FFONT%3E%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%3CP%3E%3CFONT%20size%3D%223%22%3EIN_ROW_DATA%3C%2FFONT%3E%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%3CP%3E%3CFONT%20size%3D%223%22%3E2%3C%2FFONT%3E%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%3CP%3E%3CFONT%20size%3D%223%22%3E1%3C%2FFONT%3E%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%3CP%3E%3CFONT%20size%3D%223%22%3E0%3C%2FFONT%3E%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%3CP%3E%3CFONT%20size%3D%223%22%3E1%3C%2FFONT%3E%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%3CP%3E%3CFONT%20size%3D%223%22%3E1%3C%2FFONT%3E%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%3CP%3E%3CFONT%20size%3D%223%22%3E1%3C%2FFONT%3E%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%3CP%3E%3CFONT%20size%3D%223%22%3E3.18754633061527%3C%2FFONT%3E%3C%2FP%3E%0A%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3C%2FTBODY%3E%0A%3C%2FTABLE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CFONT%20size%3D%223%22%3E--%20Delete%20the%20dummy%20table%20to%20create%20free%20space%20in%20the%20file.%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3E%3CFONT%20size%3D%223%22%3Edrop%20table%20dbo.dummy%20go%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3E%3CFONT%20size%3D%223%22%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22moematsuo_6-1634706145374.png%22%20style%3D%22width%3A%20400px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F318611iFB3DDCEFEBA8E12E%2Fimage-size%2Fmedium%3Fv%3Dv2%26amp%3Bpx%3D400%22%20role%3D%22button%22%20title%3D%22moematsuo_6-1634706145374.png%22%20alt%3D%22moematsuo_6-1634706145374.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CFONT%20size%3D%223%22%3E--%20Perform%20data%20file%20compression.%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3E%3CFONT%20size%3D%223%22%3Edbcc%20shrinkfile('shrinktest')%20go%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3E%3CFONT%20size%3D%223%22%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22moematsuo_7-1634706145374.png%22%20style%3D%22width%3A%20400px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F318613iD9E132E196B07C91%2Fimage-size%2Fmedium%3Fv%3Dv2%26amp%3Bpx%3D400%22%20role%3D%22button%22%20title%3D%22moematsuo_7-1634706145374.png%22%20alt%3D%22moematsuo_7-1634706145374.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CFONT%20size%3D%223%22%3E--%20Let's%20take%20a%20look%20at%20the%20state%20of%20fragmentation%20after%20data%20file%20compression.%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3E%3CFONT%20size%3D%223%22%3Eselect%20index_type_desc%2C%20alloc_unit_type_desc%2C%20index_depth%2C%20index_level%2C%20avg_fragmentation_in_percent%2C%20fragment_count%2C%20avg_fragment_size_in_pages%2C%20page_count%2C%20avg_page_space_%20used_in_percent%20from%20sys.dm_db_index_physical_stats(db_id('shrinktest')%2Cobject_id('tab')%2CNULL%2C%20DEFAULT%2C'DETAILED')%20go%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CFONT%20size%3D%223%22%3EThe%20page%20density%20remains%20the%20same%2C%20but%20the%20fragmentation%20rate%20has%20increased%20to%2090%25.%20It's%20more%20fragmented%20than%20it%20was%20before%20the%20index%20rebuild.%3C%2FFONT%3E%3C%2FP%3E%0A%3CTABLE%3E%0A%3CTBODY%3E%0A%3CTR%3E%0A%3CTD%3E%3CP%3E%3CFONT%20size%3D%223%22%3Eindex_type_%20desc%3C%2FFONT%3E%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%3CP%3E%3CFONT%20size%3D%223%22%3Ealloc_unit_%20type_desc%3C%2FFONT%3E%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%3CP%3E%3CFONT%20size%3D%223%22%3Eindex_%20depth%3C%2FFONT%3E%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%3CP%3E%3CFONT%20size%3D%223%22%3Eindex_%20level%3C%2FFONT%3E%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%3CP%3E%3CFONT%20size%3D%223%22%3Eavg_%20fragmentation_%20in_percent%3C%2FFONT%3E%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%3CP%3E%3CFONT%20size%3D%223%22%3Efragment_%20count%3C%2FFONT%3E%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%3CP%3E%3CFONT%20size%3D%223%22%3Eavg_%20fragment_%20size_%20in_pages%3C%2FFONT%3E%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%3CP%3E%3CFONT%20size%3D%223%22%3Epage_%20count%3C%2FFONT%3E%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%3CP%3E%3CFONT%20size%3D%223%22%3Eavg_page_space_%20used_in_percent%3C%2FFONT%3E%3C%2FP%3E%0A%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3CTR%3E%0A%3CTD%3E%3CP%3E%3CFONT%20size%3D%223%22%3ECLUSTERED%20INDEX%3C%2FFONT%3E%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%3CP%3E%3CFONT%20size%3D%223%22%3EIN_ROW_DATA%3C%2FFONT%3E%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%3CP%3E%3CFONT%20size%3D%223%22%3E2%3C%2FFONT%3E%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%3CP%3E%3CFONT%20size%3D%223%22%3E0%3C%2FFONT%3E%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%3CP%3E%3CFONT%20size%3D%223%22%3E90%3C%2FFONT%3E%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%3CP%3E%3CFONT%20size%3D%223%22%3E19%3C%2FFONT%3E%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%3CP%3E%3CFONT%20size%3D%223%22%3E1.05263157894737%3C%2FFONT%3E%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%3CP%3E%3CFONT%20size%3D%223%22%3E20%3C%2FFONT%3E%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%3CP%3E%3CFONT%20size%3D%223%22%3E99.864096861873%3C%2FFONT%3E%3C%2FP%3E%0A%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3CTR%3E%0A%3CTD%3E%3CP%3E%3CFONT%20size%3D%223%22%3ECLUSTERED%20INDEX%3C%2FFONT%3E%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%3CP%3E%3CFONT%20size%3D%223%22%3EIN_ROW_DATA%3C%2FFONT%3E%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%3CP%3E%3CFONT%20size%3D%223%22%3E2%3C%2FFONT%3E%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%3CP%3E%3CFONT%20size%3D%223%22%3E1%3C%2FFONT%3E%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%3CP%3E%3CFONT%20size%3D%223%22%3E0%3C%2FFONT%3E%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%3CP%3E%3CFONT%20size%3D%223%22%3E1%3C%2FFONT%3E%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%3CP%3E%3CFONT%20size%3D%223%22%3E1%3C%2FFONT%3E%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%3CP%3E%3CFONT%20size%3D%223%22%3E1%3C%2FFONT%3E%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%3CP%3E%3CFONT%20size%3D%223%22%3E3.18754633061527%3C%2FFONT%3E%3C%2FP%3E%0A%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3C%2FTBODY%3E%0A%3C%2FTABLE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CFONT%20size%3D%223%22%3EThe%20compression%20process%20moves%20the%20data%20behind%20the%20file%20forward%2C%20creating%20free%20space%20behind%20the%20file.%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3E%3CFONT%20size%3D%223%22%3EBy%20truncating%20the%20free%20space%20behind%20the%20created%20file%2C%20the%20database%20file%20size%20is%20reduced.%20If%20you%20compress%20a%20database%20or%20database%20file%20after%20rebuilding%20the%20index%2C%20the%20physically%20aligned%20data%20will%20be%20moved%20again%2C%20and%20it%20will%20not%20be%20physically%20aligned.%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CFONT%20size%3D%223%22%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22moematsuo_8-1634706145375.png%22%20style%3D%22width%3A%20400px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F318612i484BCA4DE5F89917%2Fimage-size%2Fmedium%3Fv%3Dv2%26amp%3Bpx%3D400%22%20role%3D%22button%22%20title%3D%22moematsuo_8-1634706145375.png%22%20alt%3D%22moematsuo_8-1634706145375.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CFONT%20size%3D%223%22%3EThis%20means%20that%20indexes%20that%20have%20been%20de-fragmented%20by%20rebuilding%20the%20indexes%20are%20fragmented%20again%20by%20compressing%20the%20database.%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CFONT%20size%3D%223%22%3E--------------%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%20style%3D%22margin%3A%200in%3B%22%3E%3CFONT%20size%3D%222%22%20color%3D%22%23808080%22%3EThis%20article%20corrects%20and%20re-posted%20the%20link%20break%20in%20the%20blog%20image%20below.%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%20style%3D%22margin%3A%200in%3B%22%3E%3CFONT%20size%3D%222%22%20color%3D%22%23808080%22%3E%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Farchive%2Fblogs%2Fjpsql%2Fdosdonts-7-rebuild-1239%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3EDO's%20DONT's%20%237%3A%20What%20you%20shouldn't%20do%20-%20Data%20file%20compression%20(SHRINK)%20after%20index%20rebuild%20(REBUILD)%20%7C%20Microsoft%20Docs%20%3C%2FA%3E%3C%2FFONT%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E
Co-Authors
Version history
Last update:
‎Oct 19 2021 10:19 PM
Updated by: