何时在 mysql 中使用 TEXT 而不是 VARCHAR

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11441823/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 14:09:03  来源:igfitidea点击:

When to use TEXT in mysql instead of VARCHAR

mysql

提问by enchance

Possible Duplicate:
MySQL: Large VARCHAR vs. TEXT?

可能的重复:
MySQL:大 VARCHAR 与文本?

Since VARCHARcan have 65k bytes now, when then should TEXTbe used instead of VARCHAR?

既然VARCHAR现在可以有 65k 字节,那么什么时候应该TEXT使用VARCHAR?

回答by Lion

A long VARCHARis stored in the same manner as a TEXT/BLOBfield in InnoDB.

longVARCHAR的存储方式与 中的TEXT/BLOB字段相同InnoDB

From storage prospective BLOB, TEXT as well as long VARCHAR are handled same way by Innodb. This is why Innodb manual calls it “long columns” rather than BLOBs.

从存储前景来看,BLOB、TEXT 和 long VARCHAR 由 Innodb 以相同的方式处理。这就是为什么 Innodb 手册称它为“长列”而不是 BLOB。

source

来源

Unless you need to index these columns (in which case VARCHARis much faster) there is no reason to use VARCHARover TEXTfor long fields - there are some engine specific optimisations in MySQLto tune the data retrieval according to length, and you should use the correct column type to take advantage of these.

除非您需要索引这些列(在这种情况下VARCHAR会更快),否则没有理由对长字段使用VARCHARover TEXT- 有一些引擎特定的优化MySQL可以根据长度调整数据检索,并且您应该使用正确的列类型来利用这些。

In case you're using MyISAMan in-depth discussion on the topic is here.

如果您正在使用MyISAM有关该主题的深入讨论,请点击此处



TEXTand BLOBare stored off the table with the table just having a pointer to the location of the actual storage.

TEXTBLOB存储在表外,表只有一个指向实际存储位置的指针。

VARCHARis stored inline with the table. VARCHARis faster when the size is reasonable.

VARCHAR与表内联存储。VARCHAR当大小合理时更快。

According to this test, VARCHARis about thrice as fast as text.

根据这个测试VARCHAR大约是文本的三倍。

回答by TimR

Text should be used for really long strings of indeterminate length. Also, queries that return TEXT fields tend to be much slower than their VARCHAR counterparts.

文本应该用于不确定长度的非常长的字符串。此外,返回 TEXT 字段的查询往往比它们的 VARCHAR 对应项慢得多。