Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL best way to store long strings

Tags:

mysql

I'm looking for some advice on the best way to store long strings of data from the mySQL experts.

I have a general purpose table which is used to store any kind of data, by which I mean it should be able to hold alphanumeric and numeric data. Currently, the table structure is simple with an ID and the actual data stored in a single column as follows:

id INT(11) data VARCHAR(128) 

I now have a requirement to store a larger amount of data (up to 500 characters) and am wondering whether the best way would be to simply increase the varchar column size, or whether I should add a new column (a TEXT type column?) for the times I need to store longer strings.

If any experts out there has any advice I'm all ears! My preferred method would be to simply increase the varchar column, but that's because I'm lazy. The mySQL version I'm running is 5.0.77.

I should mention the new 500 character requirement will only be for the odd record; most records in the table will be not longer than 50 characters. I thought I'd be future-proofing by making the column 128. Shows how much I knew!

like image 551
Damian Avatar asked Mar 14 '13 10:03

Damian


People also ask

How do I save a large string in MySQL?

We can use varchar(<maximum_limit>) . The maximum limit that we can pass is 65535 bytes. Note: This maximum length of a VARCHAR is shared among all columns except TEXT/BLOB columns and the character set used.

Which is the best way to store an attribute of large string value in SQL?

The MEDIUMTEXT data object is useful for storing larger text strings like white papers, books, and code backup. These data objects can be as large as 16 MB (expressed as 2^24 -1) or 16,777,215 characters and require 3 bytes of overhead storage.

Can MySQL store large TEXT?

LONGTEXT can store the maximum characters among all four, up to 4,294,967,295 characters i,e 4,294,967,295 bytes or 4GB. This is more than enough storage for any long-form text strings. For example, a book that MEDIUMTEXT can't hold can be stored using LONGTEXT. LONGTEXT takes 4-Bytes overhead.

Which is better VARCHAR or TEXT in MySQL?

In most circumstances, VARCHAR provides better performance, it's more flexible, and can be fully indexed. If you need to store longer strings, use MEDIUMTEXT or LONGTEXT, but be aware that very large amounts of data can be stored in columns of these types.


1 Answers

Generally speaking, this is not a question that has a "correct" answer. There is no "infinite length" text storage type in MySQL. You could use LONGTEXT, but that still has an (absurdly high) upper limit. Yet if you do, you're kicking your DBMS in the teeth for having to deal with that absurd blob of a column for your 50-character text. Not to mention the fact that you hardly do anything with it.

So, most futureproofness(TM) is probably offered by LONGTEXT. But it's also a very bad method of resolving the issue. Honestly, I'd revisit the application requirements. Storing strings that have no "domain" (as in, being well-defined in their application) and arbitrary length is not one of the strengths of RDBMS.

If I'd want to solve this on the "application design" level, I'd use NoSQL key-value store for this (and I'm as anti-NoSQL-hype as they get, so you know it's serious), even though I recognize it's a rather expensive change for such a minor change. But if this is an indication of what your DBMS is eventually going to hold, it might be more prudent to switch now to avoid this same problem hundred times in the future. Data domain is very important in RDBMS, whereas it's explicitly sidelined in non-relational solutions, which seems to be what you're trying to solve here.

Stuck with MySQL? Just increase it to VARCHAR(1000). If you have no requirements for your data, it's irrelevant what you do anyway.

like image 143
Naltharial Avatar answered Sep 21 '22 09:09

Naltharial