Received: from maia.hub.org (maia-2.hub.org [200.46.204.251]) by mail.postgresql.org (Postfix) with ESMTP id 46AFD4B152C for ; Wed, 16 Nov 2011 17:43:03 -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 91572-02 for ; Wed, 16 Nov 2011 21:42:57 +0000 (UTC) Received: from magus.postgresql.org (magus.postgresql.org [87.238.57.229]) by mail.postgresql.org (Postfix) with ESMTP id DDEE04B152A for ; Wed, 16 Nov 2011 17:42:56 -0400 (AST) Received: from nm18-vm0.bullet.mail.ac4.yahoo.com ([98.139.53.210]) by magus.postgresql.org with smtp (Exim 4.72) (envelope-from ) id 1RQnFv-0000zr-B6 for pgsql-jdbc@postgresql.org; Wed, 16 Nov 2011 21:42:56 +0000 Received: from [98.139.52.191] by nm18.bullet.mail.ac4.yahoo.com with NNFMP; 16 Nov 2011 21:42:42 -0000 Received: from [98.139.52.133] by tm4.bullet.mail.ac4.yahoo.com with NNFMP; 16 Nov 2011 21:42:42 -0000 Received: from [127.0.0.1] by omp1016.mail.ac4.yahoo.com with NNFMP; 16 Nov 2011 21:42:42 -0000 X-Yahoo-Newman-Property: ymail-3 X-Yahoo-Newman-Id: 286353.68704.bm@omp1016.mail.ac4.yahoo.com Received: (qmail 8420 invoked by uid 60001); 16 Nov 2011 21:42:41 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yahoo.com; s=s1024; t=1321479761; bh=EKf8M0UlW6wkCKVt9991aBy1MtL63nuhkOMeIaV+5E4=; h=X-YMail-OSG:Received:X-Mailer:References:Message-ID:Date:From:Reply-To:Subject:To:Cc:In-Reply-To:MIME-Version:Content-Type; b=PlmuGiLEdokp8QarsC3k+vpGJjnZNGiypDPFx2RlbxPaZ9c8YFTlxmi821Bn3UTeHpF64anFHr8L4hYvoUC1VuxBpz5qTMa7V/9fIyPiIY8Ds2V5AgXyVJLYMtWfxYWSqYDvZiK1D9u5h/KV5c6S285SDWnFxBqOjZFgz9UAF0k= DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=s1024; d=yahoo.com; h=X-YMail-OSG:Received:X-Mailer:References:Message-ID:Date:From:Reply-To:Subject:To:Cc:In-Reply-To:MIME-Version:Content-Type; b=WvTOeWvtSfgD4amiW/XhMTyBpZNKOOP7MaAlMbD5LCghpLqUBHhkfhPaSObVkjnPuiGTxk0Y0H8gfZhzkd9P0n7gcvDhReRKsDtgT3xSwZo+hvj3vbtloEGbkX0uxQJmQRVJwQ9lUNTCZr5DkhIXNQJipTPcp5vG/jz17AOXHEs=; X-YMail-OSG: viA6AMgVM1muiZwEZ_9lTPCWJxA_I1m32pIPBo3xIUKMorl CiODBKuEEiny8RzARX1i_EPOU3lQiOL97RY0MteS23q5u_Vwd3KIDkIKfd3R czWW7ZccVkiWGPkGBTJ69Ma2IWcTj.u5ya_rSdP9CMihmYgv0poq95Vaencf eV3rD_1kOW1oluJjkNklO7jjUWWovF0bpNjrAFvXsruCxz3.S4KRsNPiML4p 3z0fWYK5HFMIXnAGltXume8ywcgips1ygFVv_JkAaGkrXwjCBlbI6ZXS.4CT NPnesuN3MwSXWc1qt66sDnO7HR6LWGqvuxvlAIVS341SR9BWbhKnVvlHpdqh wPuv3oZXXM3e3PY9YtMmGv9PbRmloqk8CWBwl4UWMUXLjxaOEBKiew39WY.S pzueqyErqaNjur.2UdbEbOZ_LEYt8tII5a0nIZZztbOz_5Vj6TbPHD8MVIKh 44MI2BSaTMcupLucW2MQ0J9I- Received: from [70.36.236.24] by web33908.mail.mud.yahoo.com via HTTP; Wed, 16 Nov 2011 13:42:40 PST X-Mailer: YahooMailWebService/0.8.115.325013 References: <1321406281.74175.YahooMailNeo@web33903.mail.mud.yahoo.com> Message-ID: <1321479760.7925.YahooMailNeo@web33908.mail.mud.yahoo.com> Date: Wed, 16 Nov 2011 13:42:40 -0800 (PST) From: Anish Kejariwal Reply-To: Anish Kejariwal Subject: Re: avoid prepared statements on complex queries? To: Kris Jurka Cc: "pgsql-jdbc@postgresql.org" In-Reply-To: MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="-564810535-1737659172-1321479760=:7925" 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/35 X-Sequence-Number: 22370 ---564810535-1737659172-1321479760=:7925 Content-Type: text/plain; charset=iso-8859-1 Content-Transfer-Encoding: quoted-printable Hi Kris,=0A=0ASorry, if I wasn't clear. =A0I'm doing everything in JDBC.=0A= =0AIf I use a prepared statement, and set my main parameter to be an array = of 150 integers, the query takes for ever (1200 seconds). =A0If I instead s= kip using a prepared statement by doing the following, the query takes 30 s= econds:=0AStatement stmt =3D conn.createStatement();=0Astmt.setFetchSize(10= 00);=0AResultSet rs =3D stmt.executeQuery(QUERY_STRING);=0A=0Awhere QUERY_S= TRING is the entire query with the 150 integers.=0A=0AIn both cases, I'm ca= lling a SQL function:=0Acreate or replace function foo(int,int[],int[],int,= boolean, boolean, double precision)=0A.=0A.=0Awhere=0A=A0 =A0 =A0 =A0 data= set_id =3D any($2)=0A.=0A.=0A$$ language 'sql' immutable;=0A=0Athe second p= arameter is the one where the array will verify anywhere from 1 to 500 inte= gers.=0A=0AI'm going to try Maciek's suggestions (server logging, and wire = shark), =A0to see if I can verify that when I set ?prepareThreshold=3D0 in = the JDBC string, it is in fact forcing it to be an unnamed prepared stateme= nt.=0A=0Athanks,=0AAnish=0A=0A=0A=0A________________________________=0AFrom= : Kris Jurka =0ATo: Anish Kejariwal = =0ACc: "pgsql-jdbc@postgresql.org" =0ASent: Wedn= esday, November 16, 2011 1:20 PM=0ASubject: Re: [JDBC] avoid prepared state= ments on complex queries?=0A=0A=0A=0AOn Tue, 15 Nov 2011, Anish Kejariwal w= rote:=0A=0A> I'm running into an issue where a complex query is performing = significantly=0A> slower if I use a prepared statement.=0A> =0A> Some notes= about the query:=0A> -I'm calling a postgres function, via:=A0select * fro= m foo(?,?,?,?,?,?,?)=0A> -I'm getting back 4 million results=0A> -the under= lying query is quite complex joining several tables, and the core=0A> table= is partitioned over a couple hundred tables.=0A> =0A> Here's the java code= :=0A> =0A> conn.setAutoCommit(false);=0A> pstmt =3D=A0conn.prepareStatement= ("select * from foo(?,?,?,?,?,?,?)");=0A> pstmt.setFetchSize(1000);=0A> =0A= > //and, then I do the usual thing to set parameters in the prepared statem= ent=0A> stmt.setInt(1, SOME_NUMBER);=0A> pstmt.setArray(2, genepoolConn.cre= ateArrayOf("integer", ARRAY_OF_INTEGERS));=0A> //and so on=0A> =0A> So, obv= iously when using a prepared statement, postgres is coming up with=0A> the = wrong execution plan when the the parameter list is unexpectedly large.=0A= =0AYou haven't really shown that.=A0 Unless you issue the same query, manua= lly =0Ainterpolating in the query parameters and get a much faster time, al= l =0Ayou've shown is that things get slower with a larger number of values.= =0A=0AWhat language is your function written in?=A0 Unless it's a sql funct= ion =0Awhich could be inlined, I wouldn't have expected any difference betw= een =0Aprepared vs inline query execution.=0A=0AKris Jurka ---564810535-1737659172-1321479760=:7925 Content-Type: text/html; charset=iso-8859-1 Content-Transfer-Encoding: quoted-printable
Hi Kris,

