简体   繁体   中英

Which DATATYPE is better to use TEXT or VARCHAR?

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?

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.

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.

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

VARCHAR你可以设置每个记录接受多少个字符的限制,文本是(几乎)无限制的......不完全确定性能,但我会假设一个更具体的数据类型(varchar)会更快。

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.

It really depends on your data type.

If your field is fixed-length (eg 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.

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

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM