Received: from localhost (unknown [200.46.204.184]) by postgresql.org (Postfix) with ESMTP id 786FA2E016D for ; Sat, 23 Feb 2008 15:36:51 -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 78444-01-9 for ; Sat, 23 Feb 2008 15:36:45 -0400 (AST) X-Greylist: delayed 00:13:12.013688 by SQLgrey-1.7.5 Received: from mail01.enterprisedb.com (mail01.enterprisedb.com [63.246.7.168]) by postgresql.org (Postfix) with ESMTP id 5DC062E00F7 for ; Sat, 23 Feb 2008 15:30:37 -0400 (AST) thread-index: Ach2UMx/02TGogIgTw2pNItlWfLUsA== Received: from [10.59.59.74] ([83.217.158.166]) by mail01.enterprisedb.com over TLS secured channel with Microsoft SMTPSVC(6.0.3790.3959); Sat, 23 Feb 2008 14:17:57 -0500 Content-Class: urn:content-classes:message Importance: normal X-MimeOLE: Produced By Microsoft MimeOLE V6.00.3790.4133 Message-ID: <47C0713F.7030804@enterprisedb.com> Date: Sat, 23 Feb 2008 19:17:19 +0000 From: "Heikki Linnakangas" Organization: EnterpriseDB User-Agent: Mozilla-Thunderbird 2.0.0.9 (X11/20080110) MIME-Version: 1.0 To: "Paul Tomblin" Cc: Subject: Re: Does "preparing" a PreparedStatement really help? References: <47C04916.2070500@gmail.com> In-Reply-To: <47C04916.2070500@gmail.com> Content-Type: text/plain; format=flowed; charset="ISO-8859-1" Content-Transfer-Encoding: 7bit X-OriginalArrivalTime: 23 Feb 2008 19:17:57.0968 (UTC) FILETIME=[CC741500:01C87650] X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0 tagged_above=0 required=5 tests=none X-Spam-Level: X-Archive-Number: 200802/187 X-Sequence-Number: 18394 Paul Tomblin wrote: > Is there a performance advantage for preparing a PreparedStatement and > keeping it around and using it thousands of times rather than making a > new Statement every time? Yes. > How big? It depends. If your queries are not very complex, or the little bit of extra CPU usage isn't a problem for you because the bottlenecks are elsewhere, it might be completely insignificant for you. You would have to test it with your application to known for sure. > Back when I was doing Oracle Call Interface programming in C back in the > mid to late 1980s, we were always told that pre-parsing a query was very > expensive and so you tried not to do it very often, and once you'd done > it, you stored them to reuse. As I try to switch this system over to > using a connection pool, trying to store PreparedStatements for each > connection is fairly complicated and I'm wondering if it's worth it. Yeah, with a connection pool you can't really do it like that. You want to use a technique called "statement caching", where the connection pool / driver keeps a cache of prepared statements, so that when you create a new PreparedStatement and prepare it, it actually reuses an already prepared one from the cache. Many if not most connection pool implementations have a statement cache, but you might need to so something to enable it; check the docs. There's also a stand-alone statement cache implementation at http://jdbccache.projects.postgresql.org/ which you can use. -- Heikki Linnakangas EnterpriseDB http://www.enterprisedb.com