X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id B9F363A5A81 for ; Thu, 13 Jan 2005 10:26:07 +0000 (GMT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 10623-05 for ; Thu, 13 Jan 2005 10:26:03 +0000 (GMT) Received: from rproxy.gmail.com (rproxy.gmail.com [64.233.170.200]) by svr1.postgresql.org (Postfix) with ESMTP id AA6DB3A5A31 for ; Thu, 13 Jan 2005 10:26:04 +0000 (GMT) Received: by rproxy.gmail.com with SMTP id i8so286308rne for ; Thu, 13 Jan 2005 02:26:05 -0800 (PST) DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=beta; d=gmail.com; h=received:message-id:date:from:reply-to:to:subject:mime-version:content-type:content-transfer-encoding; b=FNK84VPNis34CDzZGGygJpuz4CN+p44Yzxin/u74aoXFA+Wx45wluvKmcEKPdzF8IuRT//F1TI973rwvTNCBG+kd3UYVlwQ8Vdf12HHscXWfG9hIFJG68uJMont+T9ECsZAxf/uEmGG58nrDMRUZvUTAKKegnvsocSWd74lb8aU= Received: by 10.38.163.75 with SMTP id l75mr178117rne; Thu, 13 Jan 2005 02:26:05 -0800 (PST) Received: by 10.38.163.47 with HTTP; Thu, 13 Jan 2005 02:26:04 -0800 (PST) Message-ID: <758d5e7f05011302265be7e12a@mail.gmail.com> Date: Thu, 13 Jan 2005 11:26:04 +0100 From: Dawid Kuroczko Reply-To: Dawid Kuroczko To: pgsql-sql@postgresql.org Subject: Columns into rows. Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.54 tagged_above=0 required=5 tests=FROM_ENDS_IN_NUMS, RCVD_BY_IP X-Spam-Level: X-Archive-Number: 200501/77 X-Sequence-Number: 19865 I'm working with a quite flat table schema (think: mySQL ;)), and I want to divide it into two tables. Lets start with how it looks now: CREATE TABLE old_table ( id serial PRIMARY KEY, body text, path_a varchar(1024), gendate_a date, path_bb varchar(1024), gendate_bb date, path_ccc varchar(1024), gendate_ccc date ); I want to have: CREATE TABLE new_table ( id serial PRIMARY KEY, body text ); CREATE TABLE new_table_paths ( id integer NOT NULL REFERENCES (new_table), pathtype NOT NULL varchar(10), path varchar(1024), gendate date, PRIMARY KEY(id,pathtype) ); ...what I'm looking for is how to, most efficiently write a join between new_table and new_table_paths so it looks like old_table (for compatibility (for other people)). I'm thinking about a trigger on schema-table (with pathtypes), which would automagically update view whenever schema-table is updated (new pathtype (like 'a', 'bb', 'ccc')) is added/removed); but this is implementation. Now, for the join. I can write: SELECT * FROM (SELECT id, path AS path_a FROM new_table_paths WHERE id = <> AND pathtype = 'a') AS a NATURAL FULL OUTER JOIN (SELECT id, path AS path_bb FROM new_table_paths WHERE id = <> AND pathtype = 'bb') AS bb; [ and so on... ] And its fast; however if I move WHERE id = <> outside selects (for views), it first "materializes" old layout, and then selects id... total waste. SELECT * FROM (SELECT id, path AS path_a FROM new_table_paths WHERE pathtype = 'a') AS a NATURAL FULL OUTER JOIN (SELECT id, path AS path_bb FROM new_table_paths WHERE pathtype = 'bb') AS bb WHERE id = <>; I wonder if you know other way to write this join so it has good performace? Once again, I need a SELECT since I want a VIEW. :-) Regards, Dawid