Received: from localhost (unknown [200.46.204.182]) by postgresql.org (Postfix) with ESMTP id 058E42E008C for ; Tue, 27 Nov 2007 12:47:38 -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 35621-01 for ; Tue, 27 Nov 2007 12:47:31 -0400 (AST) Received: from news.hub.org (news.hub.org [200.46.204.72]) by postgresql.org (Postfix) with ESMTP id 81DE12E0080 for ; Tue, 27 Nov 2007 12:47:37 -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 lARGlatU060143 for ; Tue, 27 Nov 2007 12:47:36 -0400 (AST) (envelope-from news@news.hub.org) Received: (from news@localhost) by news.hub.org (8.14.1/8.14.1/Submit) id lARGXcTb056021 for pgsql-performance@postgresql.org; Tue, 27 Nov 2007 12:33:38 -0400 (AST) (envelope-from news) From: cluster X-Newsgroups: pgsql.performance Subject: Query only slow on first run Date: Tue, 27 Nov 2007 17:33:36 +0100 Organization: Hub.Org Networking Services Lines: 9 Message-ID: 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) To: pgsql-performance@postgresql.org X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200711/317 X-Sequence-Number: 27732 I have a query that takes about 7000 ms in average to complete the first time it runs. Subsequent runs complete in only 50 ms. That is more than a factor 100 faster! How can I make the query perform good in the first run too? Query and output from both first and second run of Explain Analyze is pasted here: http://rafb.net/p/yrKyoA17.html