pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Frank <frank@chagford.com>
To: pgsql-general@lists.postgresql.org
Subject: Re: When to store data that could be derived
Date: Mon, 25 Mar 2019 17:44:21 +0200
Message-ID: <f3428d77-e3b0-ab5f-e131-0c680cac7f10@chagford.com> (raw)
In-Reply-To: <86948961-6869-c5c3-ee40-0ffaaf2d46be@chagford.com>
References: <a2b19f30-9e1d-aea5-7b33-427047522461@chagford.com>
	<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>


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




view thread (15+ messages)  latest in thread

Message-ID: <f3428d77-e3b0-ab5f-e131-0c680cac7f10@chagford.com>
Permalink:  ../f3428d77-e3b0-ab5f-e131-0c680cac7f10@chagford.com/
Also on:    postgresql.org/message-id/f3428d77-e3b0-ab5f-e131-0c680cac7f10@chagford.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-general@postgresql.org
  Cc: frank@chagford.com
  Subject: Re: When to store data that could be derived
  In-Reply-To: <f3428d77-e3b0-ab5f-e131-0c680cac7f10@chagford.com>
  References: <a2b19f30-9e1d-aea5-7b33-427047522461@chagford.com> <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> <f3428d77-e3b0-ab5f-e131-0c680cac7f10@chagford.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox