5

I know the principle of using the smallest data type possible, but I wonder if this would apply to TEXT / MEDIUMTEXT / LONGTEXT in MySQL? Is there a performance issue if I use LONGTEXT instead of MEDIUMTEXT? I've found the question What is the disadvantage to using a MySQL longtext sized field when every entry will fit within a mediumtext sized field? but I think this isn't the answer to the performance question?

Is it just a matter of a bit more storage for LONGTEXT?

When should I prefer MEDIUMTEXT to LONGTEXT?

4
  • You should use the data type you really need .. the difference is only related to the max number of char .allowed for each data type ..the *TEXT datatype have dinamic length .. and use only the space required by the content (plus some few bytes for le set the single real length)
    – ScaisEdge
    Commented Oct 3, 2019 at 19:44
  • Complementing what @scaisEdge wrote, If the inserted data of the field is too long it would compromise performance in two ways, querying it conditionally and querying it to get the value, like a field with a base64 image, but only because of the size of the text inserted there not by the type. So if you have a TEXT with maximum size and a LONGTEXT with one char, the LONGTEXT will be queried faster. Commented Oct 3, 2019 at 20:09
  • So for example, I have values about 100000 chars, for the read/write performance it doesn't matter if I use TEXT, MEDIUMTEXT or LONGTEXT? In this case I would always use LONGTEXT to have the larger limit for the future. I still don't see any advantage of the smaller text types? Commented Oct 3, 2019 at 20:35
  • If you know that your data will fit in the smaller type, you save a byte or two per record because the length field is smaller. If you have many millions of records, those bytes add up.
    – Barmar
    Commented Oct 3, 2019 at 21:00

1 Answer 1

5

The only difference is the length field in the row data. Using MEDIUMTEXT instead of LONGTEXT saves 1 byte per record. If you have 100 million records, that saves 100 MB. There was a time when that was a significant amount of disk space.

The difference could also be significant if you're running up against the size limit for database rows. The text of *TEXT data is stored in files external to the table data, so it doesn't count against the limit, but I believe the size field is in the table so it does count.

But if neither of these is an issue, go ahead and use the largest type, to future-proof your schema.

3
  • 1
    I'm pretty sure that once you get beyond what can be stored on-record, the 1/2/3/4-byte "length" thingie vanishes. That is, the 3 never manifests itself in InnoDB storage. There is a 20-byte pointer to the off-row overflow, regardless of the size of the overflow.
    – Rick James
    Commented Oct 12, 2019 at 23:01
  • @RickJames Good points. It might be more significant if you use MEDIUMTEXT when you only need TEXT, Then you're not filling up the disk, but you're still wasting space on length bytes.
    – Barmar
    Commented Oct 13, 2019 at 19:20
  • 1
    The blind use of BIGINT wastes space faster.
    – Rick James
    Commented Oct 14, 2019 at 1:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.