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 1h82RD-0001sl-MA for pgsql-general@arkaria.postgresql.org; Sun, 24 Mar 2019 12:41:19 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1h82RA-0001Lj-Ua for pgsql-general@arkaria.postgresql.org; Sun, 24 Mar 2019 12:41:16 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1h82RA-0001Lc-M3 for pgsql-general@lists.postgresql.org; Sun, 24 Mar 2019 12:41:16 +0000 Received: from mail.hjp.at ([212.17.106.138] helo=rorschach.hjp.at) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1h82R5-0006IF-Us for pgsql-general@lists.postgresql.org; Sun, 24 Mar 2019 12:41:15 +0000 Received: by rorschach.hjp.at (Postfix, from userid 1000) id 72611269B; Sun, 24 Mar 2019 13:41:09 +0100 (CET) Date: Sun, 24 Mar 2019 13:41:09 +0100 From: "Peter J. Holzer" To: pgsql-general@lists.postgresql.org Subject: Re: When to store data that could be derived Message-ID: <20190324124109.77rvmuzzaftf73fw@hjp.at> References: <835132f4-e1c6-d362-1b13-ad48671afe39@gmail.com> <6dcf59be-2029-8554-88de-88587368b50d@chagford.com> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha256; protocol="application/pgp-signature"; boundary="nwensgw2vhlntl5m" Content-Disposition: inline In-Reply-To: <6dcf59be-2029-8554-88de-88587368b50d@chagford.com> User-Agent: NeoMutt/20170113 (1.7.2) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk --nwensgw2vhlntl5m Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On 2019-03-24 10:05:02 +0200, Frank wrote: >=20 >=20 > On 2019-03-24 9:25 AM, Ron wrote: > > On 3/24/19 1:42 AM, Frank wrote: > > > As I understand it, a=A0 general rule of thumb is that you should > > > never create a physical column if the data could be derived from > > > existing columns. The main reason for this rule (which leads to the 3rd normal form) is to avoid inconsistencies when data is changed. > > Is this a historical data set that's never updated, or current data > > that's constantly added to? > >=20 >=20 > It is the latter - current data constantly added to. 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?) > > Sure the second query joins a lot of tables, but is pretty straightforw= ard. > >=20 > > What REALLY worries me is whether or not the query optimiser would look > > at the WHERE CASE, run away screaming and then make it use sequential > > scans. Thus, even query #1 would be slow. > >=20 >=20 > I had not realised that. I hope someone else chimes in on this. Your condition is: CASE WHEN a.tran_type =3D 'ar_rec' THEN y.posted WHEN a.tran_type =3D 'cb_rec' THEN w.posted END =3D '1' Think about how you would check that. The straightforward way is to compute the value of the case/end clause and compare that to 1. But to compute that value you first need the value of a.tran_type. There are two possible values here, so maybe an index scan on a.tran_type might be possible, but I'm not sure whether the optimizer is even smart enought to figure that out and if it is, whether those to values are selective enough (Maybe all or most records are either ar_rec or cb_rec). After that you can retrieve the posted value from the correct table. As a human I see that the condition can only ever be true for records =66rom y and w with posted =3D 1. So it might be better to use index scans on those columns. But this is not that easy to see, and I don't know whether the optimizer can do it. Rewriting the condition as (a.tran_type =3D 'ar_rec' and y.posted =3D 1) or (a.tran_type =3D 'cb_rec' and w.posted =3D 1) might make it easier for the optimizer to find a good plan. (Please note that I haven't tested any of this. Use EXPLAIN to check what the optimizer really does. hp --=20 _ | Peter J. Holzer | we build much bigger, better disasters now |_|_) | | because we have much more sophisticated | | | hjp@hjp.at | management tools. __/ | http://www.hjp.at/ | -- Ross Anderson --nwensgw2vhlntl5m Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQIzBAABCAAdFiEETtJbRjyPwVTYGJ5k8g5IURL+KF0FAlyXetoACgkQ8g5IURL+ KF2JxBAAgxnNUVxfn3th8/aASqZtZgZmxnPrhlPFi5dht9b9RNulRGz/M4L3RUAi yGg9c3+D/MDLhcaw/IyU9K8IpgDqpdpstnqiFKR8AvtXm8Ygc5SlagncP4RUoQK5 1NlQ5xL0GG92RUyCCFRY31QP/4SbjGLML1dE7vnNaRcYYf+8zrJkOArNaEdEDO/Z M70niBVVZtummb/3lXDR2+ePmPEwLJBgaQp/RmnoQTibsXodDcKO2Ju+q/oV93xe kqjAC+U9tDtrmVAxxLl1ELDXb78d0z9CjZplnJjvNMAjlJpuYE02lzMoFKyNO8E/ zYz5Wdl/jLOcwjDcdgBlnX8W8+Yfq4VsnLttu3rAxQPsZKF/Gytmje06aYhy6vT4 6hIAXmSING9xdXUOfsnxuOelchj5LG4rFLCZAMrvSUB20lQxzSzQqTkzKcgdkeg0 mP/0051QunCivDUw6X2dQ81YFG3Z/CPHv3bVREfJNDx9tnZ9mDIzUhEc1cfgD5cO zmSFKGqpnlSgWAGTZTzH7x+MF8AjM3iiPOoSMD2RMnBhm/uX982zF4aCg9mDHLEP nT41nGvPyI7MLHyTerIwGpcJwXYSDJt8wyKyrrdIEywpU0ImOVJWNliJAhaGnM7y F34RvrN1mUEx/cQCCKjorZo9PxNpoiLOj/kjfxpEc5z7jdlHkX4= =4v/q -----END PGP SIGNATURE----- --nwensgw2vhlntl5m--