What are the optimum varchar sizes for MySQL?

前端 未结 2 1913
无人共我
无人共我 2020-12-01 02:31

How does MySQL store a varchar field? Can I assume that the following pattern represents sensible storage sizes :

1,2,4,8,16,32,64,128,255 (max)

A clarificat

2条回答
  •  我在风中等你
    2020-12-01 03:09

    MySQL stores a varchar field as a variable length record, with either a one-byte or a two-byte prefix to indicate the record size.

    Having a pattern of storage sizes doesn't really make any difference to how MySQL will function when dealing with variable length record storage. The length specified in a varchar(x) declaration will simply determine the maximum length of the data that can be stored. Basically, a varchar(16) is no different disk-wise than a varchar(128).

    This manual page has a more detailed explanation.

    Edit: With regards to your updated question, the answer is still the same. A varchar field will only use up as much space on disk as the data you store in it (plus a one or two byte overhead). So it doesn't matter if you have a varchar(16) or a varchar(128), if you store a 10-character string in it, you're only going to use 10 bytes (plus 1 or 2) of disk space.

提交回复
热议问题