Received: from localhost (unknown [200.46.204.184]) by postgresql.org (Postfix) with ESMTP id 671F52E32BE for ; Tue, 27 Nov 2007 19:17:42 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.184]) (amavisd-maia, port 10024) with ESMTP id 79494-04 for ; Tue, 27 Nov 2007 19:17:36 -0400 (AST) Received: from news.hub.org (news.hub.org [200.46.204.72]) by postgresql.org (Postfix) with ESMTP id 0A8012E32B7 for ; Tue, 27 Nov 2007 19:17:42 -0400 (AST) Received: from news.hub.org (news.hub.org [200.46.204.72]) by news.hub.org (8.14.1/8.14.1) with ESMTP id lARNHbOu052891 for ; Tue, 27 Nov 2007 19:17:37 -0400 (AST) (envelope-from news@news.hub.org) Received: (from news@localhost) by news.hub.org (8.14.1/8.14.1/Submit) id lARMpbFN024973 for pgsql-performance@postgresql.org; Tue, 27 Nov 2007 18:51:37 -0400 (AST) (envelope-from news) From: cluster X-Newsgroups: pgsql.performance Subject: Re: Query only slow on first run Date: Tue, 27 Nov 2007 23:51:40 +0100 Organization: Hub.Org Networking Services Lines: 27 Message-ID: References: <20071127165543.GY11978@crankycanuck.ca> <23999.1196184368@sss.pgh.pa.us> <056501c83128$55d58a90$8e00a8c0@tridecap.com> Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Complaints-To: usenet@news.hub.org User-Agent: Thunderbird 2.0.0.6 (X11/20071022) In-Reply-To: <056501c83128$55d58a90$8e00a8c0@tridecap.com> To: pgsql-performance@postgresql.org X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200711/329 X-Sequence-Number: 27744 > As for optimizing the query, I noticed that all three joins are done by > nested loops. I wonder if another join method would be faster. Have you > analyzed all the tables? Yes. I did a VACUUM FULL ANALYZE before running the test queries. Also I have just performed an ANALYZE just to be sure everything was really analyzed. > You aren't disabling hash joins or merge joins are > you? Nope. > If you aren't, then as a test I would try disabling nested loops by > doing "set enable_nestloop=false" and see if the query is any faster for > you. 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.