Search code examples
mysqldatabasedjangodatabase-designvarchar

Is a varchar 2 more efficient than a varchar 255?


I'm using Django and setting my CharField(max_length=255), even though I only intend to use about 5 characters. Is this less efficient? I've read that it doesn't really matter with varchar but then read that it'll save hard drive space to specify only what you need.


Solution

  • In general, varchar(255) requires as much storage as varchar(1). In each case the table stores something like a pointer into a string table and a length. E.g. 4 bytes offset + 1 byte size = 5 bytes fixed per row, just for overhead.

    The actual content is of course in the string table, which is only as long as the string your store in it. So if you store a 5 letter name in a varchar(255) field, it'll only use (say) 5 overhead bytes + 5 content bytes = 10 bytes.

    Using a varchar(10) field will use exactly the same amount, but will only truncate strings longer than 10 bytes.


    Of course, the specific numbers depend on the storage engine implementation.