码迷,mamicode.com
首页 > 数据库 > 详细

TSql Row Compression

时间:2015-06-15 20:20:20      阅读:116      评论:0      收藏:0      [点我收藏+]

标签:

第一部分:概念理解

1,Row Compression 是将固定长度类型存储为可变长度存储类型,对于开发人员,Row Compression 是透明的,不需要更改应用程序。

1.1 对于字符型

  Char(200),这是一个固定长度的数据类型,但是在实际存储的时候,可能不会存储200个字符。在物理存储数据时,sql server后补空格以达到200个字符。如果将其转换为varchar(200),不需要后补空格,就能节省存储空间。

1.2 对于数值型

  在SQL Server 2005 SP2之前,decimal类型总是以固定数据存储的。根据值的精确度,每个decimal值都需要5到17字节的空间。新引入的Vardecimal存储格式是把decimal值以一个可变长度的格式进行存储。这种格式把小数值前后的零都去除,可以减少存储所需的空间。

  SOL Server 2008数据压缩扩展了这个功能,对所有固定长度的数据类型都进行了处理,包括integer、char和float。现在数据不是以固定大小的字节进行存储,而是用最小所需的字节。开发者不需要修改数据类型,只需要启用行压缩功能,SOL Server 2008就会使用最小的可变数据类型来存储数据。

1.3 行压缩无法处理XML、BLOB和MAX数据类型

第二部分:对表启用行压缩

2,启用表的 row compression的两种方式

2.1 在创建表的时候,启用row compression

CREATE TABLE dbo.TestCompression
(
    id int not null identity(1,1),
    jd CHAR(2000) not null
)
with( DATA_COMPRESSION=ROW)

2.2 对没有启用row compression的表启用row compression

--更改压缩选项,更新为Row,并对表数据进行压缩
ALTER TABLE dbo.TestCompression REBUILD
WITH (DATA_COMPRESSION=ROW) 

3,查看“数据压缩”节省的存储空间

EXEC SYS.sp_estimate_data_compression_savings
       @SCHEMA_NAME=DBO,
       @OBJECT_NAME=TestCompression,
       @INDEX_ID=NULL,
       @PARTITION_NUMBER=NULL,
       @DATA_COMPRESSION=ROW

4,查看表已经使用的存储空间

--查看已使用的存储空间
EXEC sp_spaceused TestCompression

5,示例代码如下

CREATE TABLE dbo.TestCompression
(
    id int not null identity(1,1),
    jd CHAR(2000) not null
)
with( DATA_COMPRESSION=ROW)

--更改压缩选项,去除压缩选项
ALTER TABLE dbo.TestCompression REBUILD
WITH (DATA_COMPRESSION=None) 

declare @i int=0
while @i<100
begin
    INSERT TestCompression(jd)
    VALUES(REPLICATE(a,50))

    set @i=@i+1
end

EXEC SYS.sp_estimate_data_compression_savings
       @SCHEMA_NAME=DBO,
       @OBJECT_NAME=TestCompression,
       @INDEX_ID=NULL,
       @PARTITION_NUMBER=NULL,
       @DATA_COMPRESSION=ROW

--查看已使用的存储空间
EXEC sp_spaceused TestCompression

--更改压缩选项,并对表进行行压缩
ALTER TABLE TestCompression REBUILD
WITH (DATA_COMPRESSION=row) 


--查看已使用的存储空间
EXEC sp_spaceused TestCompression

第三部分:对索引压缩

6,修改表结果,增加一列,在该列上创建索引

--增加一列
alter table dbo.TestCompression
add testdate char(200) default convert(varchar,getdate(),112)

--创建索引
create index idx_TestCompression_1 on TestCompression(testdate)

7,查看索引压缩前使用的存储空间

--查看已使用的存储空间
EXEC sp_spaceused TestCompression

技术分享

8,对索引进行行压缩

--对索引进行压缩
ALTER INDEX idx_TestCompression_1 ON [dbo].TestCompression 
REBUILD PARTITION = ALL 
WITH ( DATA_COMPRESSION = row  )

9,查看压缩后,索引占用的存储空间

--查看已使用的存储空间
EXEC sp_spaceused TestCompression

技术分享

10,示例代码如下

--增加一列
alter table dbo.TestCompression
add testdate char(200) default convert(varchar,getdate(),112)

--创建索引
create index idx_TestCompression_1 on TestCompression(testdate)

--查看已使用的存储空间
EXEC sp_spaceused TestCompression

--对索引进行压缩
ALTER INDEX idx_TestCompression_1 ON [dbo].TestCompression 
REBUILD PARTITION = ALL 
WITH ( DATA_COMPRESSION = row  )

--查看已使用的存储空间
EXEC sp_spaceused TestCompression


ps,完整代码

CREATE TABLE dbo.TestCompression
(
    id int not null identity(1,1),
    jd CHAR(2000) not null
)
with( DATA_COMPRESSION=ROW)

--更改压缩选项
ALTER TABLE dbo.TestCompression REBUILD
WITH (DATA_COMPRESSION=None) 

declare @i int=0
while @i<100
begin
    INSERT TestCompression(jd)
    VALUES(REPLICATE(a,50))

    set @i=@i+1
end

EXEC SYS.sp_estimate_data_compression_savings
       @SCHEMA_NAME=DBO,
       @OBJECT_NAME=TestCompression,
       @INDEX_ID=NULL,
       @PARTITION_NUMBER=NULL,
       @DATA_COMPRESSION=ROW

--查看已使用的存储空间
EXEC sp_spaceused TestCompression


--更改压缩选项
ALTER TABLE TestCompression REBUILD
WITH (DATA_COMPRESSION=row) 


--查看已使用的存储空间
EXEC sp_spaceused TestCompression


--增加一列
alter table dbo.TestCompression
add testdate char(200) default convert(varchar,getdate(),112)

--创建索引
create index idx_TestCompression_1 on TestCompression(testdate)

--查看已使用的存储空间
EXEC sp_spaceused TestCompression

--对索引进行压缩
ALTER INDEX idx_TestCompression_1 ON [dbo].TestCompression 
REBUILD PARTITION = ALL 
WITH ( DATA_COMPRESSION = row  )


--查看已使用的存储空间
EXEC sp_spaceused TestCompression

 

TSql Row Compression

标签:

原文地址:http://www.cnblogs.com/ljhdo/p/4578930.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!