Received: from maia.hub.org (maia-3.hub.org [200.46.204.243]) by mail.postgresql.org (Postfix) with ESMTP id AE8D0B607BF for ; Wed, 1 Jun 2011 17:35:26 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.243]) (amavisd-maia, port 10024) with ESMTP id 13035-04 for ; Wed, 1 Jun 2011 20:35:19 +0000 (UTC) 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 872D9B6071B for ; Wed, 1 Jun 2011 17:35:19 -0300 (ADT) 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 p51KZGot024462; Wed, 1 Jun 2011 16:35:16 -0400 (EDT) To: Robert Haas cc: =?ISO-8859-1?Q?C=E9dric_Villemain?= , panam , PostgreSQL-development Subject: Re: [PERFORM] Hash Anti Join performance degradation In-reply-to: References: <1306210464806-4420974.post@n5.nabble.com> <4DDB47BC.8030504@postnewspapers.com.au> <1306247697267-4422247.post@n5.nabble.com> <4DDC6109.7060708@postnewspapers.com.au> <1306341749094-4425890.post@n5.nabble.com> <4DDDBD96.4010809@postnewspapers.com.au> <1306413217406-4428435.post@n5.nabble.com> <7913.1306900062@sss.pgh.pa.us> <24247.1306959956@sss.pgh.pa.us> Comments: In-reply-to Robert Haas message dated "Wed, 01 Jun 2011 16:31:34 -0400" Date: Wed, 01 Jun 2011 16:35:16 -0400 Message-ID: <24461.1306960516@sss.pgh.pa.us> From: Tom Lane X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.91 tagged_above=-5 required=5 tests=BAYES_00=-1.9, T_RP_MATCHES_RCVD=-0.01 X-Spam-Level: X-Archive-Number: 201106/55 X-Sequence-Number: 188768 Robert Haas writes: > On Wed, Jun 1, 2011 at 4:25 PM, Tom Lane wrote: >> Because of the way that a bitmap heap scan works, the rows are >> guaranteed to be loaded into the hash table in physical order, which >> means (in the fast case) that the row with the largest "id" value gets >> loaded last.  And because ExecHashTableInsert pushes each row onto the >> front of its hash chain, that row ends up at the front of the hash >> chain.  Net result: for all the outer rows that aren't the one with >> maximum id, we get a joinqual match at the very first entry in the hash >> chain.  Since it's an antijoin, we then reject that outer row and go >> on to the next.  The join thus ends up costing us only O(N) time. > Ah! Make sense. If I'm reading your explanation right, this means > that we could have hit a similar pathological case on a nestloop as > well, just with a data ordering that is the reverse of what we have > here? Yeah. It's just chance that this particular data set, with this particular ordering, happens to work well with a nestloop version of the query. On average I'd expect nestloop to suck even more, because of more per-inner-tuple overhead. regards, tom lane