Received: from localhost (unknown [200.46.208.211]) by mail.postgresql.org (Postfix) with ESMTP id 99B93632CE4 for ; Sun, 3 May 2009 16:25:07 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by localhost (mx1.hub.org [200.46.208.211]) (amavisd-maia, port 10024) with ESMTP id 31057-07 for ; Sun, 3 May 2009 16:25:00 -0300 (ADT) X-Greylist: delayed 00:16:48.894228 by SQLgrey-1.7.6 Received: from mx.astrofoto.org (mx.astrofoto.org [216.254.78.156]) by mail.postgresql.org (Postfix) with ESMTP id 369CB632BCE for ; Sun, 3 May 2009 16:25:04 -0300 (ADT) Message-ID: <49FDEB97.6050800@astrofoto.org> Date: Sun, 03 May 2009 15:08:07 -0400 From: Roland Roberts User-Agent: Thunderbird 2.0.0.19 (X11/20090105) MIME-Version: 1.0 To: pgsql-jdbc@postgresql.org References: <5C2E1645-73B6-4509-96CD-FA77869516AB@noopsis.fr> <49F71EDD.2070200@opencloud.com> <02437794-A78D-449C-843F-B823195F9623@noopsis.fr> <491f66a50905030310j5b4cdcd8jbf4f6e757c2ba580@mail.gmail.com> In-Reply-To: Subject: Re: Very strange performance decrease when reusing a PreparedStatement Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-SA-Exim-Version: 4.2 (built Tue, 02 May 2006 07:36:10 -0400) X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0 tagged_above=0 required=5 tests=none X-Spam-Level: X-Archive-Number: 200905/22 X-Sequence-Number: 19572 Péter Kovács wrote: > http://jdbc.postgresql.org/documentation/83/server-prepare.html: > > "Server side prepared statements are planned only once by the server. > This avoids the cost of replanning the query every time, but also > means that the planner cannot take advantage of the particular > parameter values used in a particular execution of the query. You > should be cautious about enabling the use of server side prepared > statements globally." > > Or is this only the JDBC perspective of things, meaning that the > capability is there at the back-end, just the JDBC driver doesn't take > advantage of it? > I believe the above documentation exactly describes the recent performance problem. The issue is that the next time you use the same query, your parameters may be sufficiently different that the optimizer *would* have picked a different index, but because the query has been parsed and planned, the old one is used. This problem is *not* unique to PostgreSQL. We recently encountered exactly the same problem with Oracle 10g. In our case, the query included the equivalent of a date range as "where event_time between ? and ?". For one query, the range covered less than 24 hours and was likely to return only a single row. In aother base, the range included a full calendar year. Depending on which query happened "first" (as far as the database prepared statement cache was concerned), very different plans could result. Of the two plans, the one for a year's data worked acceptably for both queries, but the plan for a day's data resulted in abysmal performance for the year's data. At least PostgreSQL has the ability to tell the parser to reparse every time you ask it to prepare the statement. Oracle doesn't. The solution for Oracle isn't relevant, but the real point here is that your code may have to become smarter to help out the optimizer. We reparse on every execution, but can't control Oracle's server-side cache. PostgreSQL lets you control that with prepareThreshold=0. If we'd had that option with Oracle, we would have had an easy solution. I'd say PostgreSQL's implementation for this case is a good one. roland -- PGP Key ID: 66 BC 3B CD Roland B. Roberts, PhD RL Enterprises roland@rlenter.com 6818 Madeline Court roland@astrofoto.org Brooklyn, NY 11220