Received: from maia.hub.org (maia-2.hub.org [200.46.204.251]) by mail.postgresql.org (Postfix) with ESMTP id D13136326B5 for ; Thu, 19 Aug 2010 13:28:37 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.251]) (amavisd-maia, port 10024) with ESMTP id 88991-06 for ; Thu, 19 Aug 2010 16:28:26 +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 3655F6322B0 for ; Thu, 19 Aug 2010 13:28:26 -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 o7JGSNTI007714; Thu, 19 Aug 2010 12:28:23 -0400 (EDT) To: Samuel Gendler cc: pgsql-performance@postgresql.org Subject: Re: yet another q In-reply-to: References: Comments: In-reply-to Samuel Gendler message dated "Wed, 18 Aug 2010 23:14:10 -0700" Date: Thu, 19 Aug 2010 12:28:23 -0400 Message-ID: <7713.1282235303@sss.pgh.pa.us> From: Tom Lane X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.91 tagged_above=-10 required=5 tests=BAYES_00=-1.9, T_RP_MATCHES_RCVD=-0.01 X-Spam-Level: X-Archive-Number: 201008/233 X-Sequence-Number: 40076 Samuel Gendler writes: > fast plan: http://explain.depesz.com/s/iZ > slow plan: http://explain.depesz.com/s/Dv2 Your problem here is that it's switching from hash aggregation to sort-and-group-aggregate once it decides that the number of aggregate groups won't fit in work_mem anymore. While you could brute-force that by raising work_mem, it'd be a lot better if you could get the estimated number of groups more in line with the actual. Notice the very large discrepancy between the estimated and actual numbers of rows out of the aggregation steps. Increasing the stats targets for the GROUP BY columns might help, but I think what's basically going on here is there's correlation between the GROUP BY columns that the planner doesn't know about. One thing I'm wondering is why you're grouping by owner_customer_id and t_fact.provider_id, when these aren't used in the output. regards, tom lane