Skip to content
Advertisement

MySQL – why limit varchar

In MySQL you have to set a limit if you are to use a varchar. Does this limit only exist to make it less likely for devs to accidentally store too much data or is there any other reason (i.e. more effcient access) to set it to a low value?

Advertisement

Answer

VARCHAR columns are represented in the database with a fixed-size length field and variable-length contents.

The database needs to know how many bytes to use for the hidden length field. If the length is less than 256 it uses 1 byte, if it’s between 256 and 65535 it uses 2 bytes.

It could have been possible to use a length field that automatically expands as needed. But keeping it fixed-length makes things more efficient.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement