Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1h8QF6-0000XB-2c for pgsql-general@arkaria.postgresql.org; Mon, 25 Mar 2019 14:06:24 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1h8QF4-0005Y8-I9 for pgsql-general@arkaria.postgresql.org; Mon, 25 Mar 2019 14:06:22 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1h8QF4-0005Qb-1d for pgsql-general@lists.postgresql.org; Mon, 25 Mar 2019 14:06:22 +0000 Received: from mail-ot1-x32b.google.com ([2607:f8b0:4864:20::32b]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1h8QF1-0002XA-IZ for pgsql-general@lists.postgresql.org; Mon, 25 Mar 2019 14:06:20 +0000 Received: by mail-ot1-x32b.google.com with SMTP id m10so511304otp.2 for ; Mon, 25 Mar 2019 07:06:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=subject:to:references:from:message-id:date:user-agent:mime-version :in-reply-to:content-transfer-encoding:content-language; bh=GQ0yaosBC18eHf745SHXzhwuimvEXNit9qjH55cFvqw=; b=rSuAXx7nAwvaeD7gPWb9u7vFgFAXk2zglJa4xwUH6z/gUM7qTKRlHVs1O5Lu7CMwwf a8rfsZ7iJQbYGSSyNpuFqDN/tcBDl/djQUmoBjVazvot5JBIkLtgyhOL+KoP37Uudk8D h+M4CUI5dUUI6AQVqJkld59Yy31M0GIdaxb8nkcInyp+Z9sugfyv0xXQqVKH6Q91C9q9 A0o31tcufIQEWVbcTh06vfZXlnYz08NCut5u9GAAgCFgQ49C1M+ieYZzla3tQu+fiaQY 2LuUSRD7ngJtNAn+FQ6yxR5iz1/R4L97DE6ySf6lWyCe/RUrehwiNox/JD7DkqLG0OqC CN3Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-transfer-encoding :content-language; bh=GQ0yaosBC18eHf745SHXzhwuimvEXNit9qjH55cFvqw=; b=tcYR5LklAlmTn+xd1VuRHrRvUVwpPA5uJvESmdWL6BqkcTvgZdgClulSoTSB/Jsf9H zWvVe5NL9vKpUUayk6uDhqluYzyFq/fzd+yRuWgBrxXDHPZ2Rho3v6gDTKMwjIScZ20J gP7rpbyvA+v968nOT/97LpSvHmzWuTdBD5JytujOgSbhrwP6i8siQReeZxxQI0u51fOj 84adTK4IVhq0cNssda3eaRljE+oTpUD4JDkGWYnv0ZYrxC86DipwVIlr6X9EdQcDH7VE A8yoYQsyzDsGu0Kxt81/ULNbDLQq70JPUAFaDXVdiSduIC4bIhMkmLqm3NBZJV2p7wzq ycUQ== X-Gm-Message-State: APjAAAX6RwhEN03UCS1SIMKjzNx31d8RdnDtaZqnQBrgM6w7BW8L5+VU ElUMtiUXS5QincfuTz0l0774uC2gxnQ= X-Google-Smtp-Source: APXvYqz4CMorzKVemUrA5V4zXyXnk8pQvi0aZEyuhgG+RyesdYxYNddlNp6XnXNH0Y9is2Y+LZR1/g== X-Received: by 2002:a9d:6393:: with SMTP id w19mr13567150otk.257.1553522777996; Mon, 25 Mar 2019 07:06:17 -0700 (PDT) Received: from [192.168.1.10] (ip70-171-116-89.no.no.cox.net. [70.171.116.89]) by smtp.googlemail.com with ESMTPSA id g12sm6494794otr.18.2019.03.25.07.06.16 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 25 Mar 2019 07:06:17 -0700 (PDT) Subject: Re: When to store data that could be derived To: pgsql-general@lists.postgresql.org References: <835132f4-e1c6-d362-1b13-ad48671afe39@gmail.com> <6dcf59be-2029-8554-88de-88587368b50d@chagford.com> <20190324124109.77rvmuzzaftf73fw@hjp.at> <3b9874b0-1e69-d273-9be5-b3cf65811b54@chagford.com> From: Ron Message-ID: <25f76700-bb5c-c3fb-6de3-53055fe4dffa@gmail.com> Date: Mon, 25 Mar 2019 09:06:16 -0500 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.4.0 MIME-Version: 1.0 In-Reply-To: <3b9874b0-1e69-d273-9be5-b3cf65811b54@chagford.com> Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit Content-Language: en-US List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 3/25/19 8:15 AM, Frank wrote: > On 2019-03-24 2:41 PM, Peter J. Holzer wrote: >> On 2019-03-24 10:05:02 +0200, Frank wrote: > > Many thanks to Peter et al for their valuable insights. I have learned a lot. > > > So the important part here is not whether data is added, but whether > > data is changed. Sure, new transactions are added all the time. But is > > it expected that the data used to derive amount_cust and amount_local > > (e.g. the exchange rate) is changed retroactively, and if it is should > > the computed amount change? (I'm a bit worried about the join with the > > customers table here - what happens when a customer moves their > > headquarters to a country with a different currency?) > > I think I have got both of those covered. I store the exchange rates in > physical columns on the transaction, so the compute expressions will > always return the same values. I have separate tables for 'organisations' > (O) and for 'customers' (C). C has a foreign key reference to O, and most > static data such as addresses and contact details are stored on O. So if a > customer moved, I would create a new C record with the new currency, and > flag the old C record as inactive. They can happily co-exist, so receipts > can be processed against the old C record until it is paid up. > > I have started doing some volume tests, and at this stage, for the kind of > volumes I am concerned about, it looks as if performance is a non-issue. > > I generated about 22000 invoices and 22000 receipts, over 12 customers and > 6 months. Invoices and receipts are stored in separate tables, and a VIEW > presents them as a single table. > > Using the VIEW, I selected all transactions for a given customer for a > given month. It returned 620 rows and (on my slow desktop computer) it > took 20ms. I can live with that. > > I will generate some higher volumes overnight, and see if it makes a big > difference. If you do not hear from me, you can consider it 'problem > solved' :-) It would be interesting to see what the query planner tries to do with this: WHERE     CASE         WHEN a.tran_type = 'ar_rec' THEN y.posted         WHEN a.tran_type = 'cb_rec' THEN w.posted     END = '1' -- Angular momentum makes the world go 'round.