pg.ddx.io  pgsql-jdbc@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Anish Kejariwal <anishkej@yahoo.com>
To: Kris Jurka <books@ejurka.com>
Cc: pgsql-jdbc@postgresql.org <pgsql-jdbc@postgresql.org>
Subject: Re: avoid prepared statements on complex queries?
Date: Wed, 16 Nov 2011 13:42:40 -0800 (PST)
Message-ID: <1321479760.7925.YahooMailNeo@web33908.mail.mud.yahoo.com> (raw)
In-Reply-To: <alpine.BSO.2.00.1111161617200.14447@leary.csoft.net>
References: <1321406281.74175.YahooMailNeo@web33903.mail.mud.yahoo.com>
	<alpine.BSO.2.00.1111161617200.14447@leary.csoft.net>

Hi Kris,

Sorry, if I wasn't clear.  I'm doing everything in JDBC.

If I use a prepared statement, and set my main parameter to be an array of 150 integers, the query takes for ever (1200 seconds).  If I instead skip using a prepared statement by doing the following, the query takes 30 seconds:
Statement stmt = conn.createStatement();
stmt.setFetchSize(1000);
ResultSet rs = stmt.executeQuery(QUERY_STRING);

where QUERY_STRING is the entire query with the 150 integers.

In both cases, I'm calling a SQL function:
create or replace function foo(int,int[],int[],int, boolean, boolean, double precision)
.
.
where
        dataset_id = any($2)
.
.
$$ language 'sql' immutable;

the second parameter is the one where the array will verify anywhere from 1 to 500 integers.

I'm going to try Maciek's suggestions (server logging, and wire shark),  to see if I can verify that when I set ?prepareThreshold=0 in the JDBC string, it is in fact forcing it to be an unnamed prepared statement.

thanks,
Anish



________________________________
From: Kris Jurka <books@ejurka.com>
To: Anish Kejariwal <anishkej@yahoo.com>
Cc: "pgsql-jdbc@postgresql.org" <pgsql-jdbc@postgresql.org>
Sent: Wednesday, November 16, 2011 1:20 PM
Subject: Re: [JDBC] avoid prepared statements on complex queries?



On Tue, 15 Nov 2011, Anish Kejariwal wrote:

> I'm running into an issue where a complex query is performing significantly
> slower if I use a prepared statement.
> 
> 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
> 
> So, obviously when using a prepared statement, postgres is coming up with
> the wrong execution plan when the the parameter list is unexpectedly large.

You haven't really shown that.  Unless you issue the same query, manually 
interpolating in the query parameters and get a much faster time, all 
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 
which could be inlined, I wouldn't have expected any difference between 
prepared vs inline query execution.

Kris Jurka

view thread (9+ messages)  latest in thread

Message-ID: <1321479760.7925.YahooMailNeo@web33908.mail.mud.yahoo.com>
Permalink:  ../1321479760.7925.YahooMailNeo@web33908.mail.mud.yahoo.com/
Also on:    postgresql.org/message-id/1321479760.7925.YahooMailNeo@web33908.mail.mud.yahoo.com

 · 

reply

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, books@ejurka.com
  Subject: Re: avoid prepared statements on complex queries?
  In-Reply-To: <1321479760.7925.YahooMailNeo@web33908.mail.mud.yahoo.com>
  References: <1321406281.74175.YahooMailNeo@web33903.mail.mud.yahoo.com> <alpine.BSO.2.00.1111161617200.14447@leary.csoft.net> <1321479760.7925.YahooMailNeo@web33908.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