Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why not specify every VARCHAR as VARCHAR (65535)?

Tags:

mysql

Since the storage requirements for a Varchar field are based on the actual length of the string entered, what would be the downside of specifying every Varchar field as the max possible: Varchar (65535)? Well, aside from 1 extra byte for max fields > 255 characters?

[Storage Reqts for strings of length L: L + 1 bytes if column values require 0 – 255 bytes, L + 2 bytes if values may require more than 255 bytes]

Thank you!

like image 970
tgoneil Avatar asked Oct 04 '11 19:10

tgoneil


People also ask

Should I specify length of VARCHAR?

Always specify a length to any text-based datatype such as NVARCHAR or VARCHAR .

What is the limit for VARCHAR?

Values in VARCHAR columns are variable-length strings. The length can be specified as a value from 0 to 65,535. The effective maximum length of a VARCHAR is subject to the maximum row size (65,535 bytes, which is shared among all columns) and the character set used.

What is the disadvantage of using VARCHAR?

In addition to the size and performance considerations of setting the size of a varchar (and possibly more important, as storage and processing get cheaper every second), the disadvantage of using varchar(255) "just because" is reduced data integrity.

Which special characters are not allowed in VARCHAR?

Uppercase or lowercase characters such as a, b, and C. Numerals such as 1, 2, or 3. Special characters such as the "at" sign (@), ampersand (&amp , and exclamation point (!).

What is varchar in SQL Server?

VARCHAR Datatype: It is a datatype in SQL which is used to store character string of variable length but maximum of set length specified. If the length of string is less than set or fixed length then it will store as it is without padded with extra blank spaces. Storage size of VARCHAR datatype is equal to the actual length of the entered ...

What is the difference between Char (255) and VARCHAR (255)?

In storage, VARCHAR (255) is smart enough to store only the length you need on a given row, unlike CHAR (255) which would always store 255 characters.

When to use char vs varchar datatype in C++?

We should use CHAR datatype when we expect the data values in a column are of same length. We should use VARCHAR datatype when we expect the data values in a column are of variable length. 6. CHAR take 1 byte for each character. VARCHAR take 1 byte for each character and some extra bytes for holding length information.

What is the maximum length of A varchar?

Maximum VARCHAR length The maximum row length of a VARCHAR is restricted by the maximum row length of a table. This is 65,535 bytes for most storage engines (NDB has a different maximum row value). Theoretically the maximum length of a VARCHAR is 65,536 bytes. Overhead further limits the actual maximum size of a VARCHAR.


3 Answers

From the documents - Table Column-Count and Row-Size Limits:

Every table (regardless of storage engine) has a maximum row size of 65,535 bytes. Storage engines may place additional constraints on this limit, reducing the effective maximum row size.

The maximum row size constrains the number (and possibly size) of columns because the total length of all columns cannot exceed this size. For example, utf8 characters require up to three bytes per character, so for a CHAR(255) CHARACTER SET utf8 column, the server must allocate 255 × 3 = 765 bytes per value. Consequently, a table cannot contain more than 65,535 / 765 = 85 such columns.

Storage for variable-length columns includes length bytes, which are assessed against the row size. For example, a VARCHAR(255) CHARACTER SET utf8 column takes two bytes to store the length of the value, so each value can take up to 767 bytes.

So, defining a single VARCHAR(65535) column, effectively limits you to a single column on the row (assuming you have filled it up).

All this apart from the fact that such a large size is completely wrong for some types of data - if you have a phone number column which may contain local and international numbers, you may choose to use a VARCHAR field to do so, but setting it to anything over 20 may well meaningless (I am being generous).

See this answer from Bill Karwin which also indicates possible performance penalties if temporary tables get generated with unnecessarily long VARCHAR fields (to do with conversion of such fields to CHAR and back again - see the post for details).

like image 100
Oded Avatar answered Oct 06 '22 16:10

Oded


I think varchar column lengths are not only about storage. They're about data semantics as well.

I.E. specifying a name column as varchar(100) means that names stored on your system must not be longer than 100 characters.

On the storage side of things, they should be the same. Although, row size estimations would be more accurate with a specific length on varchar columns that without them (without needing a statistics gathering system keeping data distributions on varchar sizes).

like image 33
Pablo Santa Cruz Avatar answered Oct 06 '22 18:10

Pablo Santa Cruz


One possible reason would be to improve compatibility with other applications. For instance, if you had an app that used a "product_no" field that was 100 chars long, and you wanted to interface with an app that used a similar field like "model_no" that was 40 chars long, it would be a pain. Any product_nos in your app that were longer than 40 chars would get truncated and you'd have to figure out some way to translate them between the applications.

like image 1
David Sanders Avatar answered Oct 06 '22 17:10

David Sanders