Which DATATYPE is better to use TEXT or VARCHAR?

Mysql

Mysql Problem Overview


This question is based on two things performance and size

Which DATATYPE is better to use TEXT or VARCHAR? Based on performance which will affect and which will impove?

Mysql Solutions


Solution 1 - Mysql

It depends on what you're using it for. I hate to give such a generic answer, but it's true. Generally, try to get the data type as specific as you can. If your strings will never exceed some upper limit of characters, then go with VARCHAR because it will be a little more efficient. If you need more space, go with TEXT. If you aren't sure how much space your text will take up, you should probably go with TEXT; the performance difference isn't very large, and it's better to be future-proof than risk having to change it later when your requirements change. Just my two cents.


In the comments, Pitarou points out that, if MySQL creates a temporary table for your query (see this), TEXT columns will not be stored in memory and will have to be read from the disk, which is much slower. (Source, bottom of the page.) This shouldn't matter for most queries, though.

In case anyone was wondering how PostgreSQL compares, I found this benchmark that shows that CHAR, VARCHAR, and TEXT all perform equally well. So if you're using Postgres, it doesn't matter what type you use.

Solution 2 - Mysql

>From V 5.0.3 onwards, Limit of VARCHAR is increased from 0-256 to 0-65,535 (subject to the maximum row size (65,535 bytes, which is shared among all columns) and the character set used.) > > Ref. http://dev.mysql.com/doc/refman/5.0/en/char.html

If you are using TEXT that is fixed 64k length, even if you required lesser limit

So Better to go with VARCHAR with higher limit than TEXT. If requirement is more than 64K go with MEDIUMTEXT or LONGTEXT accordingly.

Solution 3 - Mysql

> Queries against the TEXT table were always 3 times slower than those against the VARCHAR table (averages: 0.10 seconds for the VARCHAR table, 0.29 seconds for the TEXT table). The difference is 100% repeatable.

Benchmark from http://forums.mysql.com/read.php?24,105964,105964

Solution 4 - Mysql

VARCHAR you can set a limit for how many chars it will accept per record, text is (virtually) unlimited... not exactly sure about performance, but i would assume a more specific datatype (varchar) would be faster.

Solution 5 - Mysql

VARCHAR should have a better performance since it has a limited size. In fact, in all of my experiences with MySQL, the search operation was always faster with VARCHAR than TEXT. Anyway, it's based on my experience. You should check the documentation to find out more about it.

Solution 6 - Mysql

It really depends on your data type.

If your field is fixed-length (e.g. a 32-character hash value), then use CHAR. This has better performance because every entry takes up the same space per row.

The standard limit for VARCHAR was 255 characters but I think it's been increased now. TEXT is pretty damn long and is generally only used for big content like a whole blog post, and comments if you don't want a limit.

With regard to size there is no (or very little) difference between VARCHAR and TEXT since they just store what they need to. CHAR fields will always take up their allotted length.

Performance-wise, VARCHAR is usually faster. VARCHARs can be indexed too which leads to faster searching.

Solution 7 - Mysql

MySQL will internally convert TEXT to varchar while creating temporary tables. So it is better to use VARCHAR if possible. There are a few minor bugs related to TEXT column such as...

http://bugs.mysql.com/bug.php?id=36676

Solution 8 - Mysql

As per my Opinion VARCHAR is best option when u know the length of characters. It will also reduce garbage Memory Allocations and space issue. TEXT will consume 255 where as VARCHAR will consume as u give the values to it.

As per performance, VARCHAR is also faster then TEXT.

Solution 9 - Mysql

There is a subtle difference in text and varchar. I have a table as shown:

CREATE TABLE `test`.`tbl`(
  `kee` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  `txt` TEXT(100),
  `vrchr` VARCHAR(100),
  PRIMARY KEY (`kee`)
);

I insert a row:

 INSERT INTO `tbl`
            (`txt`,
             `vrchr`)
VALUES ('1        
         2
         3',
        '1
         2
         3');

The column txt has value:
1
2
3

and column vrchr has value:
1

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionNigerView Question on Stackoverflow
Solution 1 - MysqlSasha ChedygovView Answer on Stackoverflow
Solution 2 - MysqlNoushadView Answer on Stackoverflow
Solution 3 - MysqlAvatarView Answer on Stackoverflow
Solution 4 - MysqlJasonView Answer on Stackoverflow
Solution 5 - MysqlFernandoView Answer on Stackoverflow
Solution 6 - MysqlDisgruntledGoatView Answer on Stackoverflow
Solution 7 - MysqlshantanuoView Answer on Stackoverflow
Solution 8 - MysqlSumeet GohelView Answer on Stackoverflow
Solution 9 - MysqlSashaView Answer on Stackoverflow