为什么如果我创建一个 varchar(65535),我会得到一个mediumtext?

发布于 2024-12-08 03:57:27 字数 547 浏览 2 评论 0原文

mediumtext 和 varchar(65535) 是同一件事吗? 我这里的意思是它们都是直接将数据存储在表中还是 mediatext 类型存储指针? 我问这个问题是因为如果我尝试创建 varchar(65535) 它会自动转换为mediumtext(如果长度实际上大于21811)。


我有另一个相关问题:

我在表中有一些字段,我希望能够存储大量字符(大约 10'000)。但实际上,它几乎总是存储不超过 10 个字符。

所以我的问题是,我是否需要使用 text 或 varchar(10000) 类型才能获得最佳性能?

我相信 varchar 类型更适合这种情况。

预先感谢您的回答。

编辑

我不明白的是为什么他们说从 mysql 5.0.3 开始,我们可以创建一个 varchar(65535) 但当我们尝试这样做时,它会将其转换为中型文本。对我来说,varchar 和 text(包括mediumtext)之间的区别在于它们的存储方式(是否使用指针)。 那么为什么他们说我们可以创建长度为 65535 的 varchar 如果我们不能呢?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

缺⑴份安定 2024-12-15 03:57:27

我正在回答我自己的问题:

那是因为我使用的是多字节字符。如果您使用 utf-8 排序规则,则将无法创建大约超过 21000 个字符的 varchar。但如果你使用ascii,你就会的。

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.

佞臣 2024-12-15 03:57:27

根据 mysql.com,mediumtext 的长度是 L + 3 个字节。对我来说,这意味着如果你有一个空字段,它将是 3 个字节。如果您有一个空 varchar,则该字段的长度将为 1 个字节。

这里是明确提到

在 MySQL 5.0.3 之前,具有长度规范的 VARCHAR 列
大于255转换为可容纳的最小TEXT类型
给定长度的值。例如,VARCHAR(500) 转换为
TEXT,VARCHAR(200000) 转换为 MEDIUMTEXT。然而,这
转换会影响尾随空格的删除。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文