Sql Server Data compression 预估和选择,以及查看成功压缩的数据页

Sql Server提供两种数据压缩的方式:row压缩和page压缩。两种压缩的内部原理暂且不论,只要知道压缩率越高,节省的disk space 更多即可。sql server 提供多种工具,供DBA查看压缩的效率。

1,查看表的压缩类型

在sys.partitions中的两个字段data_compression 和data_compression_desc ,Indicates the state of compression for each partition。

使用sys.allocation_units 查看 total_Pages, Used_pages 等信息

select p.partition_id,object_name(p.object_id) as ObjectName,
    p.index_id,
    p.rows,
    p.data_compression,
    p.data_compression_desc,
    au.Type,
    au.Type_desc,
    au.total_pages,
    au.used_pages,
    au.data_pages
from sys.partitions p
inner join sys.allocation_units au
    on p.partition_id=au.container_id
where p.object_id=object_id(‘[dbo].[UserCDC]‘,N‘U‘)

2,修改表的压缩类型

alter table [dbo].[UserCDC]
rebuild with(data_compression=row)

3,查看 page 压缩成功的pages 数量

sys.dm_db_index_operational_stats 函数的返回数据列中有


page_compression_attempt_count


bigint


Number of pages that were evaluated for PAGE level compression for specific partitions of a table, index, or indexed view. Includes pages that were not compressed because significant savings could not be achieved. Always 0 for  columnstore index.


page_compression_success_count


bigint


Number of data pages that were compressed by using PAGE compression for specific partitions of a table, index, or indexed view. Always 0 for  columnstore index.

Syntax

sys.dm_db_index_operational_stats (
    { database_id | NULL | 0 | DEFAULT }
  , { object_id | NULL | 0 | DEFAULT }
  , { index_id | 0 | NULL | -1 | DEFAULT }
  , { partition_number | NULL | 0 | DEFAULT }
)
select ios.database_id,ios.object_id,ios.index_id,ios.page_compression_attempt_count,ios.page_compression_success_count
from sys.dm_db_index_operational_stats(db_id(),OBJECT_ID(‘[dbo].UserCDC‘),1,null) as ios

4,预估压缩效率

Returns the current size of the requested object and estimates the object size for the requested compression state. Compression can be evaluated for whole tables or parts of tables. This includes heaps, clustered indexes, nonclustered indexes, indexed views, and table and index partitions. The objects can be compressed by using row compression or page compression. If the table, index, or partition is already compressed, you can use this procedure to estimate the size of the table, index, or partition if it is recompressed.

To estimate the size of the object if it were to use the requested compression setting, this stored procedure samples the source object and loads this data into an equivalent table and index created in tempdb. The table or index create in tempdb is then compressed to the requested setting and the estimated compression savings is computed.

syntax

sp_estimate_data_compression_savings
     [ @schema_name = ] ‘schema_name‘
   , [ @object_name = ] ‘object_name‘
   , [@index_id = ] index_id
   , [@partition_number = ] partition_number
   , [@data_compression = ] ‘data_compression‘
[;]
exec sys.sp_estimate_data_compression_savings @schema_name=‘dbo‘,
                @object_name=‘UserCDC‘,
                @index_id=1,
                @partition_number=1,
                @data_compression =‘row‘

Column name


Data type


Description


object_name


sysname


Name of the table or the indexed view.


schema_name


sysname


Schema of the table or indexed view.


index_id


int


Index ID of an index:

0 = Heap

1 = Clustered index

> 1 = Nonclustered index


partition_number


int


Partition number. Returns 1 for a nonpartitioned table or index.


size_with_current_compression_setting (KB)


bigint


Size of the requested table, index, or partition as it currently exists.


size_with_requested_compression_setting (KB)


bigint


Estimated size of the table, index, or partition that uses the requested compression setting; and, if applicable, the existing fill factor, and assuming there is no fragmentation.


sample_size_with_current_compression_setting (KB)


bigint


Size of the sample with the current compression setting. This includes any fragmentation.


sample_size_with_requested_compression_setting (KB)


bigint


Size of the sample that is created by using the requested compression setting; and, if applicable, the existing fill factor and no fragmentation.

Remarks   

Use sp_estimate_data_compression_savings to estimate the savings that can occur when you enable a table or partition for row or page compression. For instance if the average size of the row can be reduced by 40 percent, you can potentially reduce the size of the object by 40 percent. You might not receive a space savings because this depends on the fill factor and the size of the row. For example, if you have a row that is 8000 bytes long and you reduce its size by 40 percent, you can still fit only one row on a data page. There is no savings.

If the results of running sp_estimate_data_compression_savings indicate that the table will grow, this means that many rows in the table use almost the whole precision of the data types, and the addition of the small overhead needed for the compressed format is more than the savings from compression. In this rare case, do not enable compression.

If a table is enabled for compression, use sp_estimate_data_compression_savings to estimate the average size of the row if the table is uncompressed.

An (IS) lock is acquired on the table during this operation. If an (IS) lock cannot be obtained, the procedure will be blocked. The table is scanned under the read committed isolation level.

If the requested compression setting is same as the current compression setting, the stored procedure will return the estimated size with no data fragmentation and using the existing fill factor.

If the index or partition ID does not exist, no results are returned.

参考文档:

