All Іѕ Vаиітy
All Іѕ Vаиітy

Reputation: 26452

do setting the max_length to a very large value consume extra space?

I have a field in the model,

name = models.CharField(max_length=2000)

and the data entered is,

name='abc'

the django model's max_length is set to 2000 while the entered data is only of length 3, Does the max_length reserves space for 2000 characters in the database table per object? after the model object is saved, does the space is freed up?

Do setting up higher max_length increase the size of database if the number of objects on database is several thousands or millions?

Database is postgres

Upvotes: 10

Views: 1540

Answers (1)

Adrian Ghiuta
Adrian Ghiuta

Reputation: 1619

The CharField will be represented by a character varying(max_length) in PostgreSQL.

From the 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.

So, the disk space the string will take up is dependent on it's length + a small overhead, not on the max length of the field.

Using a higher max_length will not increase the disk space used by the db.

Upvotes: 12

Related Questions