Received: from localhost (wm.hub.org [200.46.204.128]) by postgresql.org (Postfix) with ESMTP id 21D0A9FB252 for ; Mon, 9 Oct 2006 02:28:15 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.128]) (amavisd-new, port 10024) with ESMTP id 03923-08 for ; Mon, 9 Oct 2006 05:28:06 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey- Received: from svr4.postgresql.org (svr4.postgresql.org [66.98.251.159]) by postgresql.org (Postfix) with ESMTP id 2C5CF9FA2E9 for ; Mon, 9 Oct 2006 02:28:06 -0300 (ADT) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.3 Received: from pow.actarg.com (host25.actarg.com [209.180.91.25]) by svr4.postgresql.org (Postfix) with ESMTP id 606115AF994 for ; Mon, 9 Oct 2006 05:28:05 +0000 (GMT) Received: from [192.168.5.50] (bateman.cc [65.73.113.56]) (authenticated bits=0) by pow.actarg.com (8.13.1/8.13.1) with ESMTP id k995Rv0k014226 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO); Sun, 8 Oct 2006 23:28:01 -0600 Message-ID: <4529DDCC.6010909@actarg.com> Date: Sun, 08 Oct 2006 23:27:40 -0600 From: Kyle Bateman User-Agent: Mozilla Thunderbird 1.0.8-1.1.fc4 (X11/20060501) X-Accept-Language: en-us, en MIME-Version: 1.0 To: Tom Lane Cc: pgsql-sql@postgresql.org Subject: Re: PG 8.2beta reordering working for this case? References: <44DF6582.4000806@actarg.com> <82mza77rch.fsf@mid.bfk.de> <44E11E1A.8020507@actarg.com> <27367.1155606817@sss.pgh.pa.us> <44E349BA.4070205@actarg.com> <26676.1155747492@sss.pgh.pa.us> <44E3840E.4080502@actarg.com> <45284DBC.3060901@actarg.com> <25779.1160329771@sss.pgh.pa.us> In-Reply-To: <25779.1160329771@sss.pgh.pa.us> Content-Type: multipart/mixed; boundary="------------090102040909050606070803" X-Virus-Scanned: ClamAV 0.88/1656/Sun Aug 13 21:24:35 2006 on pow.actarg.com X-Virus-Status: Clean X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0 tagged_above=0 required=5 tests= X-Spam-Level: X-Archive-Number: 200610/91 X-Sequence-Number: 26480 This is a multi-part message in MIME format. --------------090102040909050606070803 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Tom Lane wrote: >Kyle Bateman writes: > > >>Is there a way to make the optimizer do this? >> >> > >Sorry, that's not happening for 8.2. Consider using a union all (not >union) across the subledg_N tables directly and then joining to that. >That boils down to being a partitioning case and I think probably will >be covered by the 8.2 improvements. > Yup, union all is much more efficient. It hadn't really occurred to me the difference between union and union all. But it makes sense to eliminate the need for a unique sort. The q3 query went from 10 seconds to 1 second with just the addition of union all in the general ledger. BTW, explain analyze still says 10 seconds of run time (and takes 10 seconds to run), but when I remove the explain analyze, the query runs in about a second. What's that all about? Also, I came up with the view shown in the attachment. It is still much faster than joining to the union-all ledger (40 ms). I'm not sure why because I'm not sure if explain analyze is telling me the real story (I see a sequential scan of the ledgers in there when it runs 10 seconds). I'm not sure what it's doing when it runs in 1 second. Kyle --------------090102040909050606070803 Content-Type: text/x-sql; name="workaround.sql" Content-Transfer-Encoding: 7bit Content-Disposition: inline; filename="workaround.sql" -- This view is a possible workaround for the problem drop view gen_ledg_pr; --explain analyze create view gen_ledg_pr as select lg.*, pr.anst_id from subview_A lg join proj_rel pr on pr.prog_id = lg.proj union all select lg.*, pr.anst_id from subview_B lg join proj_rel pr on pr.prog_id = lg.proj union all select lg.*, pr.anst_id from subview_C lg join proj_rel pr on pr.prog_id = lg.proj ; --------------090102040909050606070803--