Received: from maia.hub.org (unknown [200.46.204.183]) by mail.postgresql.org (Postfix) with ESMTP id 2D173636158 for ; Tue, 18 Aug 2009 23:02:31 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.183]) (amavisd-maia, port 10024) with ESMTP id 88940-03-4 for ; Wed, 19 Aug 2009 02:02:25 +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 DC379635675 for ; Tue, 18 Aug 2009 23:02:21 -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 n7J22Itl009801; Tue, 18 Aug 2009 22:02:19 -0400 (EDT) To: Karl Denninger cc: Kevin Grittner , pgsql-performance@postgresql.org, pgsql-sql@postgresql.org Subject: Re: [SQL] SQL Query Performance - what gives? In-reply-to: <4A8B334F.1010801@denninger.net> References: <4A8B04C7.6060702@denninger.net> <4A8AC75C0200002500029C8A@gw.wicourts.gov> <4A8B1606.2040209@denninger.net> <4A8ADDEA0200002500029CA3@gw.wicourts.gov> <4A8B334F.1010801@denninger.net> Comments: In-reply-to Karl Denninger message dated "Tue, 18 Aug 2009 18:03:44 -0500" Date: Tue, 18 Aug 2009 22:02:18 -0400 Message-ID: <9800.1250647338@sss.pgh.pa.us> From: Tom Lane X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-2.599 tagged_above=-10 required=5 tests=BAYES_00=-2.599 X-Spam-Level: X-Archive-Number: 200908/202 X-Sequence-Number: 35234 Karl Denninger writes: > The problem appearsa to lie in the "nested loop", and I don't understand > why that's happening. It looks to me like there are several issues here. One is the drastic underestimate of the number of rows satisfying the permission condition. That leads the planner to think that a nestloop join with the other table will be fast, which is only right if there are just one or a few rows coming out of "forum". With sixty-some rows you get sixty-some repetitions of the scan of the other table, which loses. Problem number two is the overeager use of a BitmapAnd to add on another index that isn't really very selective. That might be a correct decision but it looks fishy here. We rewrote choose_bitmap_and a couple of times to try to fix that problem ... what PG version is this exactly? The third thing that looks fishy is that it's using unqualified index scans for no apparent reason. Have you got enable_seqscan turned off, and if so what happens when you fix that? What other nondefault planner settings are you using? But anyway, the big problem seems to be poor selectivity estimates for conditions like "(permission & 127) = permission". I have bad news for you: there is simply no way in the world that Postgres is not going to suck at estimating that, because the planner has no knowledge whatsoever of the behavior of "&". You could consider writing and submitting a patch that would teach it something about that, but in the near term it would be a lot easier to reconsider your representation of permissions. You'd be likely to get significantly better results, not to mention have more-readable queries, if you stored them as a group of simple boolean columns. regards, tom lane