Received: from maia.hub.org (maia-2.hub.org [200.46.204.251]) by mail.postgresql.org (Postfix) with ESMTP id DE36015B5F6 for ; Wed, 16 Nov 2011 17:20:42 -0400 (AST) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.251]) (amavisd-maia, port 10024) with ESMTP id 70124-02 for ; Wed, 16 Nov 2011 21:20:29 +0000 (UTC) Received: from makus.postgresql.org (makus.postgresql.org [98.129.198.125]) by mail.postgresql.org (Postfix) with ESMTP id B64DA15B5F4 for ; Wed, 16 Nov 2011 17:20:27 -0400 (AST) Received: from mail63.csoft.net ([205.205.214.4]) by makus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1RQmu9-0001pG-A0 for pgsql-jdbc@postgresql.org; Wed, 16 Nov 2011 21:20:26 +0000 Received: from mail63.csoft.net (localhost [127.0.0.1]) by mail63.csoft.net (Postfix) with ESMTP id 9E76A284DE; Wed, 16 Nov 2011 16:20:12 -0500 (EST) DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=csoft.net; h=date:from:to :cc:subject:in-reply-to:message-id:references:mime-version :content-type; s=sel1; bh=qBB9zMkIEo8b6HvQt5E+/JEofkE=; b=p9FMql hD5TlT/1gWC7B2CDGQX5ZF5vZ+4X20hw0BuCl100dh1LUiSrpTpx84KYKLxqFN9E jKM+wv0rgSjEnrv/XgeXf0NutVVpU1/C/aLqPFWtAz1bQlSdbeEVRctMNFJV5r7w F9yIQK5hFbDgic6xW7YDpQ2AicsOyUuLzoP5Q= Received: from leary.csoft.net (leary.csoft.net [205.205.214.5]) by mail63.csoft.net (Postfix) with ESMTP id 9C084284DD; Wed, 16 Nov 2011 16:20:12 -0500 (EST) Received: by leary.csoft.net (Postfix, from userid 1112) id 39ABFA188; Wed, 16 Nov 2011 16:20:12 -0500 (EST) Date: Wed, 16 Nov 2011 16:20:10 -0500 (EST) From: Kris Jurka X-X-Sender: books@leary.csoft.net To: Anish Kejariwal cc: "pgsql-jdbc@postgresql.org" Subject: Re: avoid prepared statements on complex queries? In-Reply-To: <1321406281.74175.YahooMailNeo@web33903.mail.mud.yahoo.com> Message-ID: References: <1321406281.74175.YahooMailNeo@web33903.mail.mud.yahoo.com> User-Agent: Alpine 2.00 (BSO 1167 2008-08-23) MIME-Version: 1.0 X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.9 tagged_above=-10 required=5 tests=BAYES_00=-1.9 X-Spam-Level: Content-Type: TEXT/PLAIN; charset=iso-8859-1 Content-Transfer-Encoding: QUOTED-PRINTABLE X-Archive-Number: 201111/31 X-Sequence-Number: 22366 On Tue, 15 Nov 2011, Anish Kejariwal wrote: > I'm running into an issue where a complex query is performing significant= ly > slower if I use a prepared statement. >=20 > Some notes about the query: > -I'm calling a postgres function, via:=A0select * from foo(?,?,?,?,?,?,?) > -I'm getting back 4 million results > -the underlying query is quite complex joining several tables, and the co= re > table is partitioned over a couple hundred tables. >=20 > Here's the java code: >=20 > conn.setAutoCommit(false); > pstmt =3D=A0conn.prepareStatement("select * from foo(?,?,?,?,?,?,?)"); > pstmt.setFetchSize(1000); >=20 > //and, then I do the usual thing to set parameters in the prepared statem= ent > stmt.setInt(1, SOME_NUMBER); > pstmt.setArray(2, genepoolConn.createArrayOf("integer", ARRAY_OF_INTEGERS= )); > //and so on >=20 > So, obviously when using a prepared statement, postgres is coming up with > the wrong execution plan when the the parameter list is unexpectedly larg= e. You haven't really shown that. Unless you issue the same query, manually= =20 interpolating in the query parameters and get a much faster time, all=20 you've shown is that things get slower with a larger number of values. What language is your function written in? Unless it's a sql function=20 which could be inlined, I wouldn't have expected any difference between=20 prepared vs inline query execution. Kris Jurka=