何时在 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
When to use TEXT in mysql instead of VARCHAR
提问by enchance
Possible Duplicate:
MySQL: Large VARCHAR vs. TEXT?
可能的重复:
MySQL:大 VARCHAR 与文本?
Since VARCHAR
can have 65k bytes now, when then should TEXT
be used instead of VARCHAR
?
既然VARCHAR
现在可以有 65k 字节,那么什么时候应该TEXT
使用VARCHAR
?
回答by Lion
A long VARCHAR
is stored in the same manner as a TEXT
/BLOB
field 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。
Unless you need to index these columns (in which case VARCHAR
is much faster) there is no reason to use VARCHAR
over TEXT
for long fields - there are some engine specific optimisations in MySQL
to tune the data retrieval according to length, and you should use the correct column type to take advantage of these.
除非您需要索引这些列(在这种情况下VARCHAR
会更快),否则没有理由对长字段使用VARCHAR
over TEXT
- 有一些引擎特定的优化MySQL
可以根据长度调整数据检索,并且您应该使用正确的列类型来利用这些。
In case you're using MyISAM
an in-depth discussion on the topic is here.
如果您正在使用MyISAM
有关该主题的深入讨论,请点击此处。
TEXT
and BLOB
are stored off the table with the table just having a pointer to the location of the actual storage.
TEXT
并BLOB
存储在表外,表只有一个指向实际存储位置的指针。
VARCHAR
is stored inline with the table. VARCHAR
is faster when the size is reasonable.
VARCHAR
与表内联存储。VARCHAR
当大小合理时更快。
According to this test, VARCHAR
is 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 对应项慢得多。