Received: from localhost (maia-5.hub.org [200.46.204.182]) by postgresql.org (Postfix) with ESMTP id B52D49FC846 for ; Tue, 5 Jun 2007 09:59:53 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.182]) (amavisd-maia, port 10024) with ESMTP id 13110-05 for ; Tue, 5 Jun 2007 09:59:42 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey-1.7.5 Received: from svr4.postgresql.org (svr4.postgresql.org [66.98.251.159]) by postgresql.org (Postfix) with ESMTP id 22F279FA2D8 for ; Tue, 5 Jun 2007 02:24:20 -0300 (ADT) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.3 Received: from mark.mielke.cc (206-248-136-41.dsl.teksavvy.com [206.248.136.41]) by svr4.postgresql.org (Postfix) with ESMTP id 4B5F65AF2CB for ; Tue, 5 Jun 2007 05:24:14 +0000 (GMT) Received: from mark.mielke.cc (localhost.localdomain [127.0.0.1]) by mark.mielke.cc (8.14.1/8.14.1) with ESMTP id l555NDw4016377; Tue, 5 Jun 2007 01:23:13 -0400 Received: (from mark@localhost) by mark.mielke.cc (8.14.1/8.14.1/Submit) id l555NCbb016376; Tue, 5 Jun 2007 01:23:12 -0400 Date: Tue, 5 Jun 2007 01:23:12 -0400 From: mark@mark.mielke.cc To: Jason Lustig Cc: pgsql-performance@postgresql.org Subject: Re: Question about SQL performance Message-ID: <20070605052312.GA16345@mark.mielke.cc> References: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.5.14 (2007-02-12) X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0.855 tagged_above=0 required=5 tests=AWL=-0.242, BAYES_50=0.001, FORGED_RCVD_HELO=0.135, NO_REAL_NAME=0.961 X-Spam-Level: X-Archive-Number: 200706/59 X-Sequence-Number: 25092 On Mon, Jun 04, 2007 at 11:18:30PM -0400, Jason Lustig wrote: > I have some questions about the performance of certain types of SQL > statements. > > What sort of speed increase is there usually with binding parameters > (and thus preparing statements) v. straight sql with interpolated > variables? Will Postgresql realize that the following queries are > effectively the same (and thus re-use the query plan) or will it > think they are different? > > SELECT * FROM mytable WHERE item = 5; > SELECT * FROM mytable WHERE item = 10; > > Obviously to me or you they could use the same plan. From what I > understand (correct me if I'm wrong), if you use parameter binding - > like "SELECT * FROM mytable WHERE item = ?" - Postgresql will know > that the queries can re-use the query plan, but I don't know if the > system will recognize this with above situation. Although they could use the same plan, it is possible that using the same plan is non-optimal. For example, if I know that 99% of the table contains item = 5, but only 1% of the table contains item = 10, then the 'best plan' may be a sequential scan for item = 5, but an index scan for item = 10. In the case of a prepared query, PostgreSQL will pick a plan that will be good for all values, which may not be best for specific queries. You save parsing time and planning time, but may risk increasing execution time. > Also, what's the difference between prepared statements (using > PREPARE and EXECUTE) and regular functions (CREATE FUNCTION)? How do > they impact performance? From what I understand there is no exact > parallel to stored procedures (as in MS SQL or oracle, that are > completely precompiled) in Postgresql. At the same time, the > documentation (and other sites as well, probably because they don't > know what they're talking about when it comes to databases) is vague > because PL/pgSQL is often said to be able to write stored procedures > but nowhere does it say that PL/pgSQL programs are precompiled. I think you can find all of these answers in the documentation, including my comments about prepared queries. Does it matter if the program is precompiled? I believe it is, but why would it matter? Are you addressing a real performance problem? Or are you trying to avoid issues that you are not sure if they exist or not? :-) Prepared queries are going to improve performance due to being able to execute multiple queries without communicating back to the client. Especially for short queries, network latency can be a significant factor for execution speed. Cheers, mark -- mark@mielke.cc / markm@ncf.ca / markm@nortel.com __________________________ . . _ ._ . . .__ . . ._. .__ . . . .__ | Neighbourhood Coder |\/| |_| |_| |/ |_ |\/| | |_ | |/ |_ | | | | | | \ | \ |__ . | | .|. |__ |__ | \ |__ | Ottawa, Ontario, Canada One ring to rule them all, one ring to find them, one ring to bring them all and in the darkness bind them... http://mark.mielke.cc/