指定压缩类型

最近更新时间: 2025-10-11 18:10:00

postgres=# create table t_col_compress
(
f1 int encoding(compress_method='delta'),
f2 varchar(32)ENCODING(compress_method='zlib',compress_level=9) ,
f3 date ENCODING(compress_method='zlib',compress_level=9) 
) with(orientation='column');
CREATE TABLE
postgres=# \d+ t_col_compress         
                                      Table"public.t_col_compress"
 Column |         Type          | Collation | Nullable | Default |Storage  | Stats target | Description 
--------+-----------------------+-----------+----------+---------+----------+--------------+-------------
 f1     |integer               |           |          |         | plain    |              | 
 f2     |character varying(32) |           |          |        | extended |              | 
 f3     |date                  |           |          |         | plain    |              | 
Options: orientation=column

postgres=#

指定表列字段的压缩方式及压缩等级创建表 目前支持压缩方式compress_method包括(delta、zstd、zlib、rle、bitpack),compress_level表示缩级别;压缩级别越大,压缩比越大,压缩时间越长,压缩级别越高越消耗cpu。 轻量压缩为自研实现算法,包括rle、delta、bitpack,他们均只有一种压缩级别,且只能压缩数值类型。 其中rle主要针对大量重复的数据,比如11111122222333111111进行压缩。delta主要针对递增或者递减的数据,比如固定的时间类型(今天明天后天),或者数值类型12345678进行压缩。bitpack针对数值比较小的数据,比如创建表的时候是integer,但实际存储的数值只是1-100。 创建表的时候指定压缩类型,但是实际存储的时候有没有用到指定的压缩类型是会自动适应调整的,有写场景可能导致最终存储没有使用压缩,如:压缩算法使用不当,比如针对text类型使用delta。 约束数据对比,100万行记录。

postgres=# \dt+
                                List ofrelations
 Schema |            Name             | Type  | Owner |   Size    | Description 
--------+-----------------------------+-------+-------+------------+-------------
 public | t_col_compress              | table | tbase | 104 MB     | 
 public | t_col_test                  | table | tbase | 119 MB     | 
 public | t_row_test                  | table | tbase | 97 MB      |

下面对比一下重复率比较高的,测试表如下:

  • 表定义 表定义
行存 列存 列存(压缩)
create table t_row_normal
(
f1 int,
f2 text ,
f3 text ,
f4 text ,
f5 text ,
f6 text ,
f7 text ,
f8 text ,
f9 text ,
f10 text
) ;
create table t_col_normal
(
f1 int,
f2 text ,
f3 text ,
f4 text ,
f5 text ,
f6 text ,
f7 text ,
f8 text ,
f9 text ,
f10 text
) with(orientation='column');
create table t_col_compress
(
f1 int,
f2 text ENCODING(compress_method='zstd',compress_level=1) ,
f3 text ENCODING(compress_method='zstd',compress_level=1) ,
f4 text ENCODING(compress_method='zstd',compress_level=1) ,
f5 text ENCODING(compress_method='zstd',compress_level=1) ,
f6 text ENCODING(compress_method='zstd',compress_level=1) ,
f7 text ENCODING(compress_method='zstd',compress_level=1) ,
f8 text ENCODING(compress_method='zstd',compress_level=1) ,
f9 text ENCODING(compress_method='zstd',compress_level=1) ,
f10 text ENCODING(compress_method='zstd',compress_level=1)
) with(orientation='column');
  • 测试脚本
insert into t_row_normalselectt,repeat(random()::text,3),repeat(random()::text,3),repeat(random()::text,3),repeat(random()::text,3),repeat(random()::text,3),repeat(random()::text,3),repeat(random()::text,3),repeat(random()::text,3),repeat(random()::text,3)from generate_series(1,1000000) as t;  

insert into t_col_normalselectt,repeat(random()::text,3),repeat(random()::text,3),repeat(random()::text,3),repeat(random()::text,3),repeat(random()::text,3),repeat(random()::text,3),repeat(random()::text,3),repeat(random()::text,3),repeat(random()::text,3)from generate_series(1,1000000) as t;

insert into t_col_compressselectt,repeat(random()::text,3),repeat(random()::text,3),repeat(random()::text,3),repeat(random()::text,3),repeat(random()::text,3),repeat(random()::text,3),repeat(random()::text,3),repeat(random()::text,3),repeat(random()::text,3)from generate_series(1,1000000) as t;
  • 用时及容量对比 对比
项目 行存 列存 列存(带压缩)
大小 521MB 896MB 480MB
生成数据时间 8.029 12.801 11.818

下面对比一下重复率非常高的,测试表如下:

  • 测试脚本
insert into t_row_normalselectt,repeat(random()::text,3),repeat(random()::text,3),repeat(random()::text,3),repeat(random()::text,3),repeat(random()::text,3),repeat(random()::text,3),repeat(random()::text,3),repeat(random()::text,3),repeat(random()::text,3)from generate_series(1,1000000) as t;  

insert into t_col_normalselectt,repeat(random()::text,3),repeat(random()::text,3),repeat(random()::text,3),repeat(random()::text,3),repeat(random()::text,3),repeat(random()::text,3),repeat(random()::text,3),repeat(random()::text,3),repeat(random()::text,3)from generate_series(1,1000000) as t;

insert into t_col_compressselect t,repeat(random()::text,3),repeat(random()::text,3),repeat(random()::text,3),repeat(random()::text,3),repeat(random()::text,3),repeat(random()::text,3),repeat(random()::text,3),repeat(random()::text,3),repeat(random()::text,3)from generate_series(1,1000000) as t;
  • 用时及容量对比 对比
项目 行存 列存 列存(带压缩)
大小 1898MB 1976MB 502MB
生成数据时间 12.680 26.019 19.261