pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: George Pavlov <gpavlov@mynewplace.com>
To: Dirk Jagdmann <jagdmann@gmail.com>
Cc: pgsql-sql@postgresql.org
Subject: Re: optimal insert
Date: Tue, 10 Oct 2006 14:04:10 -0700
Message-ID: <8C5B026B51B6854CBE88121DBF097A864DF146@ehost010-33.exch010.intermedia.net> (raw)
In-Reply-To: <bf05e51c0610101345k293ce702mc2e3e6985ef18ad7@mail.gmail.com>

And don't forget that \COPY and especially COPY are usually much faster
(and, IMHO, easier to compose/maintain) than gobs of INSERTs.


> -----Original Message-----
> From: pgsql-sql-owner@postgresql.org 
> [mailto:pgsql-sql-owner@postgresql.org] On Behalf Of Aaron Bono
> Sent: Tuesday, October 10, 2006 1:46 PM
> To: Dirk Jagdmann
> Cc: pgsql-sql@postgresql.org
> Subject: Re: [SQL] optimal insert
> 
> On 10/8/06, Dirk Jagdmann <jagdmann@gmail.com> wrote: 
> 
> 	Hello experts,
> 	
> 	I have a database that contains three tables:
> 	
> 	create table a (
> 	id serial primary key, 
> 	... -- some more fields not relevant for my question
> 	);
> 	create table b (
> 	id serial primary key,
> 	... -- some more fields not relevant for my question
> 	);
> 	create table a_b (
> 	a int not null references a, 
> 	b int not null references b
> 	);
> 	
> 	Tables a and b have already been filled with lots of 
> rows. Now my
> 	application needs to insert the relationship of a to b 
> into table a_b
> 	and is currently doing it with inserts like the following: 
> 	
> 	insert into a_b(a,b) values(1,100);
> 	insert into a_b(a,b) values(1,200);
> 	insert into a_b(a,b) values(1,54);
> 	insert into a_b(a,b) values(1,4577);
> 	
> 	So for a batch of inserts the value of a stays the 
> same, while for by 
> 	arbitrary values are inserted. Now I have wondered if 
> PostreSQL offers
> 	a smarter way to insert those values? A solution can 
> contains usage of
> 	some plpgsql code.
> 
>  
> It depends on your logic.  If you can write a query that 
> selects out the a and b records, there is a smart way:
>  
> insert into a_b(a, b)
> select 1, b.id from b where b.id in (100, 200, 54, 4577);
>  
> This is not really smart because you already have the id 
> values - the select may diminish your performance rather than 
> help.  But if the select is "smarter" then you don't even 
> need to know what the b.id <http://b.id;  values are:
>  
> insert into a_b(a, b)
> select 1, b.id from b where b.somecolumn = 'somevalue';
>  
> Hope this helps.
>  
> 
> 	
> ==================================================================
> 	   Aaron Bono
> 	   Aranya Software Technologies, Inc. 
> 	   http://www.aranya.com
> 	   http://codeelixir.com
> 	
> ================================================================== 
> 
> 



view thread (7+ messages)  latest in thread

Message-ID: <8C5B026B51B6854CBE88121DBF097A864DF146@ehost010-33.exch010.intermedia.net>
Permalink:  ../8C5B026B51B6854CBE88121DBF097A864DF146@ehost010-33.exch010.intermedia.net/
Also on:    postgresql.org/message-id/8C5B026B51B6854CBE88121DBF097A864DF146@ehost010-33.exch010.intermedia.net

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: gpavlov@mynewplace.com, jagdmann@gmail.com
  Subject: Re: optimal insert
  In-Reply-To: <8C5B026B51B6854CBE88121DBF097A864DF146@ehost010-33.exch010.intermedia.net>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox