我在模型中有一个字段,
name = models.CharField(max_length=2000)
输入的数据是,
name='abc'
django 模型的 max_length
设置为 2000 而输入的数据只有 3 的长度,
max_length
是否为每个对象在数据库表中保留 2000 个字符的空间?保存模型对象后,空间是否被释放?
如果数据库中的对象数量为数千或数百万,设置更高的 max_length
是否会增加数据库的大小?
数据库是postgres
最佳答案
CharField
将由 PostgreSQL 中的 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
不会增加数据库使用的磁盘空间。
关于python - 将 max_length 设置为非常大的值会占用额外的空间吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30663791/