Get the latest tech news

Optimizing Postgres table layout for maximum efficiency


Introduction When modeling a Postgres database, you probably don’t give much thought to the order of columns in your tables. After all, it seems like the kind of thing that wouldn’t affect storage or performance. But what if I told you that simply reordering your columns could reduce the size of your tables and indexes by 20%? This isn’t some obscure database trick — it’s a direct result of how Postgres aligns data on disk.\n

The right answer depends heavily on the context behind your product, but eventually you will notice dramatic infrastructure-related costs (specific to your databases) and maybe that’s a good starting point. A generic rule of thumb I’ve been using for the past few years is to, whenever possible, define columns based on a descending order of data types size. For example, in NDB tables, the TINYINT, SMALLINT, MEDIUMINT, and INTEGER (INT) column types each require 4 bytes storage per record due to the alignment factor.

Get the Android app

Or read this on Hacker News

Read more on:

Photo of maximum efficiency

maximum efficiency

Photo of Optimizing Postgres

Optimizing Postgres

Related news:

News photo

Data optimization is a must for maximum efficiency