Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL: why varchar(254) and not varchar(255)?

Why do so many developers set varchar to 254 and not 255 when creating MySQL tables?

Proof that this happens: mysql varchar 254

like image 475
BaUn Avatar asked Apr 06 '12 12:04

BaUn


2 Answers

Your Google query gave you the hints already. One of the first hits is this:

https://www.vbulletin.com/forum/project.php?issueid=32655

It basically says, that FULLTEXT indexes on VARCHAR(255) require twice the space of a VARCHAR(254) FULLTEXT index. And some more other bloat on top of that.

I think this is far more important than saving one byte in the data table.

like image 129
A.H. Avatar answered Oct 10 '22 02:10

A.H.


varchar fields require n+1 bytes for fields less than or equal to 255 and required n+2 bytes for fields > 255

http://dev.mysql.com/doc/refman/5.0/en/storage-requirements.html

It should be set to 255, I'm assuming developers think they will save an extra byte from 254, but 255 is the standard

like image 30
Keith Avatar answered Oct 10 '22 01:10

Keith