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

Re: Select-Insert-Query

From: Richard Huxton <dev(at)archonet(dot)com>
To: postgres(at)countup(dot)de, pgsql-performance(at)postgresql(dot)org
Subject: Re: Select-Insert-Query
Date: 2004-02-27 19:47:13
Message-ID: 200402271947.13736.dev@archonet.com (view raw or flat)
Thread:
Lists: pgsql-performance
On Friday 27 February 2004 16:52, postgres(at)countup(dot)de wrote:

*please* don't post HTML-only messages.

<br><br>what is the most performant way to select for
> example the first 99 rows of a table and insert them into another
> table...<br><br>at the moment i do this:<br><br>

> for userrecord in select *
> from table where account_id = a_account_id and counter_id =
> userrecord.counter_id and visitortable_id between a_minid and a_maxid limit
> 99 loop
>	insert into lastusers (account_id, counter_id, date, ip,
> hostname)
> values(a_account_id,userrecord.counter_id,userrecord.date,userrecord.ip,
> userrecord.hostname);
>end loop;

If that is the actual query, I'm puzzled as to what you're doing, since you 
don't know what it is you just inserted. Anyway, you can do this as a single 
query

INSERT INTO lastusers (account_id ... hostname)
SELECT a_account_id, counter_id...
FROM table where...

The LIMIT shouldn't take any time in itself, although if you are sorting then 
PG may need to sort all the rows before discarding all except the first 99.

If this new query is no better, make sure you have vacuum analyse'd the tables 
and post the output of EXPLAIN ANALYSE for the query.

-- 
  Richard Huxton
  Archonet Ltd

In response to

pgsql-performance by date

Next:From: Shridhar DaithankarDate: 2004-02-28 08:38:41
Subject: Re: FreeBSD config
Previous:From: Vivek KheraDate: 2004-02-27 18:33:33
Subject: Re: FreeBSD config

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