Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MYSQL VARCHAR or CHAR for fixed length string

If I know a value being stored in MySQL is always going to be exactly 32 characters, is it better performance to make the column type CHAR instead of VARCHAR? What exactly is the performance difference between using VARCHAR and CHAR?

Thanks.

like image 206
Justin Avatar asked May 10 '12 00:05

Justin


People also ask

Is VARCHAR fixed length?

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. See Section 8.4.

Which data type stores a fixed length string?

The CHAR datatype stores fixed-length character strings.

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.

Which is better CHAR or VARCHAR?

We should use the CHAR datatype when we expect the data values in a column are of the same length. We should use the VARCHAR datatype when we expect the data values in a column are of variable length.


1 Answers

I would use CHAR.

If you are likely to search on the column, CHAR presents a small performance upgrade over VARCHAR.

Since your data size is going to be fixed, there is no downside to using CHAR, as an VARCHAR equivalent will store anywhere from one to two bytes as a prefix.

Reference: MySQL CHAR vs VARCHAR

like image 165
hkf Avatar answered Sep 28 '22 06:09

hkf