PostgreSQL时序库分区表优化

时序库表优化

前言: 紧缩对于时序数据库是非常重要的。因为时序数据库面对的物联网天天都会产生上亿条数据。在大年夜数据时代的今天数据的重要性是不问可知的,数据就是公司的将来。但如无法对这些时序数据进行很好的治理和紧缩,那将给客户带来异常高的成本压力。


统计时序数据库timescaledb分区表的大小

with tb1 as (
SELECT
table_size.relname 表名,
pg_size_pretty ( pg_relation_size ( relid ) ) 表数据大小,
pg_size_pretty ( pg_indexes_size ( relid ) ) 表总索引大小,
pg_size_pretty ( pg_total_relation_size ( relid ) ) 表总大小,
表行数
FROM
pg_stat_user_tables table_size
LEFT JOIN (
SELECT
relname,
reltuples :: DECIMAL ( 19, 0 ) 表行数
FROM
pg_class r
JOIN pg_namespace n ON ( relnamespace = n.oid )
WHERE
relkind = 'r'
AND n.nspname = '_timescaledb_internal'
) table_num ON table_num.relname = table_size.relname
WHERE
schemaname = '_timescaledb_internal'
ORDER BY
pg_relation_size ( relid ) DESC)

select a.table_name 表名,b.table_name 分区名,c.dates 时间,表数据大小,表总索引大小,表总大小,表行数 from "_timescaledb_catalog".hypertable a
join "_timescaledb_catalog".chunk b on a.id=b.hypertable_id
join (SELECT id,dimension_id,to_char(to_timestamp(range_end),'yyyy-MM-dd') as dates FROM "_timescaledb_catalog"."dimension_slice") c on b.id=c.id
join tb1 on tb1.表名=b.table_name
ORDER BY a.table_name,c.dates;

分区数据总大小

SELECT
pg_size_pretty(sum( pg_relation_size ( relid ) )) 表数据大小,
pg_size_pretty(sum( pg_indexes_size ( relid ) )) 表总索引大小,
pg_size_pretty(sum( pg_total_relation_size ( relid ) )) 表总大小,
sum(表行数) 表行数
FROM
pg_stat_user_tables table_size
LEFT JOIN (
SELECT
relname,
reltuples :: DECIMAL ( 19, 0 ) 表行数
FROM
pg_class r
JOIN pg_namespace n ON ( relnamespace = n.oid )
WHERE
relkind = 'r'
AND n.nspname = '_timescaledb_internal'
) table_num ON table_num.relname = table_size.relname
WHERE
schemaname = '_timescaledb_internal'

时序库分区压缩:

ALTER TABLE hrmw.dw_sup SET (
timescaledb.compress,
timescaledb.compress_segmentby = '表id',
timescaledb.compress_orderby = 'collect_time DESC');

单表压缩

SELECT compress_chunk( '_timescaledb_internal.要压缩的分区表名');

查询压缩后空间状态

SELECT * FROM timescaledb_information.compressed_chunk_stats;

解压缩

SELECT decompress_chunk('_timescaledb_internal.要解压的分区表名');

相关文章

项目需要,有个数据需要导入,拿到手一开始以为是mysql,结果...
本文小编为大家详细介绍“怎么查看PostgreSQL数据库中所有表...
错误现象问题原因这是在远程连接时pg_hba.conf文件没有配置正...
因本地资源有限,在公共测试环境搭建了PGsql环境,从数据库本...
wamp 环境 这个提示就是说你的版本低于10了。 先打印ph...
psycopg2.OperationalError: SSL SYSCALL error: EOF detect...