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

后端 未结 3 641
萌比男神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?

提交回复
热议问题