我得到了一个大的(> 100M行)Postgres表,其结构为{integer,integer,integer,timestamp without time zone}.我期望一行的大小为3*整数+ 1*时间戳= 3*4 + 1*8 = 20字节.
实际上,行大小pg_relation_size(tbl) / count(*)= 52字节.为什么?
(不对表进行删除:pg_relation_size(tbl, 'fsm')〜= 0)
我试图了解列顺序如何最小化PostgreSQL中的表大小.
例:
CREATE TABLE test (
column_1 int
,column_2 int
,column_3 bigint
,column_4 bigint
,column_5 text
,column_6 text
,column_7 numeric(5,2)
,column_8 numeric(5,2)
,column_9 timestamp
,column_10 boolean
,column_11 boolean
);
INSERT INTO test
VALUES(1,1,1,1,'test','test_1',12,12,current_timestamp,true,false);
SELECT pg_column_size(test.*) FROM test;
pg_column_size
----------------
82
(1 row)
Run Code Online (Sandbox Code Playgroud)
元组大小:
元组头的23字节开销+ NULL位图的1字节,因此:
24 + 4 + 4 + 8 + 8 + 5 + 7 + 5 + 5 + 8 + 1 + 1 = 80但实际元组大小为82.
是否有2字节的额外开销?
我理解下面链接给出的例子:
在PostgreSQL中计算和节省空间
如果我们删除column_8 numeric(5,2)那么那么元组大小保持不变,即:82.
我重新排序表以最小化元组大小并给出80.
CREATE TABLE …Run Code Online (Sandbox Code Playgroud)