MySQL: Large VARCHAR vs. TEXT?

后端 未结 8 1433
庸人自扰
庸人自扰 2020-11-22 09:21

I\'ve got a messages table in MySQL which records messages between users. Apart from the typical ids and message types (all integer types) I need to save the actual message

8条回答
  •  再見小時候
    2020-11-22 10:04

    Short answer: No practical, performance, or storage, difference.

    Long answer:

    There is essentially no difference (in MySQL) between VARCHAR(3000) (or any other large limit) and TEXT. The former will truncate at 3000 characters; the latter will truncate at 65535 bytes. (I make a distinction between bytes and characters because a character can take multiple bytes.)

    For smaller limits in VARCHAR, there are some advantages over TEXT.

    • "smaller" means 191, 255, 512, 767, or 3072, etc, depending on version, context, and CHARACTER SET.
    • INDEXes are limited in how big a column can be indexed. (767 or 3072 bytes; this is version and settings dependent)
    • Intermediate tables created by complex SELECTs are handled in two different ways -- MEMORY (faster) or MyISAM (slower). When 'large' columns are involved, the slower technique is automatically picked. (Significant changes coming in version 8.0; so this bullet item is subject to change.)
    • Related to the previous item, all TEXT datatypes (as opposed to VARCHAR) jump straight to MyISAM. That is, TINYTEXT is automatically worse for generated temp tables than the equivalent VARCHAR. (But this takes the discussion in a third direction!)
    • VARBINARY is like VARCHAR; BLOB is like TEXT.

    Rebuttal to other answers

    The original question asked one thing (which datatype to use); the accepted answer answered something else (off-record storage). That answer is now out of date.

    When this thread was started and answered, there were only two "row formats" in InnoDB. Soon afterwards, two more formats (DYNAMIC and COMPRESSED) were introduced.

    The storage location for TEXT and VARCHAR() is based on size, not on name of datatype. For an updated discussion of on/off-record storage of large text/blob columns, see this .

提交回复
热议问题