Received: from maia.hub.org (maia-2.hub.org [200.46.204.251]) by mail.postgresql.org (Postfix) with ESMTP id 5BD9614CB3D6 for ; Tue, 15 Nov 2011 21:26:46 -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 33471-01-9 for ; Wed, 16 Nov 2011 01:26:39 +0000 (UTC) X-Greylist: delayed 00:07:10.020558 by SQLgrey-1.8.0-rc2 Received: from makus.postgresql.org (makus.postgresql.org [98.129.198.125]) by mail.postgresql.org (Postfix) with ESMTP id 7D032C9EA90 for ; Tue, 15 Nov 2011 21:26:22 -0400 (AST) Received: from nm22-vm0.bullet.mail.sp2.yahoo.com ([98.139.91.222]) by makus.postgresql.org with smtp (Exim 4.72) (envelope-from ) id 1RQU9f-0005W2-Va for pgsql-jdbc@postgresql.org; Wed, 16 Nov 2011 01:19:12 +0000 Received: from [98.139.91.68] by nm22.bullet.mail.sp2.yahoo.com with NNFMP; 16 Nov 2011 01:18:59 -0000 Received: from [98.139.91.56] by tm8.bullet.mail.sp2.yahoo.com with NNFMP; 16 Nov 2011 01:18:01 -0000 Received: from [127.0.0.1] by omp1056.mail.sp2.yahoo.com with NNFMP; 16 Nov 2011 01:18:01 -0000 X-Yahoo-Newman-Property: ymail-3 X-Yahoo-Newman-Id: 605372.53037.bm@omp1056.mail.sp2.yahoo.com Received: (qmail 80198 invoked by uid 60001); 16 Nov 2011 01:18:01 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yahoo.com; s=s1024; t=1321406281; bh=hgXdBJ07FtzDWYONoFv+Zn5ExUXIMs4dYcO9G+bXF+c=; h=X-YMail-OSG:Received:X-Mailer:Message-ID:Date:From:Reply-To:Subject:To:MIME-Version:Content-Type; b=CnkA/rNaNPQQJX+OwbWKVIFkRGVGD7H1JvApMqFneshDFVcYwmJkJW+3//ImBKgyDuuGM4v5NOzoiEspgSCnNsuCppBaL/i61sTSUrqiNGcMUeah2jlpN9fR7qRZVMYs7bgBLl+mFVrewbLbSJ6RH4YMKK3vlraEMAohBQNdhZs= DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=s1024; d=yahoo.com; h=X-YMail-OSG:Received:X-Mailer:Message-ID:Date:From:Reply-To:Subject:To:MIME-Version:Content-Type; b=FMipbe5Mkq2hvzWvT3kod5PPW1zThzT+mTIBiEasrwYsjkiQ3iil7ZW+l9rX8iNto9vERX+UQqf2y5nSLy0CpZpg421P6cYD8uUWEGq+fuOb638jkhWApxlh6UCG+QBpXfuMJxG96qSn4y8GK9nkWrMRbAxRjlxRvVrNStW4//Q=; X-YMail-OSG: OEeOHyAVM1nnp1_n9TG1P_._vHq_emvxFSNeq8ZjC59zb0o R4U9manCKaJeRxPxVUbowl4yskzbqQ9W7A0XOo4HDjeSDAYsAlK9aifN0txb oXHPrUxmujW0hvCFrqjg1M4XkNPQajswntvx36pXzy98H3b6OowjdceT.JQq oWlrJSa0bf1qqnNpGIf8tCf2SkJiYAaDqzFgnDbxfRbFVnXeGLN07dQk55iA kQ2WK0fOcgBW6gq0aUvePW7DcIAQa8mEa9poGsbqlFfaYCBClUSG1LXIhRZ4 Rr.YbVtzejfXZkmrfvZWKa6YECYGk._nSCG44iFDBeGOQNnzgZV_fOAQcrA8 4k0PKUDXSsdF1lovdYwfikW1YmS7GnlSC4CUWSawK35mgPfHZlCCl.qEhKY8 c8rt5841CU8KYZ_z.ubrqeTitaLzjJpysHGffvO.KF3SmejzjFcA.j10Iogw - Received: from [12.192.226.6] by web33903.mail.mud.yahoo.com via HTTP; Tue, 15 Nov 2011 17:18:01 PST X-Mailer: YahooMailWebService/0.8.115.325013 Message-ID: <1321406281.74175.YahooMailNeo@web33903.mail.mud.yahoo.com> Date: Tue, 15 Nov 2011 17:18:01 -0800 (PST) From: Anish Kejariwal Reply-To: Anish Kejariwal Subject: avoid prepared statements on complex queries? To: "pgsql-jdbc@postgresql.org" MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="-876930380-862259111-1321406281=:74175" X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.898 tagged_above=-10 required=5 tests=BAYES_00=-1.9, FREEMAIL_FROM=0.001, HTML_MESSAGE=0.001 X-Spam-Level: X-Archive-Number: 201111/26 X-Sequence-Number: 22361 ---876930380-862259111-1321406281=:74175 Content-Type: text/plain; charset=iso-8859-1 Content-Transfer-Encoding: quoted-printable I'm running into an issue where a complex query is performing significantly= slower if I use a prepared statement.=0A=0AI'm using:=0A-postgres 9.0.3=0A= =0A-postgresql-9.0-801.jdbc4.jar =A0JDBC driver=0A=0ASome notes about the q= uery:=0A-I'm calling a postgres function, via:=A0select * from foo(?,?,?,?,= ?,?,?)=0A-I'm getting back 4 million results=0A-the underlying query is qui= te complex joining several tables, and the core table is partitioned over a= couple hundred tables.=0A=0A=0AHere's the java code:=0A=0Aconn.setAutoComm= it(false);=0Apstmt =3D=A0conn.prepareStatement("select * from foo(?,?,?,?,?= ,?,?)");=0Apstmt.setFetchSize(1000);=0A=0A//and, then I do the usual thing = to set parameters in the prepared statement=0Astmt.setInt(1, SOME_NUMBER);= =0Apstmt.setArray(2, genepoolConn.createArrayOf("integer", ARRAY_OF_INTEGER= S));=0A//and so on=0A=0AWhat I have found:=0A-if=A0ARRAY_OF_INTEGERS has a = length of 3, the query performs fast=0A-if=A0ARRAY_OF_INTEGERS has a length= of 150, then query takes 1200 seconds=0A-if=A0ARRAY_OF_INTEGERS has a leng= th of 150, and I don't use a prepared statement, the query takes the expect= ed 30 seconds=0A=0ASo, obviously when using a prepared statement, postgres = is coming up with the wrong execution plan when the the parameter list is u= nexpectedly large. =A0Pretty understandable.=0A=0AMy question:=A0is there a= work around to this? =A0Can I force it not set the execution plan until I = bind the variables?=0A=0AThe only reasons I'm using a prepared statement:= =0A-parameterized queries are far easier to work with than building my quer= y via string concatenation=0A-minimize chances of SQL Injection=0A=0AIf onl= y there was a way to have parameterized queries without using prepared stat= ements....=0A=0AThanks! ---876930380-862259111-1321406281=:74175 Content-Type: text/html; charset=iso-8859-1 Content-Transfer-Encoding: quoted-printable

