pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Adrian Klaver <adrian.klaver@aklaver.com>
To: Nicklas Avén <nicklas.aven@jordogskog.no>
To: pgsql-general@lists.postgresql.org
Subject: Re: Problems pushing down WHERE-clause to underlying view
Date: Fri, 15 Feb 2019 16:08:59 -0800
Message-ID: <9f19bc9e-eec2-04db-67c1-0d449fd5f80c@aklaver.com> (raw)
In-Reply-To: <232283a6-4422-1efd-ec80-3dda8b2f5496@jordogskog.no>
References: <4d8a05b6-39de-9a06-7e86-85754fd94716@jordogskog.no>
	<9cd9e5c1-1909-6f36-1473-f3d2a773762e@aklaver.com>
	<9770a1ec-c7be-7527-66d7-0c95b8277508@jordogskog.no>
	<473540ab-5933-04e8-ce55-074f4faaab82@aklaver.com>
	<232283a6-4422-1efd-ec80-3dda8b2f5496@jordogskog.no>

On 2/15/19 12:43 PM, Nicklas Avén wrote:
> 
>  > I have not had chance to fully go through all of below. Some 
> questions/suggestions:
>  >
>  > 1) Thanks for the formatted queries. If I could make a suggestion, 
> when aliasing could you include AS. It would make finding what l.* 
> refers to easier for those of us with old eyes:)
>  >
> Yes, of course, sorry :-)
> 
> 
>  > 2) t4e_contractor_id is in the shiny_adm.contractor_access table?
>  > If not where?
> 
> Yes, sorry again, it is there
> 
> 
>  >
>  > 3) What is the schema for shiny_adm.contractor_access?
>  > In particular what indexes are on it?
>  >
> shiny_adm.contractor_access looks like this:
> 
> 
> CREATE TABLE shiny_adm.contractor_access
> (
>    machine_key text,
>    t4e_contractor_id text,
>    active integer DEFAULT 1,
>    id serial NOT NULL,
>    CONSTRAINT contractor_access_pkey PRIMARY KEY (id),
>    CONSTRAINT contractor_unique UNIQUE (machine_key, t4e_contractor_id),
>    CONSTRAINT co_check_t4e_co_email CHECK 
> (utils.verify_email(t4e_contractor_id))
> )
> 
> 
> CREATE INDEX idx_contractor
>    ON shiny_adm.contractor_access
>    USING btree
>    (t4e_contractor_id COLLATE pg_catalog."default");
> 
> CREATE INDEX idx_contractor_mk
>    ON shiny_adm.contractor_access
>    USING btree
>    (machine_key COLLATE pg_catalog."default");
> 
> 
> I tried to format the below a little better with AS and some more 
> consistent indents.
> 
> 
> I also, in the first query, changed the where clause to filter on 
> machine_key in table contractor _access. Just to illustrate the problem 
> better.
> 
> Both queries filter on the same table which is joined the same way. But 
> in the second example the where clause is not pushed to the subquery l
> 
> 
> Thanks a lot for looking into it
> 
> 
> Nicklas
> 

> 
> Next query, the slow one that calculates the whole dataset:
> 
> 
> 
> EXPLAIN ANALYZE
> SELECT
>      l.machine_key,
>      o.object_name,
>      o.sub_object_name,
>      o.object_user_id,
>      o.sub_object_user_id,
>      o.start_date AS object_start_date,
>      s.species_group_name,
>      p.product_group_name,
>      l.m3_sub AS volume_m3sub,
>      l.number_of_logs,
>      mi.basemachine_manufacturer,
>      mi.basemachine_model
> FROM  shiny_adm.contractor_access ci join
>      (
>          SELECT
>              hl.contractor_id,
>              hl.machine_key,
>              hl.operator_key,
>              hl.object_key,
>              hl.sub_object_key,
>              date(hl.harvest_date) AS harvest_date,
>              hl.species_group_key,
>              hl.product_key,
>              sum(hl.m3_sub) AS m3_sub,
>              count(*) AS number_of_logs
>          FROM
>              version_union_tables_r02.harvester_logs AS hl
>          GROUP BY
>              hl.machine_key, hl.contractor_id, hl.operator_key, 
> hl.object_key, hl.sub_object_key, (date(hl.harvest_date)), 
> hl.species_group_key, hl.product_key
>      )  AS l on l.machine_key=ci.machine_key
>      LEFT JOIN version_union_tables_r02.machine_info  AS mi ON 
> l.machine_key::text = mi.machine_key::text
>      LEFT JOIN version_union_tables_r02.objects AS o ON 
> l.machine_key::text = o.machine_key::text AND l.object_key = 
> o.object_key AND l.sub_object_key = o.sub_object_key
>      LEFT JOIN version_union_tables_r02.products  AS p ON 
> l.machine_key::text = p.machine_key::text AND l.product_key = p.product_key
>      LEFT JOIN version_union_tables_r02.species  AS s ON 
> l.machine_key::text = s.machine_key::text AND l.species_group_key = 
> s.species_group_key
> WHERE t4e_contractor_id = 'nicklas.aven@jordogskog.no';

To make it apples to apples try changing above to be more like first query:

...

  AS l
     LEFT JOIN version_union_tables_r02.machine_info  AS mi ON 
l.machine_key::text = mi.machine_key::text
     LEFT JOIN version_union_tables_r02.objects AS  o ON 
l.machine_key::text = o.machine_key::text AND l.object_key = 
o.object_key AND l.sub_object_key = o.sub_object_key
     LEFT JOIN version_union_tables_r02.products AS  p ON 
l.machine_key::text = p.machine_key::text AND l.product_key = p.product_key
     LEFT JOIN version_union_tables_r02.species  AS s ON 
l.machine_key::text = s.machine_key::text AND l.species_group_key = 
s.species_group_key
     JOIN shiny_adm.contractor_access AS ci ON l.machine_key=ci.machine_key
  WHERE t4e_contractor_id = 'nicklas.aven@jordogskog.no'

;
> 
> results in this query plan:



-- 
Adrian Klaver
adrian.klaver@aklaver.com




view thread (8+ messages)  latest in thread

Message-ID: <9f19bc9e-eec2-04db-67c1-0d449fd5f80c@aklaver.com>
Permalink:  ../9f19bc9e-eec2-04db-67c1-0d449fd5f80c@aklaver.com/
Also on:    postgresql.org/message-id/9f19bc9e-eec2-04db-67c1-0d449fd5f80c@aklaver.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: adrian.klaver@aklaver.com, nicklas.aven@jordogskog.no
  Subject: Re: Problems pushing down WHERE-clause to underlying view
  In-Reply-To: <9f19bc9e-eec2-04db-67c1-0d449fd5f80c@aklaver.com>
  References: <4d8a05b6-39de-9a06-7e86-85754fd94716@jordogskog.no> <9cd9e5c1-1909-6f36-1473-f3d2a773762e@aklaver.com> <9770a1ec-c7be-7527-66d7-0c95b8277508@jordogskog.no> <473540ab-5933-04e8-ce55-074f4faaab82@aklaver.com> <232283a6-4422-1efd-ec80-3dda8b2f5496@jordogskog.no> <9f19bc9e-eec2-04db-67c1-0d449fd5f80c@aklaver.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