问题描述
我在 MySQL 中有一个消息表,用于记录用户之间的消息.除了典型的 id 和消息类型(所有整数类型),我需要将实际的消息文本保存为 VARCHAR 或 TEXT.我将前端限制设置为 3000 个字符,这意味着消息将永远不会插入到数据库中超过此长度.
I've got a messages table in MySQL which records messages between users. Apart from the typical ids and message types (all integer types) I need to save the actual message text as either VARCHAR or TEXT. I'm setting a front-end limit of 3000 characters which means the messages would never be inserted into the db as longer than this.
使用 VARCHAR(3000) 或 TEXT 是否有理由?仅仅编写 VARCHAR(3000) 有点违反直觉.我已经阅读过 Stack Overflow 上的其他类似帖子,但如果能获得特定于此类常见消息存储的视图会很好.
Is there a rationale for going with either VARCHAR(3000) or TEXT? There's something about just writing VARCHAR(3000) that feels somewhat counter-intuitive. I've been through other similar posts on Stack Overflow but would be good to get views specific to this type of common message storing.
推荐答案
TEXT
和BLOB
可以存储在表外,而表只有一个指向实际存储位置的指针.它的存储位置取决于很多因素,例如数据大小、列大小、row_format 和 MySQL 版本.TEXT
andBLOB
may by stored off the table with the table just having a pointer to the location of the actual storage. Where it is stored depends on lots of things like data size, columns size, row_format, and MySQL version.VARCHAR
与表内联存储.VARCHAR
在大小合理时更快,权衡更快取决于您的数据和您的硬件,您希望使用您的数据对实际场景进行基准测试.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 real-world scenario with your data.这篇关于MySQL:大 VARCHAR 与文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!