Received: from localhost (unknown [200.46.204.183]) by mail.postgresql.org (Postfix) with ESMTP id 60AE465027B for ; Mon, 10 Nov 2008 15:31:47 -0400 (AST) Received: from mail.postgresql.org ([200.46.204.86]) by localhost (mx1.hub.org [200.46.204.183]) (amavisd-maia, port 10024) with ESMTP id 60996-03 for ; Mon, 10 Nov 2008 15:31:47 -0400 (AST) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from sss.pgh.pa.us (sss.pgh.pa.us [66.207.139.130]) by mail.postgresql.org (Postfix) with ESMTP id BAF2A650276 for ; Mon, 10 Nov 2008 15:31:46 -0400 (AST) Received: from sss2.sss.pgh.pa.us (tgl@localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.2/8.14.2) with ESMTP id mAAJVdsP012777; Mon, 10 Nov 2008 14:31:39 -0500 (EST) To: "Jim 'Decibel!' Nasby" cc: Richard Huxton , postgresql performance list Subject: Re: Oddity with view (now with test case) In-reply-to: <1E5A4D7D-3B95-440B-A377-B6B9F8768F7A@cashnetusa.com> References: <3CF0CFC1-FDBA-4A61-ACAA-6AF556C4C6F4@cashnetusa.com> <3128.1226322412@sss.pgh.pa.us> <7B598355-07C0-450A-9914-B5979F48A5B3@cashnetusa.com> <49187BA7.8020403@archonet.com> <1E5A4D7D-3B95-440B-A377-B6B9F8768F7A@cashnetusa.com> Comments: In-reply-to "Jim 'Decibel!' Nasby" message dated "Mon, 10 Nov 2008 12:36:12 -0600" Date: Mon, 10 Nov 2008 14:31:39 -0500 Message-ID: <12776.1226345499@sss.pgh.pa.us> From: Tom Lane X-Archive-Number: 200811/70 X-Sequence-Number: 31801 "Jim 'Decibel!' Nasby" writes: > Here's the commands to generate the test case: > create table a(a int, b text default 'test text'); > create table c(c_id serial primary key, c_text text); > insert into c(c_text) values('a'),('b'),('c'); > create table b(a int, c_id int references c(c_id)); > create view v as select a, b, null as c_id, null as c_text from a > union all select a, null, b.c_id, c_text from b join c on (b.c_id= > c.c_id); > \timing > insert into a(a) select generate_series(1,9999999); > select count(*) from a; > select count(*) from v; > explain analyze select count(*) from a; > explain analyze select count(*) from v; I think what you're looking at is projection overhead and per-plan-node overhead (the EXPLAIN ANALYZE in itself contributes quite a lot of the latter). One thing you could do is be more careful about making the union input types match up so that no subquery scan nodes are required: create view v2 as select a, b, null::int as c_id, null::text as c_text from a union all select a, null::text, b.c_id, c_text from b join c on (b.c_id=c.c_id); On my machine this runs about twice as fast as the original view. regards, tom lane