Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1UDNYi-0003e0-Hj for pgsql-performance@arkaria.postgresql.org; Wed, 06 Mar 2013 23:15:41 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.72) (envelope-from ) id 1UDNYh-0006SA-6H for pgsql-performance@arkaria.postgresql.org; Wed, 06 Mar 2013 23:15:39 +0000 Received: from makus.postgresql.org ([2001:4800:7903:4::125]) by malur.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1UDNYP-0006Dc-MX for pgsql-performance@postgresql.org; Wed, 06 Mar 2013 23:15:21 +0000 Received: from outmail148150.authsmtp.net ([62.13.148.150]) by makus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1UDNYN-0004gc-Sm for pgsql-performance@postgresql.org; Wed, 06 Mar 2013 23:15:20 +0000 Received: from mail-c232.authsmtp.com (mail-c232.authsmtp.com [62.13.128.232]) by punt7.authsmtp.com (8.14.2/8.14.2/Kp) with ESMTP id r26NFIRW092744 for ; Wed, 6 Mar 2013 23:15:18 GMT Received: from [172.47.23.103] (70-36-143-10.dsl.dynamic.sonic.net [70.36.143.10]) (authenticated bits=0) by mail.authsmtp.com (8.14.2/8.14.2/) with ESMTP id r26NFB4V077108 for ; Wed, 6 Mar 2013 23:15:12 GMT Message-ID: <5137CDFC.1060902@agliodbs.com> Date: Wed, 06 Mar 2013 15:15:08 -0800 From: Josh Berkus Organization: PostgreSQL Experts Inc. User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20130221 Thunderbird/17.0.3 MIME-Version: 1.0 To: pgsql-performance@postgresql.org Subject: Re: Poor plan when joining against a union containing a join References: In-Reply-To: X-Enigmail-Version: 1.5.1 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Server-Quench: b2ddc27d-86b3-11e2-b10b-0025903375e2 X-AuthReport-Spam: If SPAM / abuse - report it at: http://www.authsmtp.com/abuse X-AuthRoute: OCdyZgscClZXSx8a IioLCC5HRQ8+YBZL BAkbIhBDJB8RWB5f NVxfJ1xYPXUEQkpF VCReGBUITgIzDi11 bRUsPEdCYEpPXBVo U0JIR1hTF0tvAQIC AAAfVhpsdgVZen1v ZQggXHYlKjZ+E0Z6 RU9UHSMFZWNmJjId BBQLdQQAbQtMLBxY a1EqVm5fYGVVe3pg RVJiZ2tqYmhScjJv T1NFHmo/e2gsJBsJ DygFGi9nBUkMQW0s LxEgYlwbHUAQNEp6 OFQoWFQDL1cbQhBY HkZLSCReIUUIRiwq CwwSVCxWEjpcR08A X-Authentic-SMTP: 61633136333939.1019:706 X-AuthFastPath: 0 (Was 255) X-AuthSMTP-Origin: 70.36.143.10/23 X-AuthVirus-Status: No virus detected - but ensure you scan with your own anti-virus system. X-Pg-Spam-Score: -2.6 (--) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-performance Precedence: bulk Sender: pgsql-performance-owner@postgresql.org On 03/06/2013 06:54 AM, David Leverton wrote: > Hi all, > > I'm encountering very poor query plans when joining against a union, > where one branch of the union is itself a join: specifically, Postgres > does the entire inside join and then filters the result, rather than > pushing the filters down to the joined tables. I've provided a > standalone test case below, including some variations that don't show > the problem for comparison. The fourth query is the problematic one - > I would have expected the Append portion of the plan to be essentially > the same as the one in the second query. Thanks for the test case! Actually, in case #4, Postgres *is* pushing down the join qual into the segments of the Union. It's just that that's not helping performance any; it's causing a really slow join on bundle, which is actually where you're spending most of your time: -> Hash Join (cost=27.50..12970.50 rows=1000000 width=8) (actual time=0.617..344.127 rows=1000000 loops=1) Hash Cond: (bundle.bundle_type = bundle_contents.bundle_type) Buffers: shared hit=448 -> Seq Scan on bundle (cost=0.00..1443.00 rows=100000 width=8) (actual time=0.009..22.066 rows=100000 loops=1) Buffers: shared hit=443 -> Hash (cost=15.00..15.00 rows=1000 width=8) (actual time=0.594..0.594 rows=1000 loops=1) Buckets: 1024 Batches: 1 Memory Usage: 40kB Buffers: shared hit=5 -> Seq Scan on bundle_contents (cost=0.00..15.00 rows=1000 width=8) (actual time=0.008..0.207 rows=1000 loops=1) Buffers: shared hit=5 Clearly this is the wrong strategy; Postgres should be letting the filter on item_reference be the driver instead of hashing the whole bundle + bundle_contents join. I suspect that the qual pushdown into the union is hitting an inability to transverse multiple joins, which wouldn't surprise me; in fact, I'd be surprised if it could do so. On a pragmatic basis, joining against complex UNION expressions is liable to be a bit of a minefield for the next few generations of the Postgres planner; it's just really hard to optimize. You might think of using outer joins instead of a UNION. -- Josh Berkus PostgreSQL Experts Inc. http://pgexperts.com -- Sent via pgsql-performance mailing list (pgsql-performance@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-performance