Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 605
All Іѕ Vаиітy Avatar asked Jun 05 '15 10:06

All Іѕ Vаиітy


1 Answers

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.

like image 51
Adrian Ghiuta Avatar answered Oct 03 '22 21:10

Adrian Ghiuta