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.
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.
The CHAR datatype stores fixed-length character strings.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With