FireFox Naruto
FireFox Naruto

Reputation: 55

MySQL: VARCHAR(1024) vs VARCHAR(512)

In MySQL what is the difference between VARCHAR(1024) and VARCHAR(512)? If my item will never be more than 512 characters, what do I lose by using VARCHAR(1024)?

Upvotes: 5

Views: 33789

Answers (4)

wistlo
wistlo

Reputation: 319

According to mySQL documentation

In contrast to CHAR, VARCHAR values are stored as a 1-byte or 2-byte length prefix plus data. The length prefix indicates the number of bytes in the value. A column uses one length byte if values require no more than 255 bytes, two length bytes if values may require more than 255 bytes.

A deeper analysis of the performance impact of larger VARCHARs can be found here.

Upvotes: 0

Andrey Kotov
Andrey Kotov

Reputation: 1

VARCHAR(1024) 1024 this is lenght.

The CHAR and VARCHAR types are declared with a length that indicates the maximum number of characters you want to store. For example, CHAR(30) can hold up to 30 characters.

Upvotes: 0

Joël V.
Joël V.

Reputation: 90

There is a actually a difference. And it can have a big performance impact if you manipulate big data. If a temporary table is used, the records on disk will take the full length indicated instead of the variable length. A high value will slow down the request even more in that case. Temporary tables can occur for various reasons (such as memory full, or some combinations of group by /order by).

Upvotes: 3

fancyPants
fancyPants

Reputation: 51868

Don't know where you got that from, but it's not possible to create a table with varchar without specifying the length. It results in a syntax error. So your question is obsolete.

UPDATE:

Nothing. Varchar is as the name implies a datatype of variable length, at least to the maximum length you specified when creating the table. This means, that in a varchar column for each row one additional byte is used to store how long the string in the row actually is. So the difference between varchar(1024) and varchar(512) is, that your data gets truncated when you try to insert more than 1024 or 512 bytes. Note: bytes, not characters. How much bytes each character uses is dependent on the character set you're using.

Upvotes: 9

Related Questions