Received: from localhost (unknown [200.46.204.191]) by postgresql.org (Postfix) with ESMTP id 4DA772E34EE for ; Mon, 14 Jan 2008 23:30:34 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.191]) (amavisd-maia, port 10024) with ESMTP id 67628-02 for ; Mon, 14 Jan 2008 23:30:30 -0400 (AST) Received: from fg-out-1718.google.com (fg-out-1718.google.com [72.14.220.156]) by postgresql.org (Postfix) with ESMTP id B43A32E325A for ; Mon, 14 Jan 2008 23:30:29 -0400 (AST) Received: by fg-out-1718.google.com with SMTP id 22so2384474fge.23 for ; Mon, 14 Jan 2008 19:30:28 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:received:received:message-id:date:from:reply-to:to:subject:cc:in-reply-to:mime-version:content-type:references; bh=KW7gZLxFE8bTzJFK1kGQyt9HsheT2n3UVDClUaxx2W4=; b=KHZxO60j4LUbYtAK9/qhRbvnSdgeknISFQBrdNAnchr/aYN4kQ+EJhRmptTkCQYUx5zQUqeOvUhqtiTcTU/QCJZLH0yUFoMurNF3VfpGYak3RzSJdpVXGu557huDpwf9bXY9vvHSFNWwM4NbBupehYDYroEID2kfIY8SlWGi8oc= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=message-id:date:from:reply-to:to:subject:cc:in-reply-to:mime-version:content-type:references; b=Ge3HW1E+EMH+tD9i/IT1JslIpyMwmVW8HJRHBLtuJ9iKgTlJ9D2VlXbhxQtz3t6y8/Xlu76YKXAaLC1nYkeF8hw/rKubvQ42FQZ5yOKqmkOXQzc6t2Qij5F8YlMIgzWxKgIz/TKxH+4sTMK55FzGXu/EKYI5/10+K0l/jIXYKBY= Received: by 10.82.187.2 with SMTP id k2mr12295330buf.19.1200367827956; Mon, 14 Jan 2008 19:30:27 -0800 (PST) Received: by 10.82.113.9 with HTTP; Mon, 14 Jan 2008 19:30:27 -0800 (PST) Message-ID: <36af4bed0801141930r4bcaf94cqed26557899fc578b@mail.gmail.com> Date: Tue, 15 Jan 2008 09:00:27 +0530 From: "Robins Tharakan" Reply-To: robins@pobox.com To: Patric Subject: Re: Saving result set of SELECT to table column Cc: pgsql-performance@postgresql.org In-Reply-To: <478BA7B8.2090907@p-dw.com> MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="----=_Part_11317_21685002.1200367827951" References: <478BA7B8.2090907@p-dw.com> X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200801/122 X-Sequence-Number: 28361 ------=_Part_11317_21685002.1200367827951 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-Disposition: inline Hi Patric, This doesn't seem to be a question pertaining to the PERFORM queue. If I understand you correctly, this should solve your problems, without the need for any RULES / TRIGGERS. CREATE TABLE y ( y1 int4 NOT NULL, y2 varchar, CONSTRAINT a PRIMARY KEY (y1) ) CREATE TABLE z ( z1 int4 NOT NULL, z2 varchar, CONSTRAINT zz PRIMARY KEY (z1) ) CREATE TABLE x ( x1 int4 NOT NULL, xy1 int4 NOT NULL, xz1 int4 NOT NULL, xy2 varchar, xz2 varchar, CONSTRAINT xa PRIMARY KEY (x1), CONSTRAINT xy1 FOREIGN KEY (xy1) REFERENCES y (y1) MATCH SIMPLE ON UPDATE NO ACTION ON DELETE NO ACTION, CONSTRAINT xz1 FOREIGN KEY (xz1) REFERENCES z (z1) MATCH SIMPLE ON UPDATE NO ACTION ON DELETE NO ACTION ) INSERT INTO x (x1, xy1, xz1, xy2, xz2) SELECT 1, y1, z1, y2, z2 FROM y, z WHERE y1 = 1 AND z1 = 1; *Robins* On Jan 14, 2008 11:49 PM, Patric wrote: > Hi, > There will be some flames i suppose. > Well I've a normalized database.. > For instance: > > create table Y ( pk, data... ); > create table Z ( pk , data... ); > > create table X ( char, references Y, references Z); > > SELECT * from X; > > Now I want to make a listing of the result set from X. > If there are references to Z or Y (not null refs), I want to display > that data too. > > Normally I would SELECT, to get that data, not in my case. > Nearly all queries will be SELECTs, no UPDATEs or INSERTs, so need to > optimize that case. > > The dirty little denormalization would look like this: > > create table X ( char, ref. to Y, ref. to Z, StoreY Y , StoreZ Z); > > On insert or update of Z or Y, I would update these two (StoreY, > StoreZ) columns by RULE or TRIGGER.. > I know this is not nice etc.. Codd would sue for this, but in my case > performance over beauty is ok. > I'm looking for something like UPDATE X set StoreY=(SELECT * FROM Y > WHERE pk=4) WHERE foreignID2Y = 4; > > Is there a away to accomplish this straightforward in a single > statement without doing loops and stuff in a serverside procedure? > > Thanks in advance, > Patric > > > > > ---------------------------(end of broadcast)--------------------------- > TIP 2: Don't 'kill -9' the postmaster > ------=_Part_11317_21685002.1200367827951 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-Disposition: inline Hi Patric,

