sql - 在 MySQL 中使用文本而不是 varchar 类型的实际性能影响是什么?

标签 sql mysql database varchar

在 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/

相关文章:

mysql - O 和 Ö 与 Zend_Db 中的 ORDER 排序相同的字符

php - 如何插入新行并更新旧行值?

PHP 变量声明在 Laravel 5.4 中不起作用

mysql - 将数据库附加到 Web 容器时,Docker 数据库连接拒绝错误

php - 查询数据库时使用 Eloquent 模型方法

PHP子论坛内子论坛的树形遍历

c# - 如何获得相同时间的日期时间?

sql - select and join 错误必须出现在 GROUP BY 子句中或在聚合函数中使用

sql - SSRS/MDX - 从多维数据集提取数据集的表需要一个数据位于 sql 数据库中的列

sql - 如何选择事务隔离级别?