pg.ddx.io  pgsql-performance@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Louis-David Mitterrand <vindex+lists-pgsql-performance@apartia.org>
To: pgsql-performance@postgresql.org
Subject: Re: best db schema for time series data?
Date: Fri, 19 Nov 2010 10:50:21 +0100
Message-ID: <20101119095021.GB27168@apartia.fr> (raw)
In-Reply-To: <86zkt9qllo.fsf@mgm.protecting.net>
References: <20101116105055.GA26129@apartia.fr>
	<4CE2688B.2050000@tweakers.net>
	<86zkt9qllo.fsf@mgm.protecting.net>

On Tue, Nov 16, 2010 at 05:28:19PM +0100, Harald Fuchs wrote:
> In article <4CE2688B.2050000@tweakers.net>,
> Arjen van der Meijden <acmmailing@tweakers.net> writes:
> 
> > On 16-11-2010 11:50, Louis-David Mitterrand wrote:
> >> I have to collect lots of prices from web sites and keep track of their
> >> changes. What is the best option?
> >> 
> >> 1) one 'price' row per price change:
> >> 
> >> create table price (
> >> id_price primary key,
> >> id_product integer references product,
> >> price integer
> >> );
> >> 
> >> 2) a single 'price' row containing all the changes:
> >> 
> >> create table price (
> >> id_price primary key,
> >> id_product integer references product,
> >> price integer[] -- prices are 'pushed' on this array as they change
> >> );
> >> 
> >> Which is bound to give the best performance, knowing I will often need
> >> to access the latest and next-to-latest prices?
> 
> > If you mostly need the last few prices, I'd definitaly go with the
> > first aproach, its much cleaner. Besides, you can store a date/time
> > per price, so you know when it changed. With the array-approach that's
> > a bit harder to do.
> 
> I'd probably use a variant of this:
> 
>   CREATE TABLE prices (
>     pid int NOT NULL REFERENCES products,
>     validTil timestamp(0) NULL,
>     price int NOT NULL,
>     UNIQUE (pid, validTil)
>   );
> 
> The current price of a product is always the row with validTil IS NULL.
> The lookup should be pretty fast because it can use the index of the
> UNIQUE constraint.

Hi,

The validTil idea is nice, but you have to manage that field with a
trigger, right?



view thread (14+ messages)  latest in thread

Message-ID: <20101119095021.GB27168@apartia.fr>
Permalink:  ../20101119095021.GB27168@apartia.fr/
Also on:    postgresql.org/message-id/20101119095021.GB27168@apartia.fr

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-performance@postgresql.org
  Cc: vindex+lists-pgsql-performance@apartia.org
  Subject: Re: best db schema for time series data?
  In-Reply-To: <20101119095021.GB27168@apartia.fr>
  References: <20101116105055.GA26129@apartia.fr> <4CE2688B.2050000@tweakers.net> <86zkt9qllo.fsf@mgm.protecting.net> <20101119095021.GB27168@apartia.fr>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox