
CHAR and VARCHAR:
CHAR is the predecessor to VARCHAR. It has the following features
- It supports the fixed length of 255 characters
*There is no need to declare the character count while creating the table - CHAR pads empty characters with spaces
- Storage consumption is fixed even if you consume less than 255 characters
- There is no padding for unused characters
*It only uses the required amount of storage and 1-2 bytes more for the length prefix
TEXT and its variants Now coming to TEXT, it is very similar to VARCHAR at the basic level, but it has two more variants, which sets it apart. The three variants are: Although TEXT is very similar to VARCHAR, its variants offer greater flexibility when data storage requirements exceed 65,535 bytes. However, there are some drawbacks too of using TEXT, so let’s compare it with VARCHAR. VARCHAR vs. TEXT Below is a parameter-wise comparison of the two data types: Indexing Ability: VARCHAR can be fully indexed, while TEXT columns can be indexed only up to a certain length.
- Sorting Possibility: VARCHAR can be sorted using the entire length of the String, but this is not possible for TEXT
- Storage usage: TEXT occupies 2 + length of string storage space, while VARCHAR occupies 1 + length of string, up to 255 characters, and 2 + length of string greater than 255 characters. So, up to 255 characters, VARCHAR even uses lesser storage than TEXT.
- Performance Optimization: VARCHAR can be stored in MySQL’s memory storage; however, TEXT is not supported by it. So, if a query involves a TEXT column, temporary tables are created on the disk storage. Using disk-based tables takes a toll on the resources, and query run completion takes longer.
How to choose the right data type? Although it is pretty clear that VARCHAR is best in most cases, but a blog post or a detailed explanation of a complaint on a website can easily exceed its character limit. With VARCHAR. You need to define the data type while creating the data table, so you might not want it to be restricted by the character limit. Therefore, MEDIUMTEXT and LONGTEXT should be chosen for such fields. Below is an example of how to optimize the data types:
- Access the built-in tool of cPanel called phpMyAdmin
- Select your desired database from the list of databases of your website
- Check the structure of its tables to find out the data type
