我使用的是PostgreSQL 12.5,由Visual C++ build 1914编译,64位,我有下表:
CREATE TABLE public.historian_new_data_id_v2 (
"timestamp" timestamptz NOT NULL,
value float8 NOT NULL,
quality float4 NOT NULL,
tagname_id int2 NOT NULL
);我预计这个表将占用8+8+4+2=每行22个字节,因为我认为没有对齐问题。即使在最坏的情况下,所有字段都是8字节对齐的,我也希望它占用8*4= 32字节/行。
但是,这些是表的大小统计数据:
with row_count as (select COUNT(*) as c from historian_new_data_id_v2)
select
c as "number of rows",
pg_size_pretty(pg_total_relation_size('historian_new_data_id_v2')) as "total table size",
pg_total_relation_size('historian_new_data_id_v2')::numeric / c as " bytes/row"
from row_count行数:409858537
表大小:20 GB
字节/行:52.1783453494345538
这是大量的开销!每行52字节,而不是预期的22字节或更糟的情况32。这种差异是如何解释的?
另外,有什么建议可以让这个表变小(行数很快就会猛增)?
发布于 2021-02-01 10:13:31
PostgreSQL中的每一行都有系统列:
select attname, attnum, attlen
from pg_attribute
where attrelid = 'public.historian_new_data_id_v2'::regclass
order by attnum;
+------------+----------+----------+
| attname | attnum | attlen |
|------------+----------+----------|
| tableoid | -6 | 4 |
| cmax | -5 | 4 |
| xmax | -4 | 4 |
| cmin | -3 | 4 |
| xmin | -2 | 4 |
| ctid | -1 | 6 |
| timestamp | 1 | 8 |
| value | 2 | 8 |
| quality | 3 | 4 |
| tagname_id | 4 | 2 |
+------------+----------+----------+如果需要,此列可供选择:
select tableoid, cmax, xmax, cmin, xmin, ctid, timestamp, value, quality, tagname_id
from public.historian_new_data_id_v2;https://stackoverflow.com/questions/65990568
复制相似问题