Received: from localhost (maia-2.hub.org [200.46.204.187]) by postgresql.org (Postfix) with ESMTP id 69ACB9FA182 for ; Wed, 20 Dec 2006 17:22:14 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.187]) (amavisd-new, port 10024) with ESMTP id 37815-10 for ; Wed, 20 Dec 2006 17:21:58 -0400 (AST) X-Greylist: from auto-whitelisted by SQLgrey-1.7.4 Received: from genomemail.chop.edu (genomemail.chop.edu [159.14.214.193]) by postgresql.org (Postfix) with ESMTP id C49D09FA248 for ; Wed, 20 Dec 2006 17:22:01 -0400 (AST) Received: from [159.14.214.141] ([159.14.214.141]) BY genomemail.chop.edu ([159.14.214.193]) WITH ESMTP (4D WebSTAR V Mail (5.3.4)); Wed, 20 Dec 2006 16:22:01 -0500 Message-ID: <4589A978.9030501@genome.chop.edu> Date: Wed, 20 Dec 2006 16:22:00 -0500 From: Kevin Murphy User-Agent: Mozilla/5.0 (Macintosh; U; PPC Mac OS X Mach-O; en-US; rv:1.8.0.8) Gecko/20061025 Thunderbird/1.5.0.8 Mnenhy/0.7.4.666 MIME-Version: 1.0 To: pgsql-jdbc@postgresql.org Subject: Re: parametrized statements, but always replan? References: <45899296.9040008@genome.chop.edu> <81F542C8-256C-4C29-AC7D-9223BBB3B2E0@fastcrypt.com> In-Reply-To: <81F542C8-256C-4C29-AC7D-9223BBB3B2E0@fastcrypt.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200612/104 X-Sequence-Number: 16336 Dave Cramer wrote: > The only time prepared statements won't replan is if you re-use the > prepared statement, which your code does not. > > However... AFAIK, postgresql does not plan based on the parameter. OK, PG definitely plans based on the parameter value. I turned on plan logging in the server log, and without specifying prepareThreshold at all (e.g. setting it to 0), I can see that PG is making different plan estimates for different parameter values. It might be using the same general plan, but I can't bear to wade through all that output to tell. Looking at ANALYZE output on the unadorned, non-prepared query, I can see that different plans may be made, depending on the parameter. In a table with millions of rows, there are tens of thousands of distinct values of this parameter, with a very wide distribution. One value has 200K occurrences, many others just a handful. Using a different plan for the outliers can be (and is) very helpful. When I wrap the query in a SQL PREPARE statement, a generic plan is made that works OK for 99.9% of the column values, but for the most common column value, that plan is 20,000 times slower than the plan that is chosen by the planner when it knows that the query involves that most common value. Hence my interest in what is going on behind prepareStatement(). I'm good now. -Kevin Murphy