Received: from localhost (unknown [200.46.204.183]) by postgresql.org (Postfix) with ESMTP id 6795C2E2EC5 for ; Tue, 27 Nov 2007 12:57:42 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.183]) (amavisd-maia, port 10024) with ESMTP id 71050-02 for ; Tue, 27 Nov 2007 12:57:38 -0400 (AST) Received: from outbound-mail-47.bluehost.com (outbound-mail-47.bluehost.com [69.89.18.16]) by postgresql.org (Postfix) with SMTP id 393A12E2F2F for ; Tue, 27 Nov 2007 12:57:37 -0400 (AST) Received: (qmail 5544 invoked by uid 0); 27 Nov 2007 16:57:35 -0000 Received: from unknown (HELO box54.bluehost.com) (69.89.20.54) by mailproxy3.bluehost.com with SMTP; 27 Nov 2007 16:57:35 -0000 Received: from corp.messageone.com ([66.219.55.2] helo=[10.0.51.190]) by box54.bluehost.com with esmtp (Exim 4.68) (envelope-from ) id 1Ix3kc-0007Ig-DH for pgsql-performance@postgresql.org; Tue, 27 Nov 2007 09:57:35 -0700 From: Kevin Kempter To: pgsql-performance@postgresql.org Subject: Re: Query only slow on first run Date: Tue, 27 Nov 2007 09:57:07 -0700 User-Agent: KMail/1.9.6 (enterprise 0.20071012.724442) References: In-Reply-To: MIME-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit Content-Disposition: inline Message-Id: <200711270957.07818.kevin@kevinkempterllc.com> X-Identified-User: {840:box54.bluehost.com:kevinkem:kevinkempterllc.com} {sentby:bopbeforesmtp 66.219.55.2 authed with kevinkempterllc.com} X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0.475 tagged_above=0 required=5 tests=AWL=-0.656, BAYES_50=0.001, DNS_FROM_OPENWHOIS=1.13 X-Spam-Level: X-Archive-Number: 200711/319 X-Sequence-Number: 27734 On Tuesday 27 November 2007 09:33:36 cluster wrote: > 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 > > ---------------------------(end of broadcast)--------------------------- > TIP 9: In versions below 8.0, the planner will ignore your desire to > choose an index scan if your joining column's datatypes do not > match The query is faster the second run because the data pages were pulled into the buffer pool during the first run. I would suggest using the explain plan from the first run and test your changes on a recently started instance (or at least on an instance where enough activity has transpired to effectively rotate the buffer pages). /Kevin