使用 Rails 3.2.* 和 Postgres 的字符串与文本 - 我应该只使用文本吗

string vs text using Rails 3.2.* and Postgres - should I always just use text

我采用了一个 Rails 应用程序(Rails 3.2 和 Postgres 9.4),它有几个 Rails 字符串,我们已经超过了 255 个限制。此应用程序以前使用 MySQL 而不是 Postgres 作为后备存储。我的理解是 postgres 处理字符串和文本的方式相同。这个对吗?在将所有 Rails 字符串迁移到文本之前,我们应该注意哪些限制?

性能问题有点令人担忧,但不是主要问题。

来自fine manual:

Tip: There is no performance difference among these three types, apart from increased storage space when using the blank-padded type, and a few extra CPU cycles to check the length when storing into a length-constrained column. While character(n) has performance advantages in some other database systems, there is no such advantage in PostgreSQL; in fact character(n) is usually the slowest of the three because of its additional storage costs. In most situations text or character varying should be used instead.

他们所说的三种类型是 char(n)varchar(n)text。提示本质上是说:

  • char(n) 是最慢的,因为有空白填充并且必须检查长度限制。
  • varchar(n)一般在中间,因为需要检查长度约束。
  • text(即没有 nvarchar)通常是最快的,因为没有额外的开销。

除了 char(n) 的空白填充和 char(n)varchar(n) 的长度检查外,它们在幕后的处理方式相同。

对于 ActiveRecord,t.stringvarchart.texttext。如果您的字符串没有任何硬性长度限制,那么只需将 t.text 与 PostgreSQL 一起使用即可。