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