Select-Insert-Query

Поиск
Список
Период
Сортировка
Искать

Select-Insert-Query

От:
postgres@countup.de
Дата:
Hi, 

what is the most performant way to select for example the first 99 rows of a table and insert them into another table...

at the moment i do this:

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;

i think "limit" is a performance killer, is that right? but what to do instead

thanks
bye

Re: Select-Insert-Query

От:
Richard Huxton <dev@archonet.com>
Дата:
On Friday 27 February 2004 16:52, postgres@countup.de wrote:

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



what is the most performant way to select for > example the first 99 rows of a table and insert them into another > table...

at the moment i do this:

> 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

Re: Select-Insert-Query

От:
postgres@countup.de
Дата:
Hi, 

nobody has an idea? :-(

-----Ursprüngliche Nachricht-----
Von: pgsql-performance-owner@postgresql.org [mailto:pgsql-performance-owner@postgresql.org] Im Auftrag von postgres@countup.de
Gesendet: Freitag, 27. Februar 2004 17:53
An: pgsql-performance@postgresql.org
Betreff: [PERFORM] Select-Insert-Query

Hi,

what is the most performant way to select for example the first 99 rows of a table and insert them into another table...

at the moment i do this:

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;

i think "limit" is a performance killer, is that right? but what to do instead

thanks
bye

Re: Select-Insert-Query

От:
Oliver Elphick <olly@lfix.co.uk>
Дата:
On Tue, 2004-03-02 at 00:49, postgres@countup.de wrote:

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

Using LIMIT without ORDER BY will give a selection that is dependent on
the physical location of rows in the table; this will change whenever
one of them is UPDATEd.

> 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; 
> 
> i think "limit" is a performance killer, is that right? but what to do
> instead 

I'm sure it is the loop that is the killer.  Use a query in the INSERT
statement:

INSERT INTO lastusers (account_id, counter_id, date, ip, hostname)
  SELECT * FROM table
          WHERE account_id = a_account_id AND
                counter_id = userrecord.counter_id AND
                visitortable_id between a_minid and a_maxid
          ORDER BY date DESC
          LIMIT 99;

-- 
Oliver Elphick 
LFIX Ltd

FAQ