Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does VARCHAR size limit matter? [duplicate]

Possible Duplicate:
Importance of varchar length in MySQL table

When using VARCHAR (assuming this is the correct data type for a short string) does the size matter? If I set it to 20 characters, will that take up less space or be faster than 255 characters?

like image 517
Jane Panda Avatar asked Dec 01 '10 13:12

Jane Panda


People also ask

Does size of VARCHAR matter?

There is no performance impact whether you use the full length VARCHAR declaration VARCHAR(16777216) or use a smaller precision VARCHAR datatype column.

Does VARCHAR size affect performance?

If you design VARCHAR columns much larger than the greatest size you need, you will consume more memory than you have to. This affects cache efficiency, sorting speed, etc.

Should I always use VARCHAR 255?

In short there isn't much difference unless you go over the size of 255 in your VARCHAR which will require another byte for the length prefix. The length indicates more of a constraint on the data stored in the column than anything else.

Why does VARCHAR occupy 2 extra bytes?

Answer. Because the length of the column data is variable, an extra byte is added to the head of the column in order to store that length. "varchar" has a maximum size of 255 characters because the highest value the length byte can store is 0xFF, or 255.


1 Answers

Yes, is matter when you indexing multiple columns.

Prefixes can be up to 1000 bytes long (767 bytes for InnoDB tables). Note that prefix limits are measured in bytes, whereas the prefix length in CREATE TABLE statements is interpreted as number of characters. Be sure to take this into account when specifying a prefix length for a column that uses a multi-byte character set.

source : http://dev.mysql.com/doc/refman/5.0/en/column-indexes.html

In a latin1 collation, you can only specify up 3 columns of varchar(255).
While can specify up to 50 columns for varchar(20)

In-directly, without proper index, it will slow-down query speed

In terms of storage, it does not make difference,
as varchar stand for variable-length strings

like image 86
ajreal Avatar answered Oct 11 '22 11:10

ajreal