Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

varchar(255) vs tinytext/tinyblob and varchar(65535) vs blob/text

Tags:

mysql

By definition:

VARCHAR: The range of Length is 1 to 255 characters. VARCHAR values are sorted and compared in case-insensitive fashion unless the BINARY keyword is given. x+1 bytes
TINYBLOB, TINYTEXT: A BLOB or TEXT column with a maximum length of 255 (2^8 - 1) characters x+1 bytes

So based on this, I creaate the following table:

CREATE TABLE `user` (   `id` int(10) unsigned NOT NULL AUTO_INCREMENT,   `name` varchar(255),   `lastname` tinytext,   PRIMARY KEY (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1 

Or is it better to create a varchar or tinytext and why?

Is it the same for:

VARCHAR: The range of Length is > 255 characters. VARCHAR values are sorted and compared in case-insensitive fashion unless the BINARY keyword is given. x+2 bytes
BLOB, TEXT A BLOB or TEXT column with a maximum length of 65535 (2^16 - 1) characters x+2 bytes

like image 218
Tech4Wilco Avatar asked Oct 13 '11 14:10

Tech4Wilco


People also ask

What is the difference between VARCHAR and Tinytext?

TINYTEXT shines over VARCHAR when storing data that's under 255 characters with an inconsistent length and no need to be used for sorting criteria.

Which is better VARCHAR or text in MySQL?

In most circumstances, VARCHAR provides better performance, it's more flexible, and can be fully indexed. If you need to store longer strings, use MEDIUMTEXT or LONGTEXT, but be aware that very large amounts of data can be stored in columns of these types.

What does 255 mean in VARCHAR?

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. Make sure you are aware of the effects of a multi-byte character set. VARCHAR(255) stores 255 characters, which may be more than 255 bytes.

Does VARCHAR length matter?

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.


1 Answers

In this case varchar is better.

Note that varchar can be from 1 to 65535 chars.

Values in VARCHAR columns are variable-length strings. The length can be specified as a value from 0 to 255 before MySQL 5.0.3, and 0 to 65,535 in 5.0.3 and later versions. The effective maximum length of a VARCHAR in MySQL 5.0.3 and later is subject to the maximum row size (65,535 bytes, which is shared among all columns) and the character set used. See Section E.7.4, “Table Column-Count and Row-Size Limits”.

Blobs are saved in a separate section of the file.
They require an extra fileread to include in the data.
For this reason varchar is fetched much faster.

If you have a large blob that you access infrequently, than a blob makes more sense.
Storing the blob data in a separate (part of the) file allows your core data file to be smaller and thus be fetched quicker.

like image 127
Johan Avatar answered Oct 04 '22 11:10

Johan