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

Peripheral Links

Header And Logo

PostgreSQL
| The world's most advanced open source database.

Site Navigation

Search for
  Advanced Search

Re: statement-level trigger sample out there?



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)



Home | Main Index | Thread Index

Privacy Policy | PostgreSQL Archives hosted by Command Prompt, Inc. | Designed by tinysofa
Copyright © 1996 – 2008 PostgreSQL Global Development Group