Received: from localhost (postgresql.org [64.49.215.8]) by postgresql.org (Postfix) with ESMTP id C76B94761D4 for ; Sat, 19 Oct 2002 23:00:05 -0400 (EDT) Received: from candle.pha.pa.us (momjian.navpoint.com [207.106.42.251]) by postgresql.org (Postfix) with ESMTP id A6C114760A9 for ; Sat, 19 Oct 2002 23:00:03 -0400 (EDT) Received: (from pgman@localhost) by candle.pha.pa.us (8.11.6/8.10.1) id g9K2xr423634; Sat, 19 Oct 2002 22:59:53 -0400 (EDT) From: Bruce Momjian Message-Id: <200210200259.g9K2xr423634@candle.pha.pa.us> Subject: Re: indexing on char vs varchar In-Reply-To: <200210021055.22641.josh@agliodbs.com> To: josh@agliodbs.com Date: Sat, 19 Oct 2002 22:59:53 -0400 (EDT) Cc: Beth Gatewood , pgsql-sql@postgresql.org X-Mailer: ELM [version 2.4ME+ PL99 (25)] MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset=US-ASCII X-Virus-Scanned: by AMaViS new-20020517 X-Archive-Number: 200210/252 X-Sequence-Number: 9969 I have updated the FAQ to mention that char()/varchar()/text/bytea have similar performance characteristics. --------------------------------------------------------------------------- Josh Berkus wrote: > Beth, > > > Sorry....I don't understand. The length is at the front of what? > > In some RDBMSs, the VARCHAR data type has a 2 or 4-byte indicator of the > length of the stored string before the data itself, while CHAR does not > require this information because it is fixed-length. This makes the CHAR > datatype marginally smaller, and thus faster, than the VARCHAR data type on > those databases. This difference goes back to much older databases and > computers, where every byte of a row counted in terms of performance. > > In my experience, even though MS SQL Server still functions this way, the > performance difference between CHAR and VARCHAR is not measurable unless you > are getting close to the 8K data page limit that MSSQL imposes. YMMV. > > Postgres does not materially differentiate between CHAR, VARCHAR, and TEXT, > except that CHAR is padded by spaces and VARCHAR often has a length limit. > However, in terms of storage efficiency (and indexing efficiency), they are > identical. In Postgres, the character count is included in all string data > types. > > Thus, you should use the data type most appropriate to the data you are > storing, ignoring performance issues. If the data is a fixed-length string > (such as a required zip code) use CHAR; if it's variable but limited, use > varchar; if it's a long description, use TEXT. > > -- > -Josh Berkus > Aglio Database Solutions > San Francisco > > -- Bruce Momjian | http://candle.pha.pa.us pgman@candle.pha.pa.us | (610) 359-1001 + If your life is a hard drive, | 13 Roberts Road + Christ can be your backup. | Newtown Square, Pennsylvania 19073