Sorry, if I wasn't clear.  I'm doing everything i= n JDBC.

If I use a prepared statement, and set my main par= ameter to be an array of 150 integers, the query takes for ever (1200 secon= ds).  If I instead skip using a prepared statement by doing the follow= ing, the query takes 30 seconds:
Statement stmt =3D conn.createStatement();
stmt.setFetchSize(1000);
ResultSet rs =3D stmt.exe= cuteQuery(QUERY_STRING);

where QUERY_STRING i= s 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, boo= lean, double precision)
.
.
=0A=0A=0A=0A=0A=0A=0A=0A
w= here
=0A
        dataset_id =3D a= ny($2)
.=
.
<= font class=3D"Apple-style-span" size=3D"2">$$ language 'sql' immutable;

the second parameter is the one where the ar= ray will verify anywhere from 1 to 500 integers.

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

thanks,
Anish
=0A=0A=0A=0A= =0A=0A=0A=0A



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?

=0A

On Tue, 15 Nov 2011, Anish Kejar= iwal wrote:

> I'm running into an issue where a complex query is = performing significantly
> slower if I use a prepared statement.
&= gt;
> Some notes about the query:
> -I'm calling a postgres fu= nction, via: select * from foo(?,?,?,?,?,?,?)
> -I'm getting bac= k 4 million results
> -the underlying query is quite complex joining = several tables, and the core
> table is partitioned over a couple hun= dred tables.
>
> Here's the java code:
>
> conn.s= etAutoCommit(false);
> pstmt =3D conn.prepareStatement("select *= from foo(?,?,?,?,?,?,?)");
> pstmt.setFetchSize(1000);
>
&= gt; //and, then I do the usual thing to set parameters in the prepared stat= ement
> stmt.setInt(1, SOME_NUMBER);
> pstmt.setArray(2, genepo= olConn.createArrayOf("integer", ARRAY_OF_INTEGERS));
> //and so on>
> So, obviously when using a prepared statement, postgres is coming up with
> the wr= ong execution plan when the the parameter list is unexpectedly large.
You haven't really shown that.  Unless you issue the same query, man= ually
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 val= ues.

What language is your function written in?  Unless it's a = sql function
which could be inlined, I wouldn't have expected any diffe= rence between
prepared vs inline query execution.

Kris Jurka
=
---564810535-1737659172-1321479760=:7925--