postgres表中的列的顺序是否会影响性能? [英] Does the order of columns in a postgres table impact performance?

查看:248
本文介绍了postgres表中的列的顺序是否会影响性能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Postgres中, CREATE TABLE 语句中的列的顺序会影响性能?考虑以下两种情况:

  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
);

foo2 的表现要好于 foo 因为列更好的字节对齐?当Postgres执行 CREATE TABLE 是否遵循指定的列顺序,还是以最佳顺序对列进行字节对齐或性能重新组织?

解决方案

问题1




foo2的性能要好于foo,因为列更好的字节
对齐?


是的列的顺序对性能影响很小。类型对齐是更重要的因素,因为它影响磁盘上的占位面积。您可以最小化存储大小(播放列四进制)并在数据页上挤压更多行 - 这是速度最重要的因素。



这个相关的答案,有一个极端的例子,你会得到一个实质的差异。

通常情况下,这不值得去打扰。



另一个因素是检索列值是如果您有固定大小的列首先要快一点。我引用手册


要读取数据,您需要依次检查每个属性。首先
根据空位图检查该字段是否为NULL。如果
是,请转到下一个。然后确保你有正确的对齐。如果
的字段是一个固定的宽度字段,那么所有的字节都是简单的
放置。如果它是一个可变长度字段(attlen = -1),那么它有点
更复杂。所有可变长度数据类型共享共同的
头结构struct varlena,其中包括
的总长度,存储值和一些标志位。


有一个开放的 TODO项目,以允许在Postgres Wiki中重新排列列位置,部分是因为这些原因。



问题2




当postgres执行CREATE TABLE是否遵循指定的列$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $






$ $ b

列以定义的顺序存储,系统不会尝试优化。



我没有看到任何相关性列表顺序如 TOAST表,像另一个答案似乎暗示。


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 the performance of foo2 be better than foo because of better byte alignment for the columns? 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?

解决方案

Question 1

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

Yes, the order of columns has 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.

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

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.

这篇关于postgres表中的列的顺序是否会影响性能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