Skip site navigation (1) Skip section navigation (2)

Re: Varchar pkey instead of integer

From: Craig Ringer <craig(at)postnewspapers(dot)com(dot)au>
To:
Cc: pgsql-performance(at)postgresql(dot)org
Subject: Re: Varchar pkey instead of integer
Date: 2008-05-21 08:00:46
Message-ID: 4833D6AE.6060809@postnewspapers.com.au (view raw or flat)
Thread:
Lists: pgsql-performance
Shane Ambler wrote:

> Size can affect performance as much as anything else. In your case of 
> limited rows it will make little difference, though the larger table 
> with millions of rows will have this key entered for each row and be 
> indexed as the foreign key.
> 
> The real question is how you want to use the column, if you wish to 
> query for rows of a certain currency then you will notice the difference.
> 
> You could use a smallint of 2 bytes each (or a varchar(1) with an int 
> value instead of a real char) or an integer of 4 bytes, compared to your 
> varchar(10)

... and if there are only a few records in the currency column, it 
rarely changes, and you put a trigger in place to prevent the re-use of 
previously assigned keys you may be able to cache that data in your 
application.

That way you avoid a join or subquery on your lookup table to get the 
text description of the currency AND get the storage/performance of a 
small integer key.

It's something I'm doing in other places in my current DB where I have 
essentially static lookup tables. You do have to watch out for lookup 
table changes, though.

It's worth noting that my database is rather puny (the largest table has 
500,000 records) and I'm very, very far from an expert on any of this, 
so there might be some hidden downside to doing things this way that I 
just haven't hit yet.

--
Craig Ringer

In response to

pgsql-performance by date

Next:From: Albert Cervera ArenyDate: 2008-05-21 10:28:47
Subject: Posible planner improvement?
Previous:From: J. Andrew RogersDate: 2008-05-21 07:57:35
Subject: Re: Varchar pkey instead of integer

Privacy Policy | About PostgreSQL
Copyright © 1996-2014 The PostgreSQL Global Development Group