哪种DATATYPE最好使用TEXT或VARCHAR? [英] Which DATATYPE is better to use TEXT or VARCHAR?

查看:97
本文介绍了哪种DATATYPE最好使用TEXT或VARCHAR?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此问题基于两个因素:性能大小

This question is based on two things performance and size

哪种DATATYPE更好地使用TEXT或VARCHAR?基于会影响和不会改善的性能?

Which DATATYPE is better to use TEXT or VARCHAR? Based on performance which will affect and which will impove?

推荐答案

这取决于您使用它的目的.我讨厌给出这样的通用答案,但这是事实.通常,请尝试获取尽可能具体的数据类型.如果您的字符串永远不会超过某个字符的上限,请使用VARCHAR,因为这样做会更有效率.如果需要更多空间,请使用TEXT.如果不确定您的文本将占用多少空间,则可能应该使用TEXT;否则,请使用TEXT.性能差异不是很大,并且与将来在需求变化时必须进行更改的风险相比,与时俱进要好,以确保面向未来.只是我的两分钱.

It depends on what you're using it for. I hate to give such a generic answer, but it's true. Generally, try to get the data type as specific as you can. If your strings will never exceed some upper limit of characters, then go with VARCHAR because it will be a little more efficient. If you need more space, go with TEXT. If you aren't sure how much space your text will take up, you should probably go with TEXT; the performance difference isn't very large, and it's better to be future-proof than risk having to change it later when your requirements change. Just my two cents.

Pitarou在评论中指出,如果MySQL为您的查询创建了一个临时表(请参见来源,页面底部.)不过,对于大多数查询而言,这很重要.

In the comments, Pitarou points out that, if MySQL creates a temporary table for your query (see this), TEXT columns will not be stored in memory and will have to be read from the disk, which is much slower. (Source, bottom of the page.) This shouldn't matter for most queries, though.

如果有人想知道PostgreSQL的比较方式,我发现此基准,它表明CHAR,VARCHAR和TEXT的性能均相同.因此,如果您使用的是Postgres,则使用哪种类型都没关系.

In case anyone was wondering how PostgreSQL compares, I found this benchmark that shows that CHAR, VARCHAR, and TEXT all perform equally well. So if you're using Postgres, it doesn't matter what type you use.

这篇关于哪种DATATYPE最好使用TEXT或VARCHAR?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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