Received: from localhost (unknown [200.46.204.191]) by postgresql.org (Postfix) with ESMTP id 1E5D42E003F for ; Tue, 27 Nov 2007 15:04:34 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.191]) (amavisd-maia, port 10024) with ESMTP id 81289-09 for ; Tue, 27 Nov 2007 15:04:30 -0400 (AST) Received: from globalrelay.com (mail1.globalrelay.com [204.92.49.200]) by postgresql.org (Postfix) with ESMTP id 483782E3064 for ; Tue, 27 Nov 2007 15:04:30 -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 165750225; Tue, 27 Nov 2007 19:04:28 +0000 From: "Dave Dutcher" To: "'cluster'" , References: <20071127165543.GY11978@crankycanuck.ca> <23999.1196184368@sss.pgh.pa.us> Subject: Re: Query only slow on first run Date: Tue, 27 Nov 2007 13:04:28 -0600 Message-ID: <056501c83128$55d58a90$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: AcgxJi8sZf89/zRDRsm6anAoIx50ZQAADGDw 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/325 X-Sequence-Number: 27740 > -----Original Message----- > From: cluster > > >> Probably by buying much faster disk hardware. > > Or buy more RAM, so that the data can stay cached. > > So the only problem here is lack of RAM and/or disk speed? I don't think you can reach that conclusion yet. Like everybody said the reason the query was faster the second time was that the disk pages were cached in RAM, and pulling the data out of RAM is way faster than disk. If I were you, I would try to optimize the query for when the disk pages aren't in RAM. In order to test the query without having anything cached you need to clear out Postgres's shared buffers and the OS cache. That can be tricky, but it may be as easy as running a big select on another table. 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? You aren't disabling hash joins or merge joins are you? 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 it is faster without nested loops, then you might need to look into changing some settings. Dave