Received: from localhost (maia-4.hub.org [200.46.204.183]) by postgresql.org (Postfix) with ESMTP id 05EF19FB224 for ; Tue, 5 Jun 2007 10:06:38 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.183]) (amavisd-maia, port 10024) with ESMTP id 35457-01 for ; Tue, 5 Jun 2007 10:06:33 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey-1.7.5 Received: from mail.metronet.co.uk (mail.metronet.co.uk [213.162.97.75]) by postgresql.org (Postfix) with ESMTP id 74AEE9FB7BF for ; Tue, 5 Jun 2007 05:55:38 -0300 (ADT) Received: from server3.office.archonet.com (84-51-143-99.archon037.adsl.metronet.co.uk [84.51.143.99]) by smtp.metronet.co.uk (MetroNet Mail) with ESMTP id 9F43C762EE; Tue, 5 Jun 2007 09:50:56 +0100 (BST) Received: from [192.168.1.36] (dell36.office.archonet.com [192.168.1.36]) by server3.office.archonet.com (Postfix) with ESMTP id 7190C410CE4; Tue, 5 Jun 2007 09:51:46 +0100 (BST) Message-ID: <466523F0.5020306@archonet.com> Date: Tue, 05 Jun 2007 09:50:56 +0100 From: Richard Huxton User-Agent: Thunderbird 2.0.0.0 (X11/20070326) MIME-Version: 1.0 To: Jason Lustig Cc: pgsql-performance@postgresql.org Subject: Re: Question about SQL performance References: In-Reply-To: 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: 200706/60 X-Sequence-Number: 25093 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? PG will plan "raw" sql every time you issue a query. > SELECT * FROM mytable WHERE item = 5; > SELECT * FROM mytable WHERE item = 10; > > Obviously to me or you they could use the same plan. Except that in-between query 1 and 2 I inserted 10 million rows where item=10. Still obvious? > 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. If you are using PREPARE/EXECUTE (or your client-side library is doing it for you). > Also, what's the difference between prepared statements (using PREPARE > and EXECUTE) and regular functions (CREATE FUNCTION)? How do they impact > performance? Functions can be in any language, but if they both are in SQL and do the same thing, no real difference. > From what I understand there is no exact parallel to stored > procedures (as in MS SQL or oracle, that are completely precompiled) in > Postgresql. You can write functions in C - that's compiled. Not sure if java procedural code has its byte-code cached between sessions. > 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 don't see the connection. 1. You can write procedural code in pl/pgsql 2. It's not precompiled (it's "compiled" on first use) Are you looking to solve a particular problem? -- Richard Huxton Archonet Ltd