PostgreSql 将值存储为整数与 varchar

PostgreSql storing a value as integer vs varchar

我想在 table 中存储一个 15 位数字。

在查找速度方面,我应该使用 bigint 还是 varchar

此外,如果其中填充了数百万条记录,不同的数据类型是否会对存储产生任何影响?

您可以将它们存储为 BIGINT,因为与 varchar 相比,使用 INT 的比较速度更快。如果您希望有数百万条记录以加快数据检索速度,我还建议在该列上创建索引。

你也可以检查这个forum:

Indexing is fastest on integer types. Char types are probably stored underneath as integers (guessing.) VARCHARs are variable allocation and when you change the string length it may have to reallocate.

Generally integers are fastest, then single characters then CHAR[x] types and then VARCHARS.

就space而言,一个bigint占用8个字节,而一个15个字符的字符串最多占用19个字节(一个4个字节header和另一个最多15个字节对于数据),取决于它的价值。此外,一般来说,相等性检查对于数值运算应该稍微快一些。除此之外,它在很大程度上取决于您打算如何处理这些数据。如果你打算使用 numerical/mathematical 操作或根据范围查询,你应该使用 bigint.