“不建议 MySQL 使用 Text 类型”,对于 Text 内容比较短小也不能用吗?

2020-11-24 17:48:48 +08:00
 miniyao
一般知道 longtext 和大的 blob 放在 MySQL 里会影响性能。

如果只是放一些短小 tinytext 、text 也会影响性能吗?

比如有个字段大约在 300~500 个字符,那要么用 varchar(512),或者直接用 text,这两种方式,性能差别会很大吗?
6808 次点击
所在节点    MySQL
29 条回复
RedisMasterNode
2020-11-25 10:00:21 +08:00
@wnanbei varchar 和 text 不会转互
no1xsyzy
2020-11-25 10:24:54 +08:00
@loading https://tutorial.ponylang.io/
The Pony Philosophy: Get Stuff Done
wnanbei
2020-11-25 14:24:11 +08:00
@RedisMasterNode 嗯?

For a column that has a data type of VARCHAR or one of the TEXT types, changes the data type as necessary to ensure that the new column is long enough to store as many characters as the original column. For example, a TEXT column has two length bytes, which store the byte-length of values in the column, up to a maximum of 65,535. For a TEXT column, each character requires a single byte, so the column can store up to 65,535 characters. If the column is converted to , each character might require up to three bytes, for a maximum possible length of 3 × 65,535 = 196,605 bytes. That length does not fit in a TEXT column's length bytes, so MySQL converts the data type to MEDIUMTEXT, which is the smallest string type for which the length bytes can record a value of 196,605. Similarly, a VARCHAR column might be converted to MEDIUMTEXT. CONVERT TO CHARACTER SETlatin1utf8

https://dev.mysql.com/doc/refman/8.0/en/alter-table.html
wnanbei
2020-11-25 14:26:41 +08:00
@RedisMasterNode 还有

Specifying the attribute for a character data type causes the column to be created as the corresponding binary data type: CHAR becomes BINARY, VARCHAR becomes VARBINARY, and TEXT becomes BLOB. For the ENUM and SET data types, this does not occur; they are created as declared. Suppose that you specify a table using this definition: CHARACTER SET binary

https://dev.mysql.com/doc/refman/8.0/en/silent-column-changes.html
RedisMasterNode
2020-11-25 14:44:36 +08:00
@wnanbei 请注意这部分规则触发的条件,varchar 类型不会在你进行数据改动时转换为 text,这部分操作时在 create/alter table 时触发的,并且条件较为严苛

In some cases, MySQL silently changes column specifications from those given in a CREATE TABLE or ALTER TABLE statement.
- If strict SQL mode is not enabled, a VARCHAR column with a length specification greater than **65535** is converted to TEXT.

而在数据类型确认下来后,如果没有 alter table,只对数据进行改动,数据类型是不会变化的
wnanbei
2020-11-26 09:26:33 +08:00
@RedisMasterNode 既然不管在 create 语句还是 alter 语句的时候都会静默更改类型,那么这不就是等价吗
RedisMasterNode
2020-11-26 10:08:17 +08:00
@wnanbei 嗯,那这个是我理解错了,我以为你说的是 varchar 在使用过程中会根据长度转 text,这个是我整错了
DEVN
2020-12-17 07:58:44 +08:00
谁又会把 text 和 varchar 做比较呢?
rebeccaMyKid
2023-04-24 17:14:11 +08:00
@wd
@Flymachine
啊? 最长的 LONGTEXT 也最多存 4GB 啊,你哪里看到的是 unlimited 的?

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://tanronggui.xyz/t/728806

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX