Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 993
orokusaki Avatar asked Jan 13 '10 08:01

orokusaki


People also ask

What does 255 mean in varchar?

VARCHAR(255) stores 255 characters, which may be more than 255 bytes.

Can varchar store more than 255 characters?

Values in VARCHAR columns are variable-length strings. The length can be specified as a value from 0 to 65,535. The effective maximum length of a VARCHAR is subject to the maximum row size (65,535 bytes, which is shared among all columns) and the character set used.

Does varchar size affect performance?

Declared varchar column length will not affect the physical (on-disk) or data cache storage. It will affect the performance of actually using that index. The values must be loaded into a query's executing memory space in order to be read and processed.

What size should I use for varchar?

The size of the maximum size (m) parameter of a VARCHAR column can range from 1 to 255 bytes. If you are placing an index on a VARCHAR column, the maximum size is 254 bytes. You can store character strings that are shorter, but not longer, than the m value that you specify.


1 Answers

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.

like image 109
Alex Budovski Avatar answered Oct 04 '22 18:10

Alex Budovski