16
votes

Does a mediumtext and a varchar(65535) are the same thing ? What I mean here is do they both store the data directly in the table or do the mediumtext type is storing a pointer ? I'm asking this because if I try to create a varchar(65535) it is automatically transform into a mediumtext (if the length is bigger than 21811 actually).


I've got another related question which is:

I've got some fields in a table that I want to be able to store a lot of characters (around 10'000). But in reality, it will almost all the time store no more than 10 characters.

So my question is, do I need to use a text or a varchar(10000) type for best performance ?

I believe a varchar type is more appropriate for this case.

Thanks in advance for your answers.

Edit

The thing I don't understand is why they say that since mysql 5.0.3, we can create a varchar(65535) but when we try to do this, it convert it to a mediumtext. For me the difference between varchar and text (mediumtext include) is the way how they are store (using a pointer or not). So why do they say that we can create varchar with a length of 65535 if we can't?

2

2 Answers

14
votes

I'm answering to my own question:

That's because I'm using a multi-byte character. If you use an utf-8 collation, you will not be able to create a varchar with about more than 21000 chars. But if you use ascii, you will.

7
votes

According to mysql.com the length of mediumtext is L + 3 bytes. To me this means that if you have an empty field it will be 3 bytes. If you have an empty varchar the length of this field will be 1 byte.

Here is clearly mentioned

Prior to MySQL 5.0.3, a VARCHAR column with a length specification greater than 255 is converted to the smallest TEXT type that can hold values of the given length. For example, VARCHAR(500) is converted to TEXT, and VARCHAR(200000) is converted to MEDIUMTEXT. However, this conversion affects trailing-space removal.