Received: from localhost (unknown [200.46.208.211]) by mail.postgresql.org (Postfix) with ESMTP id 60710633948 for ; Mon, 4 May 2009 07:08:30 -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 50897-07 for ; Mon, 4 May 2009 07:08:20 -0300 (ADT) X-Greylist: delayed 01:04:47.12875 by SQLgrey-1.7.6 Received: from ciao.gmane.org (main.gmane.org [80.91.229.2]) by mail.postgresql.org (Postfix) with ESMTP id 2393B633936 for ; Mon, 4 May 2009 07:08:22 -0300 (ADT) Received: from list by ciao.gmane.org with local (Exim 4.43) id 1M0u5A-0000yn-VY for pgsql-jdbc@postgresql.org; Mon, 04 May 2009 09:03:28 +0000 Received: from 217.110.94.67 ([217.110.94.67]) by main.gmane.org with esmtp (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Mon, 04 May 2009 09:03:28 +0000 Received: from spam_eater by 217.110.94.67 with local (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Mon, 04 May 2009 09:03:28 +0000 X-Injected-Via-Gmane: http://gmane.org/ To: pgsql-jdbc@postgresql.org From: Thomas Kellerer Subject: Re: Very strange performance decrease when reusing a PreparedStatement Date: Mon, 04 May 2009 11:03:17 +0200 Lines: 18 Message-ID: References: <5C2E1645-73B6-4509-96CD-FA77869516AB@noopsis.fr> <49F71EDD.2070200@opencloud.com> <02437794-A78D-449C-843F-B823195F9623@noopsis.fr> <491f66a50905030310j5b4cdcd8jbf4f6e757c2ba580@mail.gmail.com> <49FDEB97.6050800@astrofoto.org> <49FE0731.2030403@kickstone.com> <49FE24D5.4060904@astrofoto.org> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Complaints-To: usenet@ger.gmane.org X-Gmane-NNTP-Posting-Host: 217.110.94.67 User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.21) Gecko/20090302 Thunderbird/2.0.0.21 Mnenhy/0.7.6.666 In-Reply-To: <49FE24D5.4060904@astrofoto.org> X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=1.599 tagged_above=0 required=5 tests=RCVD_IN_DNSWL_LOW=-1, RCVD_NUMERIC_HELO=2.599 X-Spam-Level: * X-Archive-Number: 200905/34 X-Sequence-Number: 19584 Roland Roberts, 04.05.2009 01:12: > No, my point was that PostgreSQL is doing the same thing that Oracle is > doing and in both cases it can bite you. Both are looking at the bind > variables to come up with a plan but the plan is retained for reuse > under the assumption that the bind variables will be, statistically at > least, similar the next time. When that assumption is violated, you get > stuck with a bad plan. The purpose of my Oracle example was to give a > concrete example of such a violation. Oracle 11 has introduced a feature called "Adaptive Cursor Sharing" which tries to solve this problem (Oracle 10 has some tweaks to address that problem manually, but they have their cornerstones as well) Each time a prepared statement is re-used the optimizer compares the actuals with the stored assumptions (mainly estimated rows vs. actual rows, IIRC). If those figures deviate too much, a new plan is created and used from that point onwards _for that parameter combination_. So each SQL actually has a map of prepared statements depending on the input parameters. Obviously there is still one statement execution (the first one) that is hit by the bad plan, but all subsequent executions will benefit from the new one. Might be worth considering for PostgreSQL as well :) Thomas