varchar 字段的声明大小对 PostgreSQL 有任何影响吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1067061/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Does a varchar field's declared size have any impact in PostgreSQL?
提问by ibz
Is VARCHAR(100) any better than VARCHAR(500) from a performance point of view? What about disk usage?
从性能的角度来看,VARCHAR(100) 是否比 VARCHAR(500) 更好?磁盘使用情况如何?
Talking about PostgreSQL today, not some database some time in history.
今天谈论 PostgreSQL,而不是历史上某个时期的某个数据库。
回答by the.jxc
They are identical.
它们是相同的。
From the PostgreSQL documentation:
来自 PostgreSQL 文档:
http://www.postgresql.org/docs/8.3/static/datatype-character.html
http://www.postgresql.org/docs/8.3/static/datatype-character.html
Tip: There are no performance differences between these three types, apart from increased storage size when using the blank-padded type, and a few extra cycles to check the length when storing into a length-constrained column. While character(n) has performance advantages in some other database systems, it has no such advantages in PostgreSQL. In most situations text or character varying should be used instead.
提示:这三种类型之间没有性能差异,除了使用空白填充类型时增加了存储大小,以及在存储到长度受限列时检查长度的一些额外周期。虽然 character(n) 在其他一些数据库系统中具有性能优势,但它在 PostgreSQL 中没有这样的优势。在大多数情况下,应改用文本或字符变化。
Here they are talking about the differences between char(n), varchar(n) and text (= varchar(1G)). The official story is that there is no difference between varchar(100) and text (very large varchar).
这里他们在谈论 char(n)、varchar(n) 和 text (= varchar(1G)) 之间的区别。官方说法是 varchar(100) 和 text(非常大的 varchar)没有区别。
回答by MkV
TEXT /is/ the same as VARCHAR without an explicit length, the text
TEXT /is/ 与没有明确长度的 VARCHAR 相同,文本
"The storage requirement for a short string (up to 126 bytes) is 1 byte plus the actual string, which includes the space padding in the case of character. Longer strings have 4 bytes overhead instead of 1. Long strings are compressed by the system automatically, so the physical requirement on disk might be less. Very long values are also stored in background tables so that they do not interfere with rapid access to shorter column values. In any case, the longest possible character string that can be stored is about 1 GB."
“短字符串(最多 126 个字节)的存储要求是 1 个字节加上实际字符串,其中包括字符情况下的空格填充。较长的字符串有 4 个字节的开销,而不是 1 个。长字符串由系统压缩自动,因此对磁盘的物理要求可能会更少。很长的值也存储在后台表中,这样它们就不会干扰对较短列值的快速访问。无论如何,可以存储的最长字符串大约是1 GB。”
refers to both VARCHAR and TEXT (since VARCHAR(n) is just a limited version of TEXT). Limiting your VARCHARS artificially has no real storage or performance benefits (the overhead is based on the actual length of the string, not the length of the underlying varchar), except possibly for comparisons against wildcards and regexes (but at the level where that starts to matter, you should probably be looking at something like PostgreSQL's full-text indexing support).
指的是 VARCHAR 和 TEXT(因为 VARCHAR(n) 只是 TEXT 的有限版本)。人为限制你的 VARCHARS 没有真正的存储或性能优势(开销是基于字符串的实际长度,而不是底层 varchar 的长度),除了可能与通配符和正则表达式的比较(但在开始的级别)重要的是,您可能应该查看诸如 PostgreSQL 的全文索引支持之类的内容)。
回答by rfusca
There is no difference between varchar(m)
and varchar(n)
..
varchar(m)
和varchar(n)
..之间没有区别
http://archives.postgresql.org/pgsql-admin/2008-07/msg00073.php
http://archives.postgresql.org/pgsql-admin/2008-07/msg00073.php
There is a difference between varchar(n)
and text
though, varchar(n)
has a built in constraint which must be checked and is actually a little slower.
varchar(n)
和text
虽然之间存在差异,但varchar(n)
具有必须检查的内置约束,实际上速度稍慢。
http://archives.postgresql.org/pgsql-general/2009-04/msg00945.php
http://archives.postgresql.org/pgsql-general/2009-04/msg00945.php