X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (av.hub.org [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id 25188DACCC for ; Tue, 25 Oct 2005 11:36:54 -0300 (ADT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 62852-04 for ; Tue, 25 Oct 2005 14:36:45 +0000 (GMT) Received: from website.brandywine.com (mailcheck.brandywine.com [146.145.113.9]) by svr1.postgresql.org (Postfix) with ESMTP id F38E8DAB8F for ; Tue, 25 Oct 2005 11:36:47 -0300 (ADT) X-eSCM-MailFrom: Received: from BAM_EXCHANGE (10.1.0.100) by website.brandywine.com (10.1.0.9); Tue, 25 October 2005 10:40:23 -0400 Received: from [10.1.0.60] (gen-wayne [10.1.0.60]) by bam_exchange.brandywine.com with SMTP (Microsoft Exchange Internet Mail Service Version 5.5.2653.13) id R6ZYYMX8; Tue, 25 Oct 2005 10:36:24 -0400 Message-ID: <435E4373.7060607@brandywine.com> Date: Tue, 25 Oct 2005 10:38:43 -0400 From: Daryl Richter Reply-To: daryl@brandywine.com Organization: Brandywine Asset Management User-Agent: Mozilla Thunderbird 1.0.6 (Windows/20050716) X-Accept-Language: en-us, en MIME-Version: 1.0 Cc: pgsql-sql@postgresql.org Subject: Re: automatic update or insert References: <1129790184.351579.302550@g47g2000cwa.googlegroups.com> In-Reply-To: <1129790184.351579.302550@g47g2000cwa.googlegroups.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.05 required=5 tests=[AWL=0.050] X-Spam-Level: X-Archive-Number: 200510/287 X-Sequence-Number: 23140 tobbe wrote: > Hi. > > I have a little problem. > > In a system of mine i need to insert records into table [tbStat], and > if the records exist i need to update them instead and increase a > column [cQuantity] for every update. > > I.e. the first insert sets cQuantity to 1, and for every other run > cQuantity is increased. > Regardless of how you implemented it, this seems unwise. You can never know, nor validate, that this quantity is definitely correct. Why can't you just insert another row and then count them? If this is a transient value you might be ok, but I generally wouldn't put it in a DB in that case anyway... > Currently i have implemented this as a stored procedure in the plpgsql > language. This means that in my stored procedure i first do a select to > find out if the row exists or not, then i do a insert or update > depending if the row existed. > > Unfortunately, stored procedures seems awfully slow. And i need the > application to go faster. > > One solution could be to implement the stored procedure in my program > instead. I think that this will be atleast 50% faster than my stored > procedure, so that would be ok. > > However, this has made me thinking. Couldn't this be done directly in > SQL? > > > Brgds Robert > > > ---------------------------(end of broadcast)--------------------------- > TIP 3: Have you checked our extensive FAQ? > > http://www.postgresql.org/docs/faq -- Daryl Richter Platform Author & Director of Technology (( Brandywine Asset Management ) ( "Expanding the Science of Global Investing" ) ( http://www.brandywine.com ))