Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does a varchar field's declared size have any impact in PostgreSQL?

Is VARCHAR(100) any better than VARCHAR(500) from a performance point of view? What about disk usage?

Talking about PostgreSQL today, not some database some time in history.

like image 588
ibz Avatar asked Jul 01 '09 02:07

ibz


People also ask

Do you need to specify VARCHAR length in SQL?

The answer is you don't need to, it's optional. It's there if you want to ensure that strings do not exceed a certain length.

What size VARCHAR should I use?

String values that vary significantly in length and are no longer than 8,000 bytes should be stored in a VARCHAR column. If you have huge strings (over 8,000 bytes), then VARCHAR(MAX) should be used. In order to store VARCHAR columns, the length information along with the data is stored.

Does size of VARCHAR matter?

There is no performance impact whether you use the full length VARCHAR declaration VARCHAR(16777216) or use a smaller precision VARCHAR datatype column.


2 Answers

They are identical.

From the PostgreSQL documentation:

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.

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).

like image 127
the.jxc Avatar answered Oct 20 '22 00:10

the.jxc


There is no difference between varchar(m) and varchar(n)..

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.

http://archives.postgresql.org/pgsql-general/2009-04/msg00945.php

like image 22
rfusca Avatar answered Oct 19 '22 23:10

rfusca