Received: from maia.hub.org (maia-3.hub.org [200.46.204.243]) by mail.postgresql.org (Postfix) with ESMTP id B7BFCB5D80B for ; Tue, 18 Oct 2011 03:18:24 -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 76729-06 for ; Tue, 18 Oct 2011 06:18:18 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.8.0-rc2 Received: from mail.catalyst.net.nz (bertrand.catalyst.net.nz [202.78.240.40]) by mail.postgresql.org (Postfix) with ESMTP id 3B6C4B5D803 for ; Tue, 18 Oct 2011 03:18:18 -0300 (ADT) Received: from localhost (localhost [127.0.0.1]) by mail.catalyst.net.nz (Postfix) with ESMTP id 6574A32493; Tue, 18 Oct 2011 19:18:15 +1300 (NZDT) X-Virus-Scanned: Debian amavisd-new at catalyst.net.nz Received: from mail.catalyst.net.nz ([127.0.0.1]) by localhost (bertrand.catalyst.net.nz [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id Ya6c0IKhBJbj; Tue, 18 Oct 2011 19:18:14 +1300 (NZDT) Received: from [192.168.1.64] (122-60-64-230.jetstream.xtra.co.nz [122.60.64.230]) (Authenticated sender: mark.kirkwood) by mail.catalyst.net.nz (Postfix) with ESMTPSA id E525F32491; Tue, 18 Oct 2011 19:18:13 +1300 (NZDT) Message-ID: <4E9D1A25.5050607@catalyst.net.nz> Date: Tue, 18 Oct 2011 19:18:13 +1300 From: Mark Kirkwood User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:7.0.1) Gecko/20110929 Thunderbird/7.0.1 MIME-Version: 1.0 To: Robins Tharakan CC: pgsql-performance@postgresql.org Subject: Re: Bad plan by Planner (Already resolved?) References: <4E9BCB1F.9060208@comodo.com> In-Reply-To: <4E9BCB1F.9060208@comodo.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-2.404 tagged_above=-10 required=5 tests=BAYES_00=-1.9, RP_MATCHES_RCVD=-0.504 X-Spam-Level: X-Archive-Number: 201110/152 X-Sequence-Number: 45374 On 17/10/11 19:28, Robins Tharakan wrote: > Hi, > > I stumbled upon a situation where the planner comes with a bad query > plan, but I wanted to mention upfront that I'm using a dated PG > version and I already see an update which mentions about improving > planner performance. I just wanted to check if this issue is already > resolved, and if so, which version should I be eyeing. > > My PG Version: 8.4.7 > Probably solved in: 8.4.8 / 9.0.4 ? > > Issue: It seems that the planner is unable to flatten the IN sub-query > causing the planner to take a bad plan and take ages (>2500 seconds) > and expects to give a 100 million row output, where in-fact it should > get a six row output. The same IN query, when flattened, PG gives the > correct result in a fraction of a second. > > Do let me know if this is a new case. I could try to give you the > EXPLAIN ANALYSE outputs / approximate table sizes if required. > > EXISTING QUERY: > SELECT field_b FROM large_table_a > JOIN large_table_b USING (field_b) > WHERE field_a IN (SELECT large_table_b.field_a > FROM large_table_b WHERE field_b = 2673056) > > RECOMMENDED QUERY: > SELECT s1.field_b FROM large_table_a > JOIN large_table_b s1 USING (field_b) > JOIN large_table_b s2 ON s1.field_a = s2.field_a > WHERE s2.field_b = 2673056 > > Poor plans being generated for the subquery variant above were specifically targeted in 8.4.9. It may be that you don't need the workaround in that (or corresponding later) versions - 9.0.5, 9.1.0. Regards Mark