数据库 免费薅 Postgres 羊毛的两个小 tips

hooopo · October 02, 2020 · Last by ceclinux-github replied at October 09, 2020 · 1289 hits

大概原理就是每个列类型会有 padding,如果按合理的顺序组合会消除一些浪费的物理空间,就像俄罗斯方块一样...

CREATE TABLE t (
    e int2    -- 6 bytes of padding after int2
  , a int8
  , f int2    -- 6 bytes of padding after int2
  , b int8
  , g int2    -- 6 bytes of padding after int2
  , c int8
  , h int2    -- 6 bytes of padding after int2
  , d int8)

after

CREATE TABLE t (
    a int8
  , b int8
  , c int8
  , d int8
  , e int2
  , f int2
  , g int2
  , h int2)   -- 4 int2 occupy 8 byte (MAXALIGN), no padding at the end

据说一些场景可以节省 20% 左右的空间,如果你的 DB 上 T 的话,省下来的空间还是挺可观的。

braintree 出的一个辅助工具:https://github.com/braintree/pg_column_byte_packer

另外一个就是 postgres 13,索引空间也有优化,升级之后索引占用物理空间也会减小,如果是 append only 类型的数据,使用 brin 索引可以节省好多空间

参考:

一般都是 int 和 bigint 对的齐齐的

Reply to pynix

不是吧……上面链接有写啊 int4 对不齐

interesting,看起来原理很简单。但是为什么 pg 不自己默认优化 CREATE TABLE 的 padding 呢

You need to Sign in before reply, if you don't have an account, please Sign up first.