This doesn't seem to be a question pertaining to the PERFORM queue.

If I understand you correctly, this should solve your problems, without the need for any RULES / TRIGGERS.

CREATE TABLE y
(
  y1 int4 NOT NULL,
  y2 varchar,
  CONSTRAINT a PRIMARY KEY (y1)
)


CREATE TABLE z
(
  z1 int4 NOT NULL,
  z2 varchar,
  CONSTRAINT zz PRIMARY KEY (z1)
)



CREATE TABLE x
(
  x1 int4 NOT NULL,
  xy1 int4 NOT NULL,
  xz1 int4 NOT NULL,
  xy2 varchar,
  xz2 varchar,
  CONSTRAINT xa PRIMARY KEY (x1),
  CONSTRAINT xy1 FOREIGN KEY (xy1)
      REFERENCES y (y1) MATCH SIMPLE
      ON UPDATE NO ACTION ON DELETE NO ACTION,
  CONSTRAINT xz1 FOREIGN KEY (xz1)
      REFERENCES z (z1) MATCH SIMPLE
      ON UPDATE NO ACTION ON DELETE NO ACTION
)


INSERT INTO x (x1, xy1, xz1, xy2, xz2)
SELECT 1, y1, z1, y2, z2
FROM y, z
WHERE y1 = 1
    AND z1 = 1;
   

Robins

On Jan 14, 2008 11:49 PM, Patric <lists@p-dw.com> wrote:
Hi,
  There will be some flames i suppose.
  Well I've a normalized database..
  For instance:

  create table Y ( pk, data...  );
  create table Z ( pk , data...    );

  create table X (  char, references  Y, references  Z);

  SELECT * from X;

  Now I want to make a listing of the result set from X.
  If there are references to Z or Y (not null refs), I want to display
that data too.

  Normally I would SELECT, to get that data, not in my case.
  Nearly all queries will be SELECTs, no UPDATEs or INSERTs, so need to
optimize that case.

  The dirty little denormalization would look like this:

  create table X ( char, ref. to Y, ref. to Z, StoreY Y , StoreZ Z);

  On insert or update of Z or Y, I would update these two (StoreY,
StoreZ) columns by RULE or TRIGGER..
  I know this is not nice etc.. Codd would sue for this, but in my case
performance over beauty is ok.
    I'm looking for something like UPDATE X set StoreY=(SELECT * FROM Y
WHERE pk=4) WHERE foreignID2Y = 4;

  Is there a away to accomplish this straightforward  in a single
statement without doing loops and stuff in a serverside procedure?

 Thanks in advance,
  Patric




---------------------------(end of broadcast)---------------------------
TIP 2: Don't 'kill -9' the postmaster

------=_Part_11317_21685002.1200367827951--