Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1ckcGN-0007jn-07 for pgsql-sql@arkaria.postgresql.org; Sun, 05 Mar 2017 19:56:15 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84_2) (envelope-from ) id 1ckcGL-0002iE-GX for pgsql-sql@arkaria.postgresql.org; Sun, 05 Mar 2017 19:56:13 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1ckcGK-0002hw-Pl for pgsql-sql@postgresql.org; Sun, 05 Mar 2017 19:56:12 +0000 Received: from out4-smtp.messagingengine.com ([66.111.4.28]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1ckcGE-0005Ul-31 for pgsql-sql@postgresql.org; Sun, 05 Mar 2017 19:56:11 +0000 Received: from compute6.internal (compute6.nyi.internal [10.202.2.46]) by mailout.nyi.internal (Postfix) with ESMTP id 287E520842; Sun, 5 Mar 2017 14:56:04 -0500 (EST) Received: from frontend1 ([10.202.2.160]) by compute6.internal (MEProxy); Sun, 05 Mar 2017 14:56:04 -0500 DKIM-Signature: v=1; a=rsa-sha1; c=relaxed/relaxed; d=aklaver.com; h= content-transfer-encoding:content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to:x-me-sender :x-me-sender:x-sasl-enc:x-sasl-enc; s=mesmtp; bh=hkbMa1kbpVHaEu2 U3UFYhtQO8i8=; b=XWHE9rbheE1ezpYJiVt0KIPXcRjWvp5tTce2fLl+FuRl5Zp KUzGkrEMGtDi7lYZHxtn/P3FfArmcKQ3liXbFPuuHOdCYikIZ1qH5LAOh2ac7Eb+ kW1gi9VMcV025kCCyIpFNwpG3LuE73tGzioTXdCRopw3lRNho8WMMkY0Xwds= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed/relaxed; d= messagingengine.com; h=content-transfer-encoding:content-type :date:from:in-reply-to:message-id:mime-version:references :subject:to:x-me-sender:x-me-sender:x-sasl-enc:x-sasl-enc; s= smtpout; bh=hkbMa1kbpVHaEu2U3UFYhtQO8i8=; b=scRI/hJearKTmJPuEyB1 zsaN/6rSwocW4cM4llrsKTNH8A/nvIJ2BMcdkTzjrKdyZaydx/pWbyMAB1pXpu60 /UeQ3ADXqbNV2hLOrOhzTLXArNrX8q21zrl9ehW7YRXgrNsmMUbkhxbzm1qFiz/0 xyKu+FpsAxVU33RzgXxh2yQ= X-ME-Sender: X-Sasl-enc: 8mtjZ3cPGhGhlqIF/sJFYaXMJApvDJwv6PIIGmJvbqOH 1488743763 Received: from [192.168.1.2] (174-21-203-58.tukw.qwest.net [174.21.203.58]) by mail.messagingengine.com (Postfix) with ESMTPA id B8EAC7E077; Sun, 5 Mar 2017 14:56:03 -0500 (EST) Subject: Re: avoid update - insert churn To: Kevin Duffy , pgsql-sql@postgresql.org References: From: Adrian Klaver Message-ID: <4d61f745-7fba-d768-5bbf-5fdbf2b89569@aklaver.com> Date: Sun, 5 Mar 2017 11:56:03 -0800 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Thunderbird/45.7.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit X-Pg-Spam-Score: -2.7 (--) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-sql Precedence: bulk Sender: pgsql-sql-owner@postgresql.org On 03/05/2017 10:20 AM, Kevin Duffy wrote: > > Hello All: > > I am looking for suggestions on how to optimize a relatively simple problem. > > Say I have a group of shoe stores. The store report sales daily and > daily I > calc stats off on the sales numbers. Ie rolling averages & volatility > numbers, with a couple of different look backs. > > I am storing the raw sales figures in one table and the calc'ed stats in > another. > > Here is the interesting part, it is possible and it does happen that a > store will not > report sales for a day or two or prior figures reported will get revised. > There is at max a five day window on this. > > For the prior dates there are two possibilities, I will be updating > a record or inserting a new record in sales table. For the current > date's sales it will be all inserts. > Currently what I do is loop through the data by store and shoe style > attempt an update and if that fails > do an insert. > > Same deal for the stat's table except there are multiple records for > each Store/Shoe style pair. > > > So here is my question/issue: Is this there a better design pattern for > doing this? > I could gather up the data for the prior dates and do one "bulk" > update. But that would > fail for sales that have not been reported todate. > The current date's sales are of course an insert. And I could fix this > to be one "bulk" insert. > > Was just chewing this over in my heading wondering if there is a more > efficient way to this. > Other than attempt update if fail insert. Seems to be a bit of churn to me. You do not say what version of Postgres you are using, but if 9.5+ https://www.postgresql.org/docs/9.5/static/sql-insert.html "ON CONFLICT Clause The optional ON CONFLICT clause specifies an alternative action to raising a unique violation or exclusion constraint violation error. For each individual row proposed for insertion, either the insertion proceeds, or, if an arbiter constraint or index specified by conflict_target is violated, the alternative conflict_action is taken. ON CONFLICT DO NOTHING simply avoids inserting a row as its alternative action. ON CONFLICT DO UPDATE updates the existing row that conflicts with the row proposed for insertion as its alternative action." It wraps the INSERT/UPDATE as an UPSERT in one command. > > But at the end of the day, it do believe Keep It Simple. > > Many thanks for considering this problem. > > KD > > -- Adrian Klaver adrian.klaver@aklaver.com -- Sent via pgsql-sql mailing list (pgsql-sql@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-sql