Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does the order of columns in a Postgres table impact performance?

Tags:

In Postgres does the order of columns in a CREATE TABLE statement impact performance? Consider the following two cases:

CREATE TABLE foo (   a      TEXT,    B      VARCHAR(512),   pkey   INTEGER PRIMARY KEY,   bar_fk INTEGER REFERENCES bar(pkey),   C      bytea );  

vs.

CREATE TABLE foo2 (   pkey   INTEGER PRIMARY KEY,   bar_fk INTEGER REFERENCES bar(pkey),   B      VARCHAR(512),         a      TEXT,    C      bytea ); 

Will performance of foo2 be better than foo because of better byte alignment for the columns? When Postgres executes CREATE TABLE does it follow the column order specified or does it re-organize the columns in optimal order for byte alignment or performance?

like image 460
ams Avatar asked Sep 26 '12 15:09

ams


People also ask

Does order of columns matter in PostgreSQL?

The order of columns doesn't matter in creating tables in PostgreSQL, but it does matter sometimes in creating indexes in PostgreSQL.

Does number of columns affect performance in Postgres?

Yes the number of columns will - indirectly - influence the performance. The data in the columns will also affect the speed.

Does the order of columns in a table matter?

Yes, column order does matter.

Does group by order matter Postgres?

You are right that the result is the same no matter in which order the columns appear in the GROUP BY clause, and that the same execution plan could be used. The PostgreSQL optimizer just doesn't consider reordering the GROUP BY expressions to see if a different ordering would match an existing index.


1 Answers

Question 1

Will the performance of foo2 be better than foo because of better byte alignment for the columns?

Yes, the order of columns can have a small impact on performance. Type alignment is the more important factor, because it affects the footprint on disk. You can minimize storage size (play "column tetris") and squeeze more rows on a data page - which is the most important factor for speed.

Normally it's not worth to bother. With an extreme example like in this related answer you get a substantial difference:

  • Calculating and saving space in PostgreSQL

Type alignment details:

  • Making sense of Postgres row sizes

The other factor is that retrieving column values is slightly faster if you have fixed size columns first. I quote the manual here:

To read the data you need to examine each attribute in turn. First check whether the field is NULL according to the null bitmap. If it is, go to the next. Then make sure you have the right alignment. If the field is a fixed width field, then all the bytes are simply placed. If it's a variable length field (attlen = -1) then it's a bit more complicated. All variable-length data types share the common header structure struct varlena, which includes the total length of the stored value and some flag bits.

There is an open TODO item to allow reordering of column positions in the Postgres Wiki, partly for these reasons.

Question 2

When Postgres executes a CREATE TABLE does it follow the column order specified or does it re-organize the columns in optimal order for byte alignment or performance?

Columns are stored in the defined order, the system does not try to optimize.

I fail to see any relevance of column order to TOAST tables like another answer seems to imply.

like image 187
Erwin Brandstetter Avatar answered Sep 19 '22 08:09

Erwin Brandstetter