ROW vs PAGE 압축 비교 리포트 생성 스크립트

By | 2025-07-16

MSSQL에서 테이블 또는 인덱스에 대해 ROW 압축 vs PAGE 압축 효과를 비교하는 리포트 생성 스크립트는 공식 저장 프로시저인 sp_estimate_data_compression_savings 을 기반으로 작성할 수 있습니다.


✅ 📊 ROW vs PAGE 압축 비교 리포트 스크립트

아래는 테이블 내 모든 인덱스 및 파티션에 대해 압축 전후 공간 절약량을 계산하고, ROW / PAGE 압축 효과를 비교하는 T-SQL 스크립트입니다.

🔧 전체 테이블 대상 (파티션 전체 포함):

DECLARE @schema_name SYSNAME = 'dbo';
DECLARE @table_name SYSNAME = 'YourTableName';

IF OBJECT_ID('tempdb..#CompressionResults') IS NOT NULL
    DROP TABLE #CompressionResults;

CREATE TABLE #CompressionResults (
    Index_ID INT,
    Partition_Number INT,
    Compression_Type VARCHAR(10),
    Size_Current_KB BIGINT,
    Size_Estimated_KB BIGINT,
    Size_Saved_KB AS (Size_Current_KB - Size_Estimated_KB)
);

DECLARE @index_id INT, @partition_number INT, @sql NVARCHAR(MAX);

-- 인덱스와 파티션 정보를 루프
DECLARE cur CURSOR FOR
SELECT i.index_id, p.partition_number
FROM sys.indexes i
JOIN sys.partitions p ON i.object_id = p.object_id AND i.index_id = p.index_id
WHERE i.object_id = OBJECT_ID(QUOTENAME(@schema_name) + '.' + QUOTENAME(@table_name))
GROUP BY i.index_id, p.partition_number;

OPEN cur;
FETCH NEXT FROM cur INTO @index_id, @partition_number;

WHILE @@FETCH_STATUS = 0
BEGIN
    -- ROW 압축
    INSERT INTO #CompressionResults(Index_ID, Partition_Number, Compression_Type, Size_Current_KB, Size_Estimated_KB)
    EXEC sp_estimate_data_compression_savings
        @schema_name = @schema_name,
        @object_name = @table_name,
        @index_id = @index_id,
        @partition_number = @partition_number,
        @data_compression = 'ROW';

    -- PAGE 압축
    INSERT INTO #CompressionResults(Index_ID, Partition_Number, Compression_Type, Size_Current_KB, Size_Estimated_KB)
    EXEC sp_estimate_data_compression_savings
        @schema_name = @schema_name,
        @object_name = @table_name,
        @index_id = @index_id,
        @partition_number = @partition_number,
        @data_compression = 'PAGE';

    FETCH NEXT FROM cur INTO @index_id, @partition_number;
END

CLOSE cur;
DEALLOCATE cur;

-- 결과 리포트 출력
SELECT 
    Index_ID,
    Partition_Number,
    Compression_Type,
    Size_Current_KB,
    Size_Estimated_KB,
    Size_Saved_KB,
    CAST(100.0 * Size_Saved_KB / NULLIF(Size_Current_KB, 0) AS DECIMAL(5,2)) AS Saved_Percent
FROM #CompressionResults
ORDER BY Partition_Number, Index_ID, Compression_Type;

📋 결과 예시 (출력)

Index_IDPartition_NumberCompression_TypeSize_Current_KBSize_Estimated_KBSize_Saved_KBSaved_Percent
11ROW100000850001500015.00%
11PAGE100000600004000040.00%

✅ 추가 팁

  • 이 스크립트는 클러스터드/비클러스터드 인덱스 모두 포함
  • 테이블이 파티션되어 있지 않아도 정상 작동
  • sp_estimate_data_compression_savings읽기 전용, 실제 데이터를 변경하지 않음

📌 향후 자동화에 활용

  • 위 결과를 Excel로 저장 → 용량 비교 보고서 작성
  • 특정 조건(압축 절감률 ≥ 30%) 이상만 실제 압축 적용하는 자동화 가능
  • PowerShell 또는 SSIS에서 이 스크립트를 호출하여 리포트 자동화 가능

답글 남기기

이메일 주소는 공개되지 않습니다. 필수 필드는 *로 표시됩니다