Mysql VARCHAR(X) vs TEXT

本文探讨了MySQL中Varchar与Text数据类型的使用场景。Varchar适合存储如用户名、邮箱等短文本;Text则适用于消息、评论等长文本。文章详细解释了两者的存储方式差异及性能对比,并提供了实用的选用建议。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一般情况下,我们不太会纠结用Varchar或text数据类型。
比如说,我们要存储邮箱,我们自然会用varchar,不会想到用text。而当我们要存储一段话的时候,选了text,感觉varchar也够用。当然感觉是没有用的,我们可以研究一下。

TEXT and BLOB is stored off the table with the table just having a pointer to the location of the actual storage.

VARCHAR is stored inline with the table. VARCHAR is faster when the size is reasonable, the tradeoff of which would be faster depends upon your data and your hardware, you'd want to benchmark a realworld scenario with your data.

以上引用大致是说:TEXT 和 BLOB 相当于是存储了一个位置指针,这个指针指向一个表结构数据。而VARCHAR是表内级别的数据单元。通过指针去找到表再找到数据,相对而言比表内找到一个数据要效率低些。当然TEXT、BLOB存储量比VARCHAR是要大些。

以下有个清单有助于选定数据该用哪种类型:

VARCHAR(X)

Case: user name, email, country, subject, password

TEXT

Case: messages, emails, comments, formatted text, html, code, images, links

MEDIUMTEXT

Case: large json bodies, short to medium length books, csv strings

LONGTEXT

Case: textbooks, programs, years of logs files, harry potter and the goblet of fire, scientific research logging

参考资料

http://stackoverflow.com/questions/2023481/mysql-large-varchar-vs-text

转载于:https://www.cnblogs.com/flowerszhong/p/5745188.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值