在 MySQL 中何时使用 text
类型而不是大型 varchar
是否有任何比较、个人经验或指南?
虽然我数据库中的大部分条目都少于 1000 个字符,但有些可能需要多达 4000 个字符或更多。 varchar
的限制长度是多少,这使得 text
成为更好的变体?
我不需要索引那些字段。
最佳答案
我没有个人经验,但这个人有:
VARCHAR vs. TEXT - some performance numbers
快速回答:varchar 要快一点。
编辑 - 不,不是。他以不同的方式为它们编制索引——他在 varchar(255 个字符)上有一个完整索引,但在文本上有一个 255 个字符的前缀索引。当他删除它时,它们的表现大致相同。
在线程的后面是这个有趣的花絮:
When a tmp table is needed for a SELECT, the first choice is to use MEMORY, which will be RAM-only, hence probably noticeably faster. (Second choice is MyISAM.) However, TEXT and BLOB are not allowed in MEMORY, so it can't use it. (There are other reasons why it might skip MEMORY.)
编辑 2 - 一些更相关的信息,这次是比较不同索引处理各种类型的方式。
MyISAM puts TEXT and BLOB 'inline'. If you are searching a table (range scan / table scan), you are 'stepping over those cow paddies' -- costly for disk I/O. That is, the existence of the inline blob hurts performance in this case.
InnoDB puts only 767 bytes of a TEXT or BLOB inline, the rest goes into some other block. This is a compromise that sometimes helps, sometimes hurts performance.
Something else (Maria? Falcon? InnoDB plugin?) puts TEXTs and BLOBs entirely elsewhere. This would make a noticeable difference in performance when compared to VARCHAR. Sometimes TEXT would be faster (eg, range scan that does not need the blob); sometimes the VARCHAR would be faster (eg, if you need to look at it and/or return it).
关于sql - 在 MySQL 中使用文本而不是 varchar 类型的实际性能影响是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2017444/