在PostgreSQL上使用varchar而不是varchar(n) [英] using varchar over varchar(n) with Postgresql

查看:1246
本文介绍了在PostgreSQL上使用varchar而不是varchar(n)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在设计一个数据库,因为我对数据类型还不满意,所以我想知道以下内容:

I'm designing a database, since I'm not yet confortable with the data types I was wondering the following :

如果我不在乎字符向量的长度,是否适合使用 varchar ? (即使期望字符串很短?)

If I don't care about the length of a vector of characters, is it appropriate to use varchar ? (even if the strings are expected to be quite short ?)

直到现在,当我需要使用 text (varchar)时,存储非常长的字符串,但阅读文档并询问周围的问题我听说如果提供了最大长度并且存储的字符串的长度小于它,postgresql不会存储多余的字节。
我想应该使用最大长度,以防遥远的设备负担不起大的存储空间。
可以同意还是解释?

Until now I've been using text (varchar) when I needed to store really long strings but reading the documentation and asking questions around I heard postgresql does not store the extra-bytes if the maximum-length is supplied and the stored string's length is lesser than it. I guess the maximum-length is used in case the distant device can't afford large memory space. Could one agree or explain that ?

推荐答案

从文档中获取:


这三种类型之间没有性能差异,除了使用空白填充类型时增加的存储空间,以及一些额外的CPU周期来检查存储为长度的长度-约束列。尽管character(n)在某些其他数据库系统中具有性能优势,但在PostgreSQL中却没有这种优势。实际上,character(n)通常是这三个中最慢的,因为它需要额外的存储成本。在大多数情况下,应改用文字或字符。

There is no performance difference among these three types, apart from increased storage space when using the blank-padded type, and a few extra CPU cycles to check the length when storing into a length-constrained column. While character(n) has performance advantages in some other database systems, there is no such advantage in PostgreSQL; in fact character(n) is usually the slowest of the three because of its additional storage costs. In most situations text or character varying should be used instead.

这篇关于在PostgreSQL上使用varchar而不是varchar(n)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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