From: Anish Kejariwal <anishkej@yahoo.com>
To: pgsql-jdbc@postgresql.org <pgsql-jdbc@postgresql.org>
Subject: avoid prepared statements on complex queries?
Date: Tue, 15 Nov 2011 17:18:01 -0800 (PST)
Message-ID: <1321406281.74175.YahooMailNeo@web33903.mail.mud.yahoo.com> (raw)
I'm running into an issue where a complex query is performing significantly slower if I use a prepared statement.
I'm using:
-postgres 9.0.3
-postgresql-9.0-801.jdbc4.jar JDBC driver
Some 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 complex joining several tables, and the core table is partitioned over a couple hundred tables.
Here's the java code:
conn.setAutoCommit(false);
pstmt = conn.prepareStatement("select * from foo(?,?,?,?,?,?,?)");
pstmt.setFetchSize(1000);
//and, then I do the usual thing to set parameters in the prepared statement
stmt.setInt(1, SOME_NUMBER);
pstmt.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 statement, postgres is coming up with the wrong execution plan when the the parameter list is unexpectedly large. Pretty understandable.
My question: is there a work around to this? Can I force it not set the execution plan until I bind the variables?
The only reasons I'm using a prepared 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!
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-jdbc@postgresql.org
Cc: anishkej@yahoo.com
Subject: Re: avoid prepared statements on complex queries?
In-Reply-To: <1321406281.74175.YahooMailNeo@web33903.mail.mud.yahoo.com>
References: <1321406281.74175.YahooMailNeo@web33903.mail.mud.yahoo.com>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox