Varchar or Text Data Types for strings length up to few thousands of chars

后端 未结 3 639
萌比男神i
萌比男神i 2021-01-14 10:00

I have a small social networking website, with posts and comments.
I decided to let users create posts with any number of charس they want, and I think the best Datatype

相关标签:
3条回答
  • 2021-01-14 10:08

    There are various places where VARCHAR and TEXT do or do not differ. Some things I say:

    • If the maximum length is more than 512 characters, go with TEXT.
    • Never use TINYTEXT -- it has only negatives relative to VARCHAR(255).
    • Don't use VARCHAR(255); pick a reasonable max instead of 255. (This is a minor optimization relating to temp table in complex queries.)
    • Use CHAR only for things that are truly fixed length. In almost all such case, tack on CHARACTER SET ascii (or latin1). (Else it will take more space than you expect.
    • Use CHARACTER SET ut8mb4. (Exceptions are becoming more an more rare.)

    (Sorry, I digress. Back on topic...)

    I indexing, in layout of InnoDB rows (there are 4 different ROW_FORMATs), etc, VARCHAR(513) will be essentially indistinguishable from TEXT.

    One of the few arguments for VARCHAR is that it limits the store to the length given. But how often is that important?

    0 讨论(0)
  • 2021-01-14 10:19

    Values in VARCHAR columns are variable-length strings. The length can be specified as a value from 0 to 65,535, which would seem big enough to handle your user's expected comment length.

    The four TEXT types are TINYTEXT, TEXT, MEDIUMTEXT, and LONGTEXT. The maximum amount of data that can be stored for each data type is approximately:

    TINYTEXT 256 bytes

    TEXT 65,535 bytes ~64kb

    MEDIUMTEXT 16,777,215 bytes ~16MB

    LONGTEXT 4,294,967,295 bytes ~4GB

    See also When to use TEXT in mysql instead of VARCHAR

    0 讨论(0)
  • 2021-01-14 10:27

    Use one of the four type of TEXT available [TINYTEXT, TEXT, MEDIUMTEXT, and LONGTEXT] depending on your requirements. http://dev.mysql.com/doc/refman/5.7/en/storage-requirements.html

    0 讨论(0)
提交回复
热议问题