服务器之家:专注于服务器技术及软件下载分享
分类导航

Mysql|Sql Server|Oracle|Redis|MongoDB|PostgreSQL|Sqlite|DB2|mariadb|Access|数据库技术|

服务器之家 - 数据库 - 数据库技术 - 为什么你不要收缩数据库文件(国外翻译)

为什么你不要收缩数据库文件(国外翻译)

2021-11-02 16:26潇湘隐者博客 数据库技术

这几天查看了很多关于SQL SERVER收缩数据文件方面的文章,准备写一篇关于收缩日志方面的文章,但是突然有种冲动将看过经典的文章翻译出来,需要的朋友可以参考下

前言,这几天查看了很多关于sql server收缩数据文件方面的文章,准备写一篇关于收缩日志方面的文章,但是突然有种冲动将看过经典的文章翻译出来,下面这篇文章是翻译的是paul randal – “why you should not shrink your data files”。有些比较难以翻译、清晰的地方,我会贴上原文。好了,不啰嗦了,直接看下面的翻译吧。 

  我最大的一个热点问题是关于收缩数据文件,虽然在微软的时候,我自己写了相关收缩数据文件代码,我再也没有机会去重写它,让它操作起来更方便。我真的不喜欢收缩。

  现在,不要混淆了收缩事务日志文件和收缩数据文件,当事务日志文件的增长失控或为了移除过多的vlf碎片(和看到金佰利的优秀文章),然而,收缩事务日志数据文件不要频繁使用(罕见的操作)并且不应是你执行定期维护计划的一部分。

  收缩数据文件应该执行得甚至更少。这就是为什么——数据文件收缩导致产生了大量索引碎片,让我用一个简单并且你可以运行的脚步来演示。下面的脚本将会创建一个数据文件,创建一个10mb大小的“filler”表,一个10mb大小的“production”聚簇索引,然后分析新建的聚集索引的碎片情况。 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
use [master];
go
 
if databasepropertyex(n'dbmaint2008', n'version') is not null
  drop database [dbmaint2008];
go
 
create database dbmaint2008;
go
use [dbmaint2008];
go
 
set nocount on;
go
 
-- create the 10mb filler table at the 'front' of the data file
create table [fillertable](
  [c1] int identity,
  [c2] char (8000) default 'filler');
go
 
-- fill up the filler table
insert into [fillertable] default values;
go 1280
 
-- create the production table, which will be 'after' the filler table in the data file
create table [prodtable](
  [c1] int identity,
  [c2] char (8000) default 'production');
create clustered index [prod_cl] on [prodtable]([c1]);
go
 
insert into [prodtable] default values;
go 1280
 
-- check the fragmentation of the production table
select
  [avg_fragmentation_in_percent]
from sys.dm_db_index_physical_stats(
  db_id(n'dbmaint2008'), object_id(n'prodtable'), 1, null, 'limited');
go

执行结果如下

为什么你不要收缩数据库文件(国外翻译)

聚集索引的逻辑碎片在收缩数据文件前大约接近0.4%。[但是我测试结果是0.54%,如上图所示,不过也算是接近0.4%]

现在我删除filter表,运行收缩数据文件命令后,重新分析聚集索引的碎片化。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
-- drop the filler table, creating 10mb of free space at the 'front' of the data file
drop table [fillertable];
go
 
-- shrink the database
dbcc shrinkdatabase([dbmaint2008]);
go
 
-- check the index fragmentation again
select
  [avg_fragmentation_in_percent]
from sys.dm_db_index_physical_stats(
  db_id(n'dbmaint2008'), object_id(n'prodtable'), 1, null, 'limited');
go

下面是我的执行结果,作者执行结果,请看原文:

为什么你不要收缩数据库文件(国外翻译)

原文:

wow! after the shrink, the logical fragmentation is almost 100%. the shrink operation *completely* fragmented the index, removing any chance of efficient range scans on it by ensuring the all range-scan readahead i/os will be single-page i/os.

译文:

哇,真是恐怖!数据文件收缩后,索引的逻辑碎片几乎接近100%,收缩数据文件导致了索引的完全碎片化。消除了任何关于它的有效范围扫描的机会,确保所有执行提前读范围扫描的 i/o 在单页的 i/o操作
为什么会这样呢? 当单个数据文件收缩操作一次后,它会用gam位图索引找出数据文件中分配最高的页,然后尽可能的向前移动到文件能够移动的地方,就这样子,在上面的例子中,它完全反转了聚集索引,让它从非碎片化到完全碎片化。
同样的代码用于dbcc shrinkfile, dbcc shrinkdatabase,以及自动收缩,他们同样糟糕,就像索引的碎片化,数据文件的收缩同样产生了大量的i/o操作,耗费大量的cpu资源,并且生成了*load*事务日志,因为任何操作都会全部记录下来。
数据文件收缩决不能作为定期维护的一部分,你决不能启用“自动收缩”属性,我尝试把它从sql 2005和sql 2008产品中移除,它还存在的唯一原因是为了更好的向前兼容,不要掉入这样的陷阱:创建一个维护计划,重新生成所有索引,然后尝试回收重建索引耗费的空间采取收缩数据文件 — — 这就是你做的生成了大量事务日志,但实质没有提高性能的零和游戏。
所以,你为什么要运行一个收缩呢,?举例来说,如果你把一个相当大的数据库删除了相当大的比例,该数据库不太可能增长,或者你需要转移一个数据库文件前先清空数据文件?

译文:

我很想推荐的方法如下:

创建一个新的文件组
将所有受影响的表和索引移动到一个新的文件组用create index ... with (drop_existing=on)的脚本,在移动表的同时,删除表中的碎片。
删掉那些你准备收缩的旧文件组,你反正要收缩(或缩小它的方式下来,如果它的主文件组)。
基本上你需要提供一些更多的空间,才可以收缩的旧文件,但它是一个更清晰的设置。

原文:

the method i like to recommend is as follows:

create a new filegroup
move all affected tables and indexes into the new filegroup using the create index … with (drop_existing = on) on syntax, to move the tables and remove fragmentation from them at the same time
drop the old filegroup that you were going to shrink anyway (or shrink it way down if its the primary filegroup)
basically you need to provision some more space before you can shrink the old files, but it's a much cleaner mechanism.

如果你完全没有选择需要收缩日志文件,请注意这个操作会导致索引的碎片化,你应该在收缩数据文件采取一些步骤消除它可能导致的性能问题,唯一的方式是用dbcc indexdefpage或 alter index ...reorganize消除索引的碎片不要引起数据文件的增长,这些命令要求扩展空间8kb的页代替重建一个新的索引在索引重建操作中。
底线 — — 尽量避免不惜一切代价运行数据文件收缩

所以,还在用作业定期收缩数据文件或数据库开启了“自动收缩”属性的朋友们,请及时纠正你们的错误认识吧!

支持原著,也希望大家支持我辛苦的翻译劳动,请加上链接潇湘隐者博客。

原文链接:http://www.cnblogs.com/kerrycode/archive/2013/06/04/3116339.html

延伸 · 阅读

精彩推荐