设为首页 收藏本站
查看: 1059|回复: 0

[经验分享] SQL Server 2016 COMPRESS 和 DECOMPRESS 函数

[复制链接]

尚未签到

发表于 2018-6-25 10:13:11 | 显示全部楼层 |阅读模式
  SQL Server 2016 COMPRESS 和 DECOMPRESS 函数
  
  英文原文:
  https://www.simple-talk.com/blogs/2015/12/24/compresss-and-decompress-in-sql-server-2016/
  COMPRESS和DECOMPRESS适用于SQL Server 2016和Azure SQL Database。
  COMPRESS介绍
  使用GZIP算法压缩输入表达式。压缩结果为varbinary(max)类型的字节数组。
  语法
  COMPRESS(expression)
  参数
  expression是一个nvarchar(n), nvarchar(max), varchar(n), varchar(max), varbinary(n), varbinary(max), char(n), nchar(n), or binary(n) 表达式。
  注意
  压缩数据不能被索引
  示例
  A.在插入数据到表时压缩数据
INSERT INTO player (name, surname, info )  
VALUES (N'Ovidiu', N'Cracium',
  
COMPRESS(N'{"sport":"Tennis","age": 28,"rank":1,"points":15258, turn":17}'));
  
INSERT INTO player (name, surname, info )
  
VALUES (N'Michael', N'Raheem', compress(@info));
  B.归档被删除行的压缩数据
DELETE player  
WHERE datemodified < @startOfYear
  
OUTPUT id, name, surname datemodifier, COMPRESS(info)
  
INTO dbo.inactivePlayers ;
  DECOMPRESS介绍
  使用GZIP算法解压缩输入表达式。返回结果为varbinary(max)类型。
  语法
  DECOMPRESS(expression)
  参数
  expression是一个varbinary(n), varbinary(max), or binary(n)
  返回类型
  用户应该显式转换结果为目标类型
  示例
  A.在查询时解压缩数据
SELECT _id, name, surname, datemodified,  
CAST(DECOMPRESS(info) AS NVARCHAR(MAX)) AS info
  
FROM player;
  B.使用计算列显示压缩数据
CREATE TABLE (  
_id int primary key identity,
  
name nvarchar(max),
  
surname nvarchar(max),
  
info varbinary(max),
  
info_json as CAST(decompress(info) as nvarchar(max))
  
);
  COMPRESS和DECOMPRESS实战
  SQL Server 2016的新特性可以使用COMPRESS和DECOMPRESS函数在DML(INSERT/SELECT/UPDATE)操作中压缩和解压缩列。
  我们注意到压缩列不能被索引。换句话说,如果不使用该列作为搜索条件,压缩或许是一个好的选择。
  让我们看一个这些函数的示例和压缩结果。
-- Create the first sample table  
create table WithoutCompress
  
(id int identity(1,1),
  
longfield varchar(max) )
  
go
  

  
-- Optimize large values storage (see more about this here )
  
exec sp_tableoption 'WithoutCompress' , 'Large Value Types out of row',true
  
go
  

  
-- Insert a thousand of big records
  
insert into WithoutCompress (longfield) values (replicate('this is a compress example',1000))
  
go 1000
  

  
-- Check the total pages of the table
  
select allocation_unit_type_desc,page_type_desc, count(*) as TotalPages
  
from sys.dm_db_database_page_allocations (DB_ID('test'),OBJECT_ID('WithoutCompress'),0,1,'DETAILED')
  
group by allocation_unit_type_desc,page_type_desc
  
order by allocation_unit_type_desc
DSC0000.gif

  我们有1012个TEXT_MIX_PAGE,除了其他类型的页。
-- Create the 2nd table  
create table WithCompress
  
(id int identity(1,1),
  
longfield varbinary(max) )
  
go
  压缩信息的列为varbinary(max)
-- Optimize large values storage  
exec sp_tableoption 'WithCompress' , 'Large Value Types out of row',true
  
go
  

  
-- Insert a thousand large records
  
insert into WithCompress (longfield) values (Compress(replicate('this is a compress example',1000)))
  
go 1000
  

  
-- Check the total pages of the 2nd table
  
select allocation_unit_type_desc,page_type_desc, count(*) as TotalPages
  
from sys.dm_db_database_page_allocations (DB_ID('teste'),OBJECT_ID('WithCompress'),0,1,'DETAILED')
  
group by allocation_unit_type_desc,page_type_desc
  
order by allocation_unit_type_desc
DSC0001.gif

  现在我们只有24个TEXT_MIX_PAGE。当然这个比率依赖于表中数据。如果你的数据已经被压缩,如JPG文件,例如,这个比率不会很高,只会在压缩和解压缩过程中消耗CPU。
  当我们查询表时可以解压缩信息。示例查询为:
select top 10 decompress(longfield) as longfield from WithCompress
DSC0002.gif

  正如上图所见,DECOMPRESS函数的结果是varbinary(max)类型。我们需要转换结果来看到原始信息。让我们添加CAST到查询中:
select top 10 cast(decompress(longfield) as varchar(max)) as longfield  
from WithCompress
DSC0003.gif

  压缩使用GZIP算法,因此我们可以在客户端应用程序中解压缩信息,而不是查询中。varbinary列匹配完整性框架中的byte[]。我们可以使用GZipStream在.NET中解压缩信息。
  这是一个解压缩列和在一个Windows表单textbox中显示结果的示例代码:
var rec = (from x in ctx.WithCompresses  
select x).FirstOrDefault();
  
MemoryStream ms = new MemoryStream(rec.longfield);
  
GZipStream gz = new GZipStream(ms, CompressionMode.Decompress);
  
StreamReader sr = new StreamReader(gz);
  
textBox2.Text = sr.ReadToEnd();

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-530462-1-1.html 上篇帖子: SCCM2016 Boot Configuration Data(BCD) 不包含可用的OS 下篇帖子: SharePoint 2016文档库只能创建单一文件故障
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表