Received: from localhost (unknown [200.46.204.182]) by postgresql.org (Postfix) with ESMTP id 5FA6A2E0EC0 for ; Tue, 27 Nov 2007 19:47:36 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.182]) (amavisd-maia, port 10024) with ESMTP id 32034-07 for ; Tue, 27 Nov 2007 19:47:22 -0400 (AST) Received: from globalrelay.com (mail1.globalrelay.com [204.92.49.200]) by postgresql.org (Postfix) with ESMTP id 86FFD2E3389 for ; Tue, 27 Nov 2007 19:47:32 -0400 (AST) X-Virus-Scanned: Scanned by GRC-AntiVirus Gateway Received: from [63.231.179.94] (HELO davedell) by globalrelay.com (CommuniGate Pro SMTP 4.2.3) with ESMTP id 165806113; Tue, 27 Nov 2007 23:47:30 +0000 From: "Dave Dutcher" To: "'cluster'" , References: <20071127165543.GY11978@crankycanuck.ca> <23999.1196184368@sss.pgh.pa.us> <056501c83128$55d58a90$8e00a8c0@tridecap.com> Subject: Re: Query only slow on first run Date: Tue, 27 Nov 2007 17:47:31 -0600 Message-ID: <05ae01c8314f$e0a86990$8e00a8c0@tridecap.com> MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit X-Mailer: Microsoft Office Outlook 11 Thread-Index: AcgxS9vMaOyNCILOSbakAZMJx38FbAAAp/4A X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.3198 In-Reply-To: X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200711/331 X-Sequence-Number: 27746 > -----Original Message----- > From: cluster > > If I disable the nested loops, the query becomes *much* slower. > > A thing that strikes me is the following. As you can see I have the > constraint: q.status = 1. Only a small subset of the data set > has this status. I have an index on q.status but for some > reason this is not used. Instead the constraint are ensured > with a "Filter: (q.status = 1)" > in an index scan for the primary key in the "q" table. If the > small subset having q.status = 1 could be isolated quickly > using an index, I would expect the query to perform better. I > just don't know why the planner doesn't use the index on q.status. > What version of Postgres are you using? Do you know what your join_collapse_limit is set to? You might be able to force it to scan for questions with a status of 1 first to see if it helps by changing the FROM clause to: FROM posts p, question_tags qt, (SELECT * FROM questions WHERE status = 1 OFFSET 0) q Dave