Re: statement-level trigger sample out there?

From: hubert depesz lubaczewski <depesz(at)depesz(dot)com>
To: Christian Kindler <christian(dot)kindler(at)gmx(dot)net>
Cc: Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com>, Stephen Cook <sclists(at)gmail(dot)com>, pgsql-sql(at)postgresql(dot)org
Subject: Re: statement-level trigger sample out there?
Date: 2007-11-30 09:41:46
Message-ID: 20071130094146.GA10972@depesz.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

On Thu, Nov 29, 2007 at 04:04:54PM +0100, Christian Kindler wrote:
> I have a realy big table (> 2'000'000 records). every second there are several inserts and updates. the thing is i need a last row reference depending on a foreing_key.
>
> something like this:
>
> id, foreign_key, last_id, value1, value1, date
> >1<, 3, null, 12, 13, 2007-01-01
> >2<, 4, null, 11, 10, 2007-01-01
> 4, 3, >1<, 12, 13, 2007-01-02
> 5, 4, >2<, 11, 10, 2007-01-02
> ...
>
> of course the sequence can have holes so I have to calculate the real last row id. for now i calculate for each row by invoking a "select max(id) where foreign_key = $1" but this cost a lot of performance. I could do this easily with one update for the whole query - if i could know which foreign_key and which date range was performed.

create index q on table (foreing_key, id);
and then:
select max(id) from table where foreing_key = ?;
should be very fast.
if it is not:
select id from table where foreing_key = ? order by foreing_key desc, id desc limit 1;

will be fast.

of course - remember about vacuum/analyze.

depesz

--
quicksil1er: "postgres is excellent, but like any DB it requires a
highly paid DBA. here's my CV!" :)
http://www.depesz.com/ - blog dla ciebie (i moje CV)

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Daniel Caune 2007-11-30 15:00:11 Using schema
Previous Message Gerardo Herzig 2007-11-29 20:08:38 Re: statement-level trigger sample out there?