https://msdn.microsoft.com/zh-cn/library/ms175012.aspx
https://msdn.microsoft.com/zh-cn/library/cc280574.aspx
https://msdn.microsoft.com/en-us/library/ms174281.aspx
https://msdn.microsoft.com/en-us/library/ms189792(v=sql.110).aspx

页压缩的实现

https://msdn.microsoft.com/zh-cn/library/cc280464(v=sql.120).aspx

时间: 2024-11-07 21:22:08

Sql Server Data compression 预估和选择,以及查看成功压缩的数据页的相关文章

SQL SERVER ->> Data Compression

最近做了一个关于数据压缩的项目,要把整个SQL SERVER服务器下所有的表对象要改成页压缩.于是趁此机会了解了一下SQL SERVER下压缩技术. 这篇文章几乎就是完全指导手册了 https://technet.microsoft.com/en-us/library/dd894051(v=sql.100).aspx 当然这里还有技术wiki page https://msdn.microsoft.com/en-us/library/cc280449.aspx 那看了这么多,这里总结一下: 1)

Sql Server之旅——第六站 使用winHex利器加深理解数据页

这篇我来介绍一个winhex利器,这个工具网上有介绍,用途大着呢,可以用来玩数据修复,恢复删除文件等等....它能够将一个file解析成 hex形式,这样你就可以对hex进行修改,然后你就可以看到修复后的结果,为什么要在sqlserver系列中说这个呢???很简单呀,sqlserver的DB本 质上也是一个mdf文件,对吧,既然是文件,我就可以利用winhex对它进行随意的修改,然后你也知道sqlserver的数据都是以数据页的形式封装的, 那我就可以修改它的数据页,对不对,这样我就可以随便改变

SQL Server Data Tools 开发 微软Dynamics 2015 报表开发 入门

打开 SQL Server Data Tools 界面如下: 其实就是Visual studio 新建一个项目,我们还是基于.net framework 4.0吧 新建数据源: 可以点测试连接, 1ms的反应时间啊,很快的. 下面就是开始 添加新报表了,右键 报表, 选择第一个菜单, "添加新报表" 本人就选了CRM中产品的表, select name,price,productionumber from productbase 可以选择表格,或者矩阵.具体可以点一下,看是什么样式的呢

SQL Server 执行计划利用统计信息对数据行的预估原理以及SQL Server 2014中预估策略的改变

前提  本文仅讨论SQL Server查询时, 对于非复合统计信息,也即每个字段的统计信息只包含当前列的数据分布的情况下, 在用多个字段进行组合查询的时候,如何根据统计信息去预估行数的. 利用不同字段的统计信息做数据行数预估的算法原理,以及SQL Server 2012和SQL Server 2014该算法的差异情况, 这里暂时不涉及复合统计信息,暂不涉及统计信息的更新策略及优化相关话题,以及其他SQL Server版本计算方式. 统计信息是什么 简单说就是对某些字段的数据分布的一种描述,让SQ

接着上一篇,把刚才SQL Server Data Tools 开发的报表复制到 重新部署到微软Dynamics CRM 系统中

报表开发好了,要应用到系统中呢,下面教大家如何把Visual studio或者SQL Server Data Tools 开发的报表导入到系统中. 进入, 销售---报表 ,点 新建 点 "浏览" 定位到报表保存的文件夹.一般在我的文档中 Projects 点 "保存并关闭" 好了,看到了吧,分配权限就可以使用了,是不是很简单啊. 下面是逆向操作,报表导出. 保存即可以.

报错:此版本的SQL Server Data Tools与此计算机中安装的数据库运行时组件不兼容

在Visual Studio 2012中使用Entity Framework,根据模型生成数据库时,报如下错误: 无法在自定义编辑器中打开Transact-SQL文件此版本的SQL Server Data Tools与此计算机中安装的数据库运行时组件不兼容 解决办法:下载"Server Data Tools for Visual Studio 2012" ,并安装,重新启动Visual Studio 2012.

Microsoft SQL Server Data Tools - Business Intelligence for Visual Studio 2013 http://www.microsoft.com/en-us/download/details.aspx?id=42313

Microsoft SQL Server Data Tools - Business Intelligence for Visual Studio 2013 http://www.microsoft.com/en-us/download/details.aspx?id=42313

从性能的角度谈SQL Server聚集索引键的选择

简介 在SQL Server中,数据是按页进行存放的.而为表加上聚集索引后,SQL Server对于数据的查找就是按照聚集索引的列作为关键字进行了.因此对于聚集索引的选择对性能的影响就变得十分重要了.本文从旨在从性能的角度来谈聚集索引的选择,但这仅仅是从性能方面考虑.对于有特殊业务要求的表,则需要按实际情况进行选择. 聚集索引所在的列或列的组合最好是唯一的 这个原因需要从数据的存放原理来谈.在SQL Server中,数据的存放方式并不是以行(Row)为单位,而是以页为单位.因此,在查找数据时,S

SQL Server -- 下载 SQL Server Data Tools (SSDT)

https://docs.microsoft.com/zh-cn/sql/ssdt/download-sql-server-data-tools-ssdt SQL Server Data Tools 是一款可免费下载的现×××发工具,用于生成 SQL Server 关系数据库.Azure SQL 数据库.Integration Services 包.Analysis Services 数据模型和 Reporting Services 报表. 使用 SSDT,你可以设计和部署任何 SQL Serv