Search code examples
postgresqlstorage

Storage size of empty smallint[] Arrays vs separate table in PostgreSQL


I have two options:

a) Adding three smallint[] (array) attributes to my (main) table, two of them will be empty/unused most of the time and the third, will be used often (technically all 3 could be used at the same time, but it rarely happens).

b) Creating a new table with two foreign keys: 1 int-key (main) and 1 smallint-key AND the smallint[] array I need.

I want to optimize the database in terms of storage usage and have little to no experience in that regard.

Easier to implement would be the empty array variant. Quick testing the size of an empty smallint array (vs just smallint datatype) gave me chills:

According to pgAdmin 4/postgres its a wooping 140 Byte difference (rule of thumb dummy table test result), which would mean (if true) 280 Bytes down the drain for each dataset/row.

This is opposed to the 24-32 Bytes that ChatGPT claimed.


Solution

  • Even so it might be interesting to know, which of these options might have a slight edge over the other, more important questions need to be answered.

    The fact that some or most of the arrays in the attribute version of the solution will be empty (or filled with "the same values") indicate that the database normalization is infringed.

    Therefore there is only one reasonable solution: Option b)

    Many thanks to @Tim Roberts and @JohnH for pointing me towards the right direction.