What data types exist in MySQL?
What is best for storing long texts? (for example in the form of HTML) Why?
What data types exist in MySQL?
What is best for storing long texts? (for example in the form of HTML) Why?
It depends on what you need. It has no details, context.
A VARCHAR
allows 65535 characters ( bytes if you are using an old version
TEXT
usually has slower access in most situations. Nothing very significant but it is common for it to require more disk accesses than TEXT
even having the same size. As the text is stored separately, it looks like it has a different table and is making a VARCHAR
, although the process is different.
Temporary tables with any type of JOIN
need to be on disk , which is much slower than normal tables that can only be in memory.
Using a TEXT
in common indexes (not full-text ) you need to determine the key size. You can not index the entire column, you have to say how many characters you want in the index. And another important point is that the texts in the key are always stored with spaces so that all keys are the same size. This is inefficient and if you do not know what you are doing, you will get fake results.
TEXT
does not allow values TEXT
, DEFAULT
yes.
I've seen some people say that they should always use VARCHAR
. This is not true. It has disadvantages. Use TEXT
until you have a reason to use VARCHAR
.
Your case seems to require at least a TEXT
, not only by size, but by the semantics of what you are storing. But if some of these restrictions create a problem for you and you can ensure that these HTMLs are small and will not have multiple on the same line, do not drop MEDIUMTEXT
.
The type of data you intend to use ( TEXT
) supports data of at most around 65kb .
In order to have a reference, the HTML returned from the news portal Globo.com has the size of ~ 48kb, and BBC , more than 100kb .
The two examples above show that an HTML file can easily exceed the maximum limit of a TEXT
field of MySQL.
With this in mind, there would be two other types of text data that would be most appropriate for you:
MEDIUMTEXT
= supports up to ~ 16MB of data.
LONGTEXT
= supports up to ~ 4,000MB of data.
By way of comparison, if a field is MEDIUMTEXT
, it would support more than 100,000 BBC "HTML codes" before reaching the limit. If it were a LONGTEXT
, it would be more than 42 million copies of the BBC HTML code.
So, in your case, I imagine a MEDIUMTEXT
would already provide more than enough space to store HTML page codes.