MySQL中的VARCHAR vs TEXT [英] VARCHAR vs TEXT in MySQL

查看:251
本文介绍了MySQL中的VARCHAR vs TEXT的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个字段:一个用于存储最大大小为500个字符的片段,另一个用于存储描述最多不得超过10,000个字符。

I have two fields: one to store an excerpt with a max size of 500 characters, and another to store a description with a max size of 10,000 characters.

我应该使用哪些数据类型, TEXT c $ c> VARCHAR ?为什么?

What data types should I use, TEXT or VARCHAR? And why?

MySQL 5.0.3之后VARCHAR接受〜65000个字符。但是这不能告诉我为什么要使用一种类型和另一种类型。

After MySQL 5.0.3 VARCHAR accepts ~65000 characters. But this does not tell why I should use one type and or the other.

我推理应该使用 VARCHAR ,因为我可以分配一个大小限制, TEXT 描述字段,因为它更大。

I'm reasoning that I should use VARCHAR for the excerpt because I can assign a size limit, and TEXT for the description field as it's larger.

推荐答案

一个长的 VARCHAR code> TEXT / 中的BLOB 字段InnoDB 重新使用事务性,引用完整性和崩溃恢复,对吗?) - 也就是说,在磁盘上的其余表(可能需要另一个磁盘读取来检索)的外部。

A long VARCHAR is stored in the same manner as a TEXT/BLOB field in InnoDB (which I assume you're using for transactionality, referential integrity and crash recovery, right?) - that is, externally to the rest of the table on disk (which may require another disk read to retrieve).


从存储前景BLOB,TEXT作为
以及long VARCHAR由Innodb处理相同的
方式。这是为什么Innodb
手动调用它是长列,而不是BLOBs

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.

来源

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

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.

如果您使用 MyISAM ,则有关此主题的深入讨论为此处

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

这篇关于MySQL中的VARCHAR vs TEXT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