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 1h8RmC-0005jM-Sn for pgsql-general@arkaria.postgresql.org; Mon, 25 Mar 2019 15:44:41 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1h8RmB-000666-1w for pgsql-general@arkaria.postgresql.org; Mon, 25 Mar 2019 15:44:39 +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 1h8RmA-00065w-PX for pgsql-general@lists.postgresql.org; Mon, 25 Mar 2019 15:44:38 +0000 Received: from securemail-pl-omx3.synaq.com ([196.35.198.23]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1h8Rm8-0000qE-7y for pgsql-general@lists.postgresql.org; Mon, 25 Mar 2019 15:44:38 +0000 Received: from [197.90.32.26] (helo=[192.168.0.8]) by securemail-pl-omx3.synaq.com with esmtpa (Exim 4.90_1) (envelope-from ) id 1h8Rm0-0006VD-7x for pgsql-general@lists.postgresql.org; Mon, 25 Mar 2019 17:44:28 +0200 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> <25f76700-bb5c-c3fb-6de3-53055fe4dffa@gmail.com> <86948961-6869-c5c3-ee40-0ffaaf2d46be@chagford.com> From: Frank Message-ID: Date: Mon, 25 Mar 2019 17:44:21 +0200 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:60.0) Gecko/20100101 Thunderbird/60.6.0 MIME-Version: 1.0 In-Reply-To: <86948961-6869-c5c3-ee40-0ffaaf2d46be@chagford.com> Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 8bit X-SYNAQ-Pinpoint-Information: Please contact SYNAQ for more information X-SYNAQ-Pinpoint-ID: 1h8Rm0-0006VD-7x X-SYNAQ-Pinpoint: Found to be clean X-SYNAQ-Pinpoint-SpamCheck: not spam, SpamAssassin (not cached, score=-1.8, required 9, autolearn=disabled, ALL_TRUSTED -1.00, DCC_REPUT_00_12 -0.80) X-Pinpoint-From: frank@chagford.com List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 2019-03-25 5:11 PM, Frank wrote: > > > On 2019-03-25 4:06 PM, Ron wrote: >> On 3/25/19 8:15 AM, Frank wrote: >> >> 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' >> > > I have attached the schema showing the full VIEW definition, and the > result of the following EXPLAIN - > > EXPLAIN SELECT * FROM ccc.ar_trans WHERE cust_row_id = 4 AND tran_date > BETWEEN '2015-06-01' AND '2015-06-30'. > > Because I have used 'WHERE tran_date' in the query, and tran_date is > also derived from a CASE expression, I imagine that will also add some > complication. > > I am running PostgreSQL 11.1 on Fedora 29. > > Frank On reflection, I have not been consistent with my use of indexes, and I think that will affect the query plan. There are at least two issues - 1. The first table defined in the VIEW is ccc.ar_tran_inv. It has the following index - "arinv_cust_date" btree (cust_row_id NULLS FIRST, tran_date NULLS FIRST) WHERE deleted_id = 0 I have not used "WHERE deleted_id = 0" when constructing the VIEW, but I have used "WHERE posted = '1'". I don't think the index can be used with this setup. 2. The complicated table in the VIEW is ccc.ar_rec_subtran. Various columns such as tran_date and posted are retrieved via CASE expressions from two underlying tables. Those tables have certain indexes defined, but I cannot see how they can be utilised from my current setup. I think I should spend some time tidying this up before you try to make sense of the query plan. Any tips on how to improve it will be appreciated. Frank