在不同的整数类型中保存相同的值有什么不同吗?

时间:2022-02-02 12:32:12

The main integer fields in Django are the following,

Django中的主要整数字段如下所示,

Integer Field

An integer. Values from -2147483648 to 2147483647 are safe in all databases supported by Django. The default form widget for this field is a NumberInput when localize is False or TextInput otherwise.

一个整数。在Django支持的所有数据库中,从-2147483648到2147483647的值是安全的。当本地化为False或TextInput时,该字段的默认表单小部件是一个NumberInput。


SmallIntegerField

Like an IntegerField, but only allows values under a certain (database-dependent) point. Values from -32768 to 32767 are safe in all databases supported by Django.

像IntegerField一样,但只允许在特定(数据库依赖)点下的值。在Django支持的所有数据库中,从-32768到32767的值是安全的。


BigIntegerField

A 64 bit integer, much like an IntegerField except that it is guaranteed to fit numbers from -9223372036854775808 to 9223372036854775807. The default form widget for this field is a TextInput.

一个64位的整数,很像一个IntegerField,除了它被保证适合从-9223372036854775808到9223372036854775807的数字。该字段的默认表单小部件是TextInput。


If I have a number, 33432 (some number that fits in all of the fields), If I choose to store the same number in all of the below fields, will there be an excess use of memory with both the database or at the runtime?

如果我有一个数字33432(某个数字适用于所有字段),如果我选择在下面的所有字段中存储相同的数字,那么数据库或运行时是否会过度使用内存?

number0 = models.SmallIntegerField()
number1 = models.IntegerField()
number2 = models.BigIntegerField()

My database is PostgreSQL.

我的PostgreSQL数据库。

2 个解决方案

#1


1  

Most operations are fastest for plain integer, but the difference is very small and typically the least of your concerns when optimizing performance.

大多数操作对于纯整数来说是最快的,但是差异非常小,并且在优化性能时通常是最不需要考虑的。

Storage size is more relevant, but the difference between various integer types is still very small and often hardly relevant, sometimes lost to padding and alignment. There are other data types that can waste much more space.

存储大小更相关,但是各种整数类型之间的差异仍然非常小,而且通常不相关,有时由于填充和对齐而丢失。还有其他数据类型可能会浪费更多的空间。

smallint (int2) occupies 2 bytes on disk and in RAM.
integer (int, int4) occupies 4 bytes on disk and in RAM.
bigint (int8) occupies 8 bytes on disk and in RAM.

smallint (int2)在磁盘和RAM中占用2个字节。整数(int, int4)在磁盘和RAM中占用4个字节。bigint (int8)在磁盘和RAM中占用8个字节。

Details for numeric types in Postgres in the manual.

关于手册中Postgres中的数字类型的详细信息。

There are various other factors for actual storage size. You have to consider page and tuple overhead, alignment and padding, possible NULL values, indexing ...

对于实际的存储大小,还有各种其他因素。您必须考虑页面和元组开销、对齐和填充、可能的空值、索引……

Details:

细节:

There is some potential for optimizing, but typically not much. Best concentrate on choosing an appropriate data type for your data and don't worry about minor differences in storage and performance, unless you know exactly what you are doing.

有一些优化的潜力,但通常不是很多。最好专注于为数据选择合适的数据类型,不要担心存储和性能上的微小差异,除非您确切地知道自己在做什么。

#2


0  

Yes, the memory storage and runtime memory allocated for your numeric data will be directly proportional to the byte size of the numeric structure you choose.

是的,为数字数据分配的内存和运行时内存将直接与选择的数字结构的字节大小成比例。

There are some compression methods such as changing integers and decimals to the variable-length format instead of their native fixed-length format for SQL server. However, they introduce higher CPU usage as mentioned here.

有一些压缩方法,比如将整数和小数转换为可变长度格式,而不是SQL server的本地固定长度格式。但是,正如这里提到的,它们引入了更高的CPU使用率。

#1


1  

Most operations are fastest for plain integer, but the difference is very small and typically the least of your concerns when optimizing performance.

大多数操作对于纯整数来说是最快的,但是差异非常小,并且在优化性能时通常是最不需要考虑的。

Storage size is more relevant, but the difference between various integer types is still very small and often hardly relevant, sometimes lost to padding and alignment. There are other data types that can waste much more space.

存储大小更相关,但是各种整数类型之间的差异仍然非常小,而且通常不相关,有时由于填充和对齐而丢失。还有其他数据类型可能会浪费更多的空间。

smallint (int2) occupies 2 bytes on disk and in RAM.
integer (int, int4) occupies 4 bytes on disk and in RAM.
bigint (int8) occupies 8 bytes on disk and in RAM.

smallint (int2)在磁盘和RAM中占用2个字节。整数(int, int4)在磁盘和RAM中占用4个字节。bigint (int8)在磁盘和RAM中占用8个字节。

Details for numeric types in Postgres in the manual.

关于手册中Postgres中的数字类型的详细信息。

There are various other factors for actual storage size. You have to consider page and tuple overhead, alignment and padding, possible NULL values, indexing ...

对于实际的存储大小,还有各种其他因素。您必须考虑页面和元组开销、对齐和填充、可能的空值、索引……

Details:

细节:

There is some potential for optimizing, but typically not much. Best concentrate on choosing an appropriate data type for your data and don't worry about minor differences in storage and performance, unless you know exactly what you are doing.

有一些优化的潜力,但通常不是很多。最好专注于为数据选择合适的数据类型,不要担心存储和性能上的微小差异,除非您确切地知道自己在做什么。

#2


0  

Yes, the memory storage and runtime memory allocated for your numeric data will be directly proportional to the byte size of the numeric structure you choose.

是的,为数字数据分配的内存和运行时内存将直接与选择的数字结构的字节大小成比例。

There are some compression methods such as changing integers and decimals to the variable-length format instead of their native fixed-length format for SQL server. However, they introduce higher CPU usage as mentioned here.

有一些压缩方法,比如将整数和小数转换为可变长度格式,而不是SQL server的本地固定长度格式。但是,正如这里提到的,它们引入了更高的CPU使用率。