10

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

All Іѕ Vаиітy
  • 24,861
  • 16
  • 87
  • 111
  • You can use `manage.py sqlmigrate` to inspect the sql yourself: https://docs.djangoproject.com/en/1.8/ref/django-admin/#sqlmigrate-app-label-migrationname – Wtower Jun 05 '15 at 10:10

1 Answers1

11

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.

Adrian Ghiuta
  • 1,569
  • 16
  • 29
  • 2
    Just to note, this answer is true for PostgreSQL, MySQL, Oracle and SQL Server. If you choose to use a non-relational or other database, the same may not be true. – FlipperPA Jun 05 '15 at 15:43