Received: from maia.hub.org (maia-3.hub.org [200.46.204.243]) by mail.postgresql.org (Postfix) with ESMTP id C66B51337B4F for ; Tue, 16 Nov 2010 12:28:46 -0400 (AST) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.243]) (amavisd-maia, port 10024) with ESMTP id 81098-06 for ; Tue, 16 Nov 2010 16:28:39 +0000 (UTC) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.6 Received: from lo.gmane.org (lo.gmane.org [80.91.229.12]) by mail.postgresql.org (Postfix) with ESMTP id 82BF91337B4E for ; Tue, 16 Nov 2010 12:28:39 -0400 (AST) Received: from list by lo.gmane.org with local (Exim 4.69) (envelope-from ) id 1PIOOb-0002FL-9Q for pgsql-performance@postgresql.org; Tue, 16 Nov 2010 17:28:37 +0100 Received: from 212.126.222.122 ([212.126.222.122]) by main.gmane.org with esmtp (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Tue, 16 Nov 2010 17:28:37 +0100 Received: from hari.fuchs by 212.126.222.122 with local (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Tue, 16 Nov 2010 17:28:37 +0100 X-Injected-Via-Gmane: http://gmane.org/ To: pgsql-performance@postgresql.org From: Harald Fuchs Subject: Re: best db schema for time series data? Date: Tue, 16 Nov 2010 17:28:19 +0100 Lines: 43 Message-ID: <86zkt9qllo.fsf@mgm.protecting.net> References: <20101116105055.GA26129@apartia.fr> <4CE2688B.2050000@tweakers.net> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Complaints-To: usenet@dough.gmane.org X-Gmane-NNTP-Posting-Host: 212.126.222.122 X-Archive: encrypt User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.2 (gnu/linux) Cancel-Lock: sha1:e0edL4GluXxLdLocWhsXdDBg4mg= X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-0.746 tagged_above=-10 required=5 tests=BAYES_00=-1.9, RCVD_NUMERIC_HELO=1.164, T_RP_MATCHES_RCVD=-0.01 X-Spam-Level: X-Archive-Number: 201011/265 X-Sequence-Number: 41289 In article <4CE2688B.2050000@tweakers.net>, Arjen van der Meijden 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.