Received: from localhost (mx1.hub.org [200.46.208.251]) by postgresql.org (Postfix) with ESMTP id 924239FA217 for ; Tue, 10 Oct 2006 17:45:41 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.208.251]) (amavisd-new, port 10024) with ESMTP id 80949-07 for ; Tue, 10 Oct 2006 17:45:38 -0300 (ADT) X-Greylist: domain auto-whitelisted by SQLgrey- Received: from py-out-1112.google.com (py-out-1112.google.com [64.233.166.178]) by postgresql.org (Postfix) with ESMTP id BC26B9FB319 for ; Tue, 10 Oct 2006 17:45:35 -0300 (ADT) Received: by py-out-1112.google.com with SMTP id d80so2869696pyd for ; Tue, 10 Oct 2006 13:45:34 -0700 (PDT) DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=beta; d=gmail.com; h=received:message-id:date:from:sender:to:subject:cc:in-reply-to:mime-version:content-type:references:x-google-sender-auth; b=qML2jUnFDMiCDiwzbS3HnvHTFoQDoX9Kxd6j8bBOqc1Xme5AESWPtxzmmCwaUlTXjYLNQ7MBIv+w9n4nVoF7/c9+4DCtnxXvPigNUjCQ3dCEw3yhjKGpNdHgQBADWvxL9wbgF/QuR4h69xcPbFtAWWrx5JTjVRKQ0Ye1JC9sGeI= Received: by 10.35.96.7 with SMTP id y7mr16059844pyl; Tue, 10 Oct 2006 13:45:30 -0700 (PDT) Received: by 10.35.78.6 with HTTP; Tue, 10 Oct 2006 13:45:30 -0700 (PDT) Message-ID: Date: Tue, 10 Oct 2006 15:45:30 -0500 From: "Aaron Bono" To: "Dirk Jagdmann" Subject: Re: optimal insert Cc: pgsql-sql@postgresql.org In-Reply-To: <5d0f60990610081404s79a90cdbha4603c70c75ce233@mail.gmail.com> MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="----=_Part_59853_15029784.1160513130698" References: <5d0f60990610081404s79a90cdbha4603c70c75ce233@mail.gmail.com> X-Google-Sender-Auth: e5d3f2e2a5f87c22 X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0.599 tagged_above=0 required=5 tests=AWL, DNS_FROM_RFC_ABUSE, HTML_30_40, HTML_MESSAGE, SPF_PASS X-Spam-Level: X-Archive-Number: 200610/102 X-Sequence-Number: 26491 ------=_Part_59853_15029784.1160513130698 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Content-Disposition: inline On 10/8/06, Dirk Jagdmann 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 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 > ================================================================== ------=_Part_59853_15029784.1160513130698 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-Disposition: inline
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 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
==================================================================
------=_Part_59853_15029784.1160513130698--