Does setting max_length really consume extra space?

I have a field in the model,

name = models.CharField(max_length=2000)

      

and the entered data:

name='abc'

      

the django model is max_length

set to 2000 while the input is 3 in length. Does it save max_length

space for 2000 characters in the database table for each object? after saving the model object, is the space freed?

Does the max_length

database size increase if the number of objects in the database is several thousand or millions?

Database - postgres

+3


source to share


1 answer


CharField

will be introduced character varying(max_length)

to PostgreSQL.

From the docs :

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



So the disk space a string will take depends on its length + small overhead, not the maximum field length.

Using a higher max_length

one will not increase the disk space used by db.

+3


source







All Articles