I'm using:
-postgres 9.0.3

Som= e notes about the query:
-I'm calling a postgres function, via: = select * from foo(?,?,?,?,?,?,?)
-I'm getting back = 4 million results
-the underlying query is quite com= plex joining several tables, and the core table is partitioned over a coupl= e hundred tables.

He= re's the java code:

=0A= =0A=0A=0A=0A=0A=0A=0A
=09=09=09conn.setAutoCommit(false);
=0A=0A=0A=0A=0A=0A=0A=0A
=09=09=09=09pstm= t =3D conn.prepareStatement("select * from foo(?,?,= ?,?,?,?,?)");
=0A
=09=09=09=09pstmt.setFetch= Size(1000);

//and, then = I do the usual thing to set parameters in the prepared statement
stmt.setInt(1, SOME_NUMBER);
=0A
=09=09pstmt.setArray(2, genepoolConn.createArrayOf(= "integer", ARRAY_OF_INTEGERS));
//and so on

= What I have found:
-if ARRAY_OF_INTEGERS has a = length of 3, the query performs fast
-if ARRAY_= OF_INTEGERS has a length of 150, then query takes 1200 seconds
-if ARRAY_OF_INTEGERS has a length of 150, and I don't use = a prepared statement, the query takes the expected 30 seconds

So, obviously when using a prepared s= tatement, postgres is coming up with the wrong execution plan when the the = parameter list is unexpectedly large.  Pretty understandable.

My question: is there a wor= k around to this?  Can I force it not set the execution plan until I bind the variables?

The only reasons I'm using a prepa= red statement:
-parameterized queries are far easier= to work with than building my query via string concatenation
-minimize chances of SQL Injection

If only there was a way to have parameterized queries = without using prepared statements....

Thanks!





---876930380-862259111-1321406281=:74175--