Received: from maia.hub.org (unknown [200.46.204.183]) by mail.postgresql.org (Postfix) with ESMTP id E507D63249E for ; Sun, 31 Jan 2010 23:32:33 -0400 (AST) 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 53907-01 for ; Mon, 1 Feb 2010 03:32:22 +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 1EDEC62DBF0 for ; Sun, 31 Jan 2010 23:32:22 -0400 (AST) 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 o113WKAP028234; Sun, 31 Jan 2010 22:32:20 -0500 (EST) To: dclements89@gmail.com cc: pgsql-sql Subject: Re: Fwd: Help required on query performance In-reply-to: <1f30b80c1001311650q264dfda0uf9894cac3b9e0c3e@mail.gmail.com> References: <1f30b80c1001290049ub864091r8298fca3daa0eb69@mail.gmail.com> <1f30b80c1001311650q264dfda0uf9894cac3b9e0c3e@mail.gmail.com> Comments: In-reply-to Dave Clements message dated "Mon, 01 Feb 2010 11:50:22 +1100" Date: Sun, 31 Jan 2010 22:32:20 -0500 Message-ID: <28233.1264995140@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: 201001/63 X-Sequence-Number: 33599 Dave Clements writes: > Hello, I have this query in my system which takes around 2.5 seconds > to run. I have diagnosed that the problem is actually a hashjoin on > perm and s_ast_role tables. Is there a way I can avoid that join? BTW, just for the record, that diagnosis was completely off. The upper level of your explain results is HashAggregate (cost=38145.19..38145.20 rows=1 width=149) (actual time=2635.965..2636.086 rows=243 loops=1) -> Nested Loop (cost=15.00..38145.18 rows=1 width=149) (actual time=4.417..2635.086 rows=598 loops=1) -> Nested Loop (cost=4.13..37993.95 rows=8 width=153) (actual time=0.781..310.579 rows=975 loops=1) ... -> Bitmap Heap Scan on sq_ast_lnk_tree t (cost=10.87..18.88 rows=2 width=4) (actual time=2.382..2.382 rows=1 loops=975) ... from which we can see that the main problem is doing the sq_ast_lnk_tree scan over again 975 times, once per row coming out of the other side of the join. That accounted for 975*2.382 = 2322.450 msec, or the vast majority of the runtime. The planner wouldn't have picked this plan except that it thought that only 8 rows would come out of the other side of the join; repeating the scan 8 times seemed better than the alternatives. After you improved the statistics, it most likely switched *to* a hash join (or possibly a merge join) for this step, rather than switching away from one. regards, tom lane