Re: varchar as primary key

From: Jeff Davis <pgsql(at)j-davis(dot)com>
To: Alexander Staubo <alex(at)purefiction(dot)net>
Cc: Matthew Hixson <hixson(at)poindextrose(dot)org>, Postgres General <pgsql-general(at)postgresql(dot)org>
Subject: Re: varchar as primary key
Date: 2007-05-03 21:54:08
Message-ID: 1178229248.28383.296.camel@dogma.v10.wvs
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Thu, 2007-05-03 at 22:32 +0200, Alexander Staubo wrote:
> On 5/3/07, Matthew Hixson <hixson(at)poindextrose(dot)org> wrote:
> > Is there a significant performance difference between using int
> > primary keys and string primary keys in Postgres?
>
> PostgreSQL uses B-trees for its indexes, insertion time is logarithmic
> regardless of the type of the key, but strings have a larger overhead
> since they involve character comparisons; (i - j) is a lot faster than
> strcmp(i, j). If you do go for strings, I would suggest that the

If you're using a non-C locale, it's slower than strcmp() too.
PostgreSQL has to do an extra memcpy() in order to use strcoll(),
because strings in postgresql aren't necessarily NULL-terminated and
there's no such thing as strncoll(), unfortunately (a comment in the
code points this out).

This mostly matters in sorting. If your B-tree is in memory and you do a
simple lookup (what else would you do with UUIDs?), it's unlikely to
take very long at all.

Regards,
Jeff Davis

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Carlos Moreno 2007-05-03 22:42:12 Re: Feature Request --- was: PostgreSQL Performance Tuning
Previous Message Listmail 2007-05-03 21:41:55 Re: Indexing questions: Index == key? And index vs substring - how successful?