<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Data Compression Procedure for All Tables - MSSQL Query</title>
	<atom:link href="https://mssqlquery.com/tag/data-compression-procedure-for-all-tables/feed" rel="self" type="application/rss+xml" />
	<link>https://mssqlquery.com</link>
	<description>MSSQL and TSQL Programming and TSQL Examples</description>
	<lastBuildDate>Fri, 07 Apr 2023 19:30:19 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.3.1</generator>

<image>
	<url>https://mssqlquery.com/wp-content/uploads/2023/06/cropped-mssql-query-icon-32x32.png</url>
	<title>Data Compression Procedure for All Tables - MSSQL Query</title>
	<link>https://mssqlquery.com</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>Data Compression Procedure for All Tables in SQL Server</title>
		<link>https://mssqlquery.com/data-compression-procedure-for-all-tables-in-sql-server</link>
		
		<dc:creator><![CDATA[Yavuz Selim Kart]]></dc:creator>
		<pubDate>Tue, 29 Mar 2022 17:13:24 +0000</pubDate>
				<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[Stored Procedures]]></category>
		<category><![CDATA[Data Compression]]></category>
		<category><![CDATA[Data Compression Procedure]]></category>
		<category><![CDATA[Data Compression Procedure for All Tables]]></category>
		<guid isPermaLink="false">https://mssqlquery.com/?p=802</guid>

					<description><![CDATA[<p>Hello everyone, In this article, I will give information about data compression procedure for all tables in SQL Server. There are two types of data compression. Row Level Data Compression: A compression method that converts fixed-length data types to variable-length data types and frees up free space. It also saves&#46;&#46;&#46;</p>
<p>The post <a href="https://mssqlquery.com/data-compression-procedure-for-all-tables-in-sql-server">Data Compression Procedure for All Tables in SQL Server</a> first appeared on <a href="https://mssqlquery.com">MSSQL Query</a>.</p>]]></description>
										<content:encoded><![CDATA[<p>Hello everyone,</p>
<p>In this article, I will give information about data compression procedure for all tables in SQL Server.</p>
<p>There are two types of data compression.</p>
<p><strong>Row Level Data Compression:</strong> A compression method that converts fixed-length data types to variable-length data types and frees up free space. It also saves additional space by ignoring zero and null values. In contrast, it can fit more rows in a single data sheet.</p>
<p><strong>Page-Level Data Compression:</strong> A compression method that starts with row-level data compression and then adds a prefix and a dictionary compression for data pages.</p>
<p>The following procedure expects either <strong>ROW</strong> or <strong>PAGE</strong> parameter. Which compression type you want to use, you can run that parameter by writing it to the procedure.</p>
<pre class="line-numbers"><code class="language-sql">CREATE PROCEDURE DataCompressionProcedureforAllTables 
(@compression_method CHAR(4))
AS
SET NOCOUNT ON;
BEGIN
    DECLARE @schema_name sysname,
            @table_name sysname;
    CREATE TABLE #compress_report_tb
    (
        ObjName sysname,
        schemaName sysname,
        indx_ID INT,
        partit_number INT,
        size_with_current_compression_setting BIGINT,
        size_with_requested_compression_setting BIGINT,
        sample_size_with_current_compression_setting BIGINT,
        sample_size_with_requested_compression_setting BIGINT
    );
    DECLARE c_sch_tb_crs CURSOR FOR
    SELECT TABLE_SCHEMA,
           TABLE_NAME
    FROM INFORMATION_SCHEMA.TABLES
    WHERE TABLE_TYPE LIKE 'BASE%'
          AND TABLE_CATALOG = UPPER(DB_NAME());
    OPEN c_sch_tb_crs;
    FETCH NEXT FROM c_sch_tb_crs
    INTO @schema_name,
         @table_name;
    WHILE @@Fetch_Status = 0
    BEGIN
        INSERT INTO #compress_report_tb
        EXEC sp_estimate_data_compression_savings @schema_name = @schema_name,
                                                  @object_name = @table_name,
                                                  @index_id = NULL,
                                                  @partition_number = NULL,
                                                  @data_compression = @compression_method;
        FETCH NEXT FROM c_sch_tb_crs
        INTO @schema_name,
             @table_name;
    END;
    CLOSE c_sch_tb_crs;
    DEALLOCATE c_sch_tb_crs;
    SELECT schemaName AS [schema_name],
           ObjName AS [table_name],
           AVG(size_with_current_compression_setting) AS avg_size_with_current_compression_setting,
           AVG(size_with_requested_compression_setting) AS avg_size_with_requested_compression_setting,
           AVG(size_with_current_compression_setting - size_with_requested_compression_setting) AS avg_size_saving
    FROM #compress_report_tb
    GROUP BY schemaName,
             ObjName
    ORDER BY schemaName ASC,
             avg_size_saving DESC;
    DROP TABLE #compress_report_tb;
END;
SET NOCOUNT OFF;


--Usage 
--Run after selecting the relevant database. Otherwise, the responsibility belongs to you.

EXEC DataCompressionProcedureforAllTables @compression_method= 'PAGE';
GO</code></pre>
<p>Good luck to everyone in business and life.</p>
<div class='epvc-post-count'><span class='epvc-eye'></span>  <span class="epvc-count"> 146</span><span class='epvc-label'> Views</span></div><p>The post <a href="https://mssqlquery.com/data-compression-procedure-for-all-tables-in-sql-server">Data Compression Procedure for All Tables in SQL Server</a> first appeared on <a href="https://mssqlquery.com">MSSQL Query</a>.</p>]]></content:encoded>
					
		
		
			</item>
	</channel>
</rss>
