Received: from localhost (mx1.hub.org [200.46.208.251]) by postgresql.org (Postfix) with ESMTP id AD10A9FB24C for ; Wed, 27 Sep 2006 12:51:59 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.208.251]) (amavisd-new, port 10024) with ESMTP id 10006-01 for ; Wed, 27 Sep 2006 12:51:49 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey- Received: from globalrelay.com (mail1.globalrelay.com [216.18.71.77]) by postgresql.org (Postfix) with ESMTP id B86859FB20C for ; Wed, 27 Sep 2006 12:51:48 -0300 (ADT) X-Virus-Scanned: Scanned by GRC-AntiVirus Gateway X-GR-Acctd: YES Received: from [67.90.96.2] (HELO DaveEMachine) by globalrelay.com (CommuniGate Pro SMTP 4.2.3) with ESMTP id 106734405; Wed, 27 Sep 2006 08:51:40 -0700 From: "Dave Dutcher" To: "'Tim Truman'" , Subject: Re: Forcing the use of particular execution plans Date: Wed, 27 Sep 2006 10:51:26 -0500 Message-ID: <007b01c6e24c$ccfc8190$8300a8c0@tridecap.com> MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: quoted-printable X-Priority: 3 (Normal) X-MSMail-Priority: Normal X-Mailer: Microsoft Outlook, Build 10.0.6626 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2962 In-Reply-To: <000501c6e1ff$c80022c0$0340050a@sdg> Importance: Normal X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0.101 tagged_above=0 required=5 tests=AWL X-Spam-Level: X-Archive-Number: 200609/357 X-Sequence-Number: 21043 > -----Original Message----- > From: pgsql-performance-owner@postgresql.org [mailto:pgsql-performance-owner@postgresql.org] On Behalf Of Tim Truman >=20 > Hi, >=20 > I have the following query which has been running very slowly=20 > and after a > lot of testing/trial and error I found an execution plan that=20 > ran the query > in a fraction of the time (and then lost the statistics that=20 > produced it). > What I wish to know is how to force the query to use the=20 > faster execution > plan. It would be a bit easier to diagnose the problem if you posted EXPLAIN ANALYZE rather than just EXPLAIN. The two plans you posted looked very similar except for the order of the nested loop in subquery 1 and an = index scan rather than a seq scan in subquery 2. =20 My guess would be that the order of the nested loop is determined mostly = by estimates of matching rows. If you ran an EXPLAIN ANALYZE you could = tell if the planner is estimating correctly. If it is not, you could try = increasing your statistics target and running ANALYZE. =20 To make the planner prefer an index scan over a seq scan, I would first check the statistics again, and then you can try setting enable_seqscan = to false (enable_seqscan is meant more for testing than production) or, you could try reducing random_page_cost, but you should test that against a range of queries before putting it in production. Dave