Halo列存columnar介绍
Halo列存columnar介绍
随着大数据时代的到来,数据对于企业的重要性越来越高。数据仓库作为一种集成式的数据存储平台,被广泛应用于企业数据分析。而在数据仓库的背景下,OLAP(在线分析处理)成为一种重要的数据处理技术。作为仓储一般都代表数据表很大,磁盘占用空间多,针对这种情况,羲和(Halo)通用数据库采用一种列存引擎,可以提高大表查询的速度,并可以降低磁盘的使用率。
一、列存插件 columnar的特点:
1.高压缩率
面向列的数据库通常采用一些高效的压缩算法,能够显著减小数据在磁盘上的存储空间。这是因为在同一列中的数据通常具有相似的特性,例如相同数据类型、相近的数值范围等。通过对列进行压缩,可以大幅度减少磁盘占用的空间,提升存储效率。
2.高性能查询
由于面向列的数据库将数据按列存储,可以仅加载需要的列,避免加载不必要的数据。这种存储结构针对大规模的数据查询提供了显著的优势。在面向列的数据库中,可以利用向量化操作和批处理技术来高效地执行查询,提升查询性能。
3.高并发写入
传统的行存储数据库在多个用户同时写入时,可能会出现写的冲突,需要加锁等机制来保证数据的一致性。而面向列的数据库采用列存储结构,不同列的数据可以独立存储,减少了写入操作的冲突,提高了并发写入的能力。
4.适用于大数据分析
面向列的数据库在数据分析和数据仓库领域拥有广泛的应用。由于列存储结构的特点,面向列的数据库能够更高效地处理大规模数据集,并支持快速的数据聚合、多维分析和复杂查询。这些特性使得它成为了大数据分析的一种重要工具。
二、Halo用法介绍
-- 创建扩展,需要超级用户权限
CREATE EXTENSION IF NOT EXISTS columnar;
-- 可在建表时指定要创建的类型:
CREATE TABLE halo_heap_table (id int) USING heap;
CREATE TABLE halo_columnar_table (id int) USING columnar;
-- 支持列存、行存相互转换
CREATE TABLE halo_table (i INT) USING heap;创建heap表
-- convert to columnar 转columnar表
SELECT columnar.alter_table_set_access_method('halo_table', 'columnar');
-- convert back to row (heap) 转heap表
SELECT columnar.alter_table_set_access_method('halo_table', 'heap');
--也支持通过拷贝数据手动转换
CREATE TABLE halo_table_heap (i INT) USING heap;
insert into halo_table_heap select n from generate_series(1,10) as n;
CREATE TABLE halo_table_columnar (LIKE halo_table_heap) USING columnar;
INSERT INTO halo_table_columnar SELECT * FROM halo_table_heap;
--支持分区表,分区表可以是heap表,也可以是columna表
CREATE TABLE halo_parent(ts timestamptz, i int, n numeric, s text)
PARTITION BY RANGE (ts);
-- columnar partition
CREATE TABLE p0 PARTITION OF halo_parent
FOR VALUES FROM ('2020-01-01') TO ('2020-02-01')
USING columnar;
-- columnar partition
CREATE TABLE p1 PARTITION OF halo_parent
FOR VALUES FROM ('2020-02-01') TO ('2020-03-01')
USING columnar;
-- row partition
CREATE TABLE p2 PARTITION OF halo_parent
FOR VALUES FROM ('2020-03-01') TO ('2020-04-01')
USING heap;
INSERT INTO halo_parent VALUES ('2020-01-15', 10, 100, 'one thousand'); -- columnar
INSERT INTO halo_parent VALUES ('2020-02-15', 20, 200, 'two thousand'); -- columnar
INSERT INTO halo_parent VALUES ('2020-03-15', 30, 300, 'three thousand'); -- row
三、列存储列存和行存的对比测试
1、表所占空间对比
创建heap表
create table halo_test1(id int,info text) USING heap;
insert into halo_test1 select n,'test' from generate_series(1,10000000) as n;
select pg_size_pretty(pg_relation_size('halo_test1'));
创建columnar表
create table halo_test2(id int,info text) USING columnar;
insert into halo_test2 select n,'test' from generate_series(1,10000000) as n;
select pg_size_pretty(pg_relation_size('halo_test2'));
可见columnar表所占空间只是heap表的1/15。
2、插入数据对比
创建heap表
create table halo_test3(id int,info text) USING heap;
insert into halo_test3 select n,'test' from generate_series(1,10000000) as n;
创建columnar表
create table halo_test4(id int,info text) USING columnar;
insert into halo_test4 select n,'test' from generate_series(1,10000000) as n;
可见columnar表插入时间只是heap表的1/4。
3、查询时间对比
创建heap表
create table halo_test5(id int,info text) USING heap;
insert into halo_test5 select n,'test' from generate_series(1,10000000) as n;
create table halo_test6(id int,info text) USING heap;
insert into halo_test6 select n,'test' from generate_series(1,100000) as n;
查看执行时间
创建columnar表
create table halo_test7(id int,info text) USING columnar;
insert into halo_test7 select n,'test' from generate_series(1,10000000) as n;
create table halo_test8(id int,info text) USING columnar;
insert into halo_test8 select n,'test' from generate_series(1,100000) as n;
查看执行时间
四、总结
对相同数据量的heap表和columnar表进行单表读、多表关联读、并发写入、和所占空间大写进行了对比。columnar表所占空间只是heap表的1/15,单表读耗时columnar表是heap表的1/4,大量写入数据耗时 columnar表是heap表的1/4。可见Halo数据库的列存储方案,可以针对不同业务 ,节约大量磁盘空间,并提高数据库读写的能力。