将max_length设置为非常大的值是否会占用额外的空间?

2024-06-24 02:20:53 发布

您现在位置:Python中文网/ 问答频道 /正文

我在模型中有一个字段

name = models.CharField(max_length=2000)

输入的数据是

^{pr2}$

django模型的max_length设置为2000,而输入的数据长度只有3, max_length是否为每个对象的数据库表中的2000个字符保留空间?保存模型对象后,是否释放空间?在

如果数据库上的对象数是几千或几百万,设置更高的max_length是否会增加数据库的大小?在

数据库是postgres


Tags: 数据对象djangoname模型数据库models空间
1条回答
网友
1楼 · 发布于 2024-06-24 02:20:53

在PostgreSQL中,CharField将由character varying(max_length)表示。在

docs

The storage requirement for a short string (up to 126 bytes) is 1 byte plus the actual string, which includes the space padding in the case of character. Longer strings have 4 bytes of overhead instead of 1.

因此,字符串将占用的磁盘空间取决于它的长度+一个较小的开销,而不是字段的最大长度。在

使用更高的max_length不会增加数据库使用的磁盘空间。在

相关问题 更多 >

    热门问题