Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1bZ2LB-0006FR-6c for pgsql-hackers@arkaria.postgresql.org; Sun, 14 Aug 2016 20:49:06 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84_2) (envelope-from ) id 1bZ2LA-0001n6-HN for pgsql-hackers@arkaria.postgresql.org; Sun, 14 Aug 2016 20:49:04 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1bZ2JW-0000Ca-9Z for pgsql-hackers@postgresql.org; Sun, 14 Aug 2016 20:47:22 +0000 Received: from post.visena.com ([46.226.10.50]) by makus.postgresql.org with esmtps (TLS1.2:DHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.84_2) (envelope-from ) id 1bZ2JN-00059u-LH for pgsql-hackers@postgresql.org; Sun, 14 Aug 2016 20:47:21 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=visena.com; s=20141101.wh; h=Content-Type:MIME-Version:Subject:In-Reply-To:Message-ID:To:From:Date; bh=bkUANGpVBCa+bC5VvPIAGtEHL/anofH4C4y1HgEIf+Y=; b=pegP3zwky6hooYV3YP7vTfDXmZU7XG80x3vUqC+TyhNoOiZfs2Z6sXdr/52m7ky5UBJEqMk9A0F974hypHWALpk4Ow22P7HYgRu24wU3GJdksUwqj2OaFvhV2Y9YRUZS0ySd+c0kfLxZ5eI5n/myJIxaJx8H3jSdZc8YTGJQyNg=; Received: from [10.0.1.10] (helo=tc7-visena.wh.internal.visena.com) by post.visena.com with esmtp (Exim 4.82) (envelope-from ) id 1bZ2JH-0001Sx-A7 for pgsql-hackers@postgresql.org; Sun, 14 Aug 2016 22:47:10 +0200 Received: from localhost ([127.0.0.1] helo=tc7-visena.wh.internal.visena.com) by tc7-visena.wh.internal.visena.com with esmtp (Exim 4.82) (envelope-from ) id 1bZ2JH-0008Li-32 for pgsql-hackers@postgresql.org; Sun, 14 Aug 2016 22:47:07 +0200 Date: Sun, 14 Aug 2016 22:47:04 +0200 (CEST) From: Andreas Joseph Krogh To: pgsql-hackers@postgresql.org Message-ID: In-Reply-To: <24135.1470587756@sss.pgh.pa.us> Subject: Re: Bogus ANALYZE results for an otherwise-unique column with many nulls MIME-Version: 1.0 X-Mailer: Visena Mail 2.0.3-SNAPSHOT X-Spam-Score: -1.0 X-Spam-Report: SpamAssasin (score=-1.0, required 5.0 ALL_TRUSTED=-1,HTML_MESSAGE=0.001) X-Pg-Spam-Score: -2.5 (--) Content-Type: multipart/related; boundary="----=_Part_1_869519999.1471207625606" List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-hackers Precedence: bulk Sender: pgsql-hackers-owner@postgresql.org ------=_Part_0_1631323581.1471207625485 Content-Type: multipart/related; boundary="----=_Part_1_869519999.1471207625606" ------=_Part_1_869519999.1471207625606 Content-Type: multipart/alternative; boundary="----=_Part_2_677827721.1471207626079" ------=_Part_2_677827721.1471207626079 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable P=C3=A5 s=C3=B8ndag 07. august 2016 kl. 18:35:56, skrev Tom Lane >: Dean Rasheed writes: > On 5 August 2016 at 21:48, Tom Lane wrote: >> OK, thanks.=C2=A0 What shall we do about Andreas' request to back-patch= this? >> I'm personally willing to do it, but there is the old bugaboo of "maybe >> it will destabilize a plan that someone is happy with". > My inclination would be to back-patch it because arguably it's a > bug-fix -- at the very least the old behaviour didn't match the docs > for stadistinct: Yeah.=C2=A0 I suspect that situations like this are pretty rare, or we'd h= ave recognized the problem sooner.=C2=A0 And at least for Andreas, it'd be fix= ing a real problem.=C2=A0 I'll apply the back-patch unless I hear objections i= n the next couple of hours. > Additionally, I think that example is misleading because it's only > really true if there are no null values in the column. Perhaps it > would help to have a more explicit example to illustrate how nulls > affect stadistinct, for example: Good idea, will do. regards, tom lane =C2=A0 =C2=A0 (Sorry for the HTML-formatting, I'm using a web-based client which isn't ve= ry=20 good at converting the HTML to plain-text) =C2=A0 Just wanted to thank you for fixing this. =C2=A0 Take this query (pg-9.5): =C2=A0 explain analyze SELECT com.entity_id AS company_id , sum((coalesce(log .adjusted_time,log.duration) / (3600 * 1000)::numeric) * coalesce(log .adjusted_hourly_rate,log.hourly_rate)) as not_invoiced_hours_amount_adjust= ed ,=20 sum((log.duration / (3600 * 1000)::numeric) * log.hourly_rate) as=20 not_invoiced_hours_amount_original ,sum(coalesce(log.adjusted_time, log .duration))AS not_invoiced_hours_duration_adjusted , sum(log.duration) AS= =20 not_invoiced_hours_duration_originalFROM onp_crm_relation com JOIN=20 onp_crm_activity_loglog ON com.entity_id =3D log.relation_id JOIN onp_crm_p= erson=20 logforON logfor.onp_user_id =3D log.logged_for AND logfor.is_resource =3D F= ALSE =20 LEFT OUTER JOIN( onp_crm_activity act INNER JOIN onp_crm_project proj ON=20 act.project_id =3D proj.id )ON log.activity_id =3D act.id WHERE 1 =3D 1 AND= log .entry_type =3D'TIMESHEET_ENTRY' AND log.start_date IS NOT NULL AND log .is_chargable =3DTRUE AND log.status IN (1,2,3,5) AND log.start_date <=3D= =20 '2016-06-27' -- AND com.entity_id =3D 2246068 AND NOT EXISTS( SELECT * FROM= =20 onp_crm_calendarentry_invoice_membership cemJOIN onp_crm_invoice_line il ON= =20 cem.invoice_line_id =3D il.idJOIN onp_crm_invoice inv ON il.invoice_id =3D= =20 inv.entity_idWHERE cem.calendar_entry_id =3D log.id AND inv.status_key =3D= =20 'INVOICE_STATUS_INVOICED' AND inv.sent_date <=3D '2016-06-27' AND NOT EXIST= S(=20 SELECT* FROM onp_crm_invoice creditnote WHERE il.invoice_id =3D=20 creditnote.credit_againstAND creditnote.status_key =3D 'INVOICE_STATUS_INVO= ICED'=20 ANDcreditnote.sent_date <=3D '2016-06-27' ) ) GROUP BY com.entity_id;=20 =C2=A0 Before the fix, the plan was like this: =C2=A0 QUERY PLAN=20 ---------------------------------------------------------------------------= ---------------------------------------------------------------------------= ---------------------------------------------------------------------------= -------------------------- HashAggregate (cost=3D13874.94..13894.38 rows=3D972 width=3D32) (actual tim= e =3D420188.465..420189.232rows=3D462 loops=3D1) Group Key: com.entity_id -> = Nested=20 Loop Left Join(cost=3D1710.21..13848.21 rows=3D972 width=3D32) (actual time =3D1004.858..420166.736rows=3D3882 loops=3D1) -> Nested Loop Anti Join (cos= t =3D1709.51..12990.02rows=3D972 width=3D36) (actual time=3D1003.001..419356.= 749 rows=3D3882 loops=3D1) Join Filter: (cem.calendar_entry_id =3D log.id) Rows Removed by = Join=20 Filter: 3996874113 -> Hash Join (cost=3D1172.69..10612.53 rows=3D972 width= =3D44)=20 (actualtime=3D29.085..333.323 rows=3D89431 loops=3D1) Hash Cond: (log.relat= ion_id =3D=20 com.entity_id) ->Hash Join (cost=3D1063.39..10489.85 rows=3D976 width=3D40)= (actual=20 time=3D27.598..261.822 rows=3D89431 loops=3D1) Hash Cond: (log.logged_for = =3D=20 logfor.onp_user_id) -> Bitmap Heap Scanon onp_crm_activity_log log (cost =3D976.83..10055.99rows=3D90010 width=3D44) (actual time=3D27.402..223.407 = rows=3D89486=20 loops=3D1) Recheck Cond: ((status =3D ANY ('{1,2,3,5}'::integer[])) AND is_= chargable AND(start_date IS NOT NULL) AND ((entry_type)::text =3D 'TIMESHEET_ENTRY'::= text))=20 Filter: (start_date <=3D '2016-06-27'::date) Rows Removed by Filter: 991 He= ap=20 Blocks: exact=3D6095 -> Bitmap Index Scan on origo_calendar_entry_status_1_= idx ( cost=3D0.00..954.33 rows=3D93008 width=3D0) (actual time=3D26.339..26.339 r= ows=3D101274=20 loops=3D1) Index Cond: (status =3D ANY ('{1,2,3,5}'::integer[])) -> Hash (c= ost =3D39.46..39.46rows=3D3768 width=3D8) (actual time=3D0.148..0.148 rows=3D36= loops=3D1)=20 Buckets:4096 Batches: 1 Memory Usage: 34kB -> Bitmap Heap Scan on=20 onp_crm_person logfor (cost=3D3.69..39.46 rows=3D3768 width=3D8) (actual ti= me =3D0.049..0.137rows=3D36 loops=3D1) Recheck Cond: (onp_user_id IS NOT NULL)= Filter: ( NOTis_resource) Rows Removed by Filter: 5 Heap Blocks: exact=3D11 -> Bitmap= Index=20 Scanon onp_crm_person_onp_id_idx (cost=3D0.00..2.75 rows=3D41 width=3D0) (a= ctual time =3D0.021..0.021rows=3D41 loops=3D1) -> Hash (cost=3D80.66..80.66 rows=3D229= 1 width=3D8)=20 (actualtime=3D1.449..1.449 rows=3D2292 loops=3D1) Buckets: 4096 Batches: 1 = Memory=20 Usage: 122kB -> Index Only Scan using onp_crm_relation_pkey on onp_crm_rela= tion=20 com (cost=3D0.28..80.66 rows=3D2291 width=3D8) (actual time=3D0.221..1.036 = rows=3D2292=20 loops=3D1) Heap Fetches: 1127 -> Materialize (cost=3D536.82..2362.91 rows= =3D1 width=3D4 ) (actualtime=3D0.000..1.564 rows=3D44693 loops=3D89431) -> Nested Loop (co= st =3D536.82..2362.91rows=3D1 width=3D4) (actual time=3D10.480..382.816 rows= =3D85668 loops=3D1 ) -> NestedLoop (cost=3D536.53..2362.59 rows=3D1 width=3D8) (actual time =3D10.437..176.495rows=3D88731 loops=3D1) -> Hash Anti Join (cost=3D536.24.= .2362.05 rows =3D1 width=3D8) (actual time=3D9.914..44.329 rows=3D76347 loops=3D1) Hash C= ond:=20 (il.invoice_id =3D creditnote.credit_against) ->Index Only Scan using=20 origo_invoice_line_id_invoice_idxon onp_crm_invoice_line il (cost=3D0.29..1= 530.95=20 rows=3D78707 width=3D8) (actual time=3D0.213..20.450 rows=3D78787 loops=3D1= ) Heap Fetches: 3131 -> Hash (cost=3D390.36..390.36 rows=3D11647 width=3D8) (actual time=3D= 9.589..9.589=20 rows=3D372 loops=3D1) Buckets: 16384 Batches: 1 Memory Usage: 143kB -> Inde= x Only=20 Scanusing origo_invoice_credit_against_status_sent_idx on onp_crm_invoice= =20 creditnote (cost=3D0.29..390.36 rows=3D11647 width=3D8) (actual time=3D0.20= 5..8.953 rows =3D11507 loops=3D1) Index Cond: ((status_key =3D 'INVOICE_STATUS_INVOICED':= :text) AND=20 (sent_date <=3D'2016-06-27'::date)) Heap Fetches: 999 -> Index Only Scan us= ing=20 onp_crm_calendarentry_invoice_invoice_line_id_calendar_entr_keyon=20 onp_crm_calendarentry_invoice_membership cem (cost=3D0.29..0.45 rows=3D9 wi= dth=3D8)=20 (actualtime=3D0.001..0.001 rows=3D1 loops=3D76347) Index Cond: (invoice_lin= e_id =3D=20 il.id) Heap Fetches:4951 -> Index Only Scan using=20 origo_invoice_id_status_sent_idxon onp_crm_invoice inv (cost=3D0.29..0.31 r= ows=3D1=20 width=3D8) (actual time=3D0.002..0.002 rows=3D1 loops=3D88731) Index Cond: = ((entity_id=20 =3D il.invoice_id)AND (status_key =3D 'INVOICE_STATUS_INVOICED'::text) AND= =20 (sent_date <=3D'2016-06-27'::date)) Heap Fetches: 9293 -> Nested Loop (cost =3D0.71..0.87rows=3D1 width=3D4) (actual time=3D0.207..0.207 rows=3D1 loops= =3D3882) -> Index Scanusing onp_crm_activity_pkey on onp_crm_activity act (cost=3D0.42..0.56 = rows=3D1=20 width=3D8) (actual time=3D0.199..0.199 rows=3D1 loops=3D3882) Index Cond: (= log .activity_id =3D id) ->Index Only Scan using onp_crm_project_pkey on=20 onp_crm_project proj (cost=3D0.29..0.30 rows=3D1 width=3D4) (actual time=3D= 0.006..0.006=20 rows=3D1 loops=3D3882) Index Cond: (id =3D act.project_id) Heap Fetches: 27= 82 Planning time: 33.132 ms Execution time: 420195.041 ms (55 rows)=20 =C2=A0 After the fix (using pg-9.5.4), the plan is this: =C2=A0 QUERY PLAN=20 ---------------------------------------------------------------------------= ---------------------------------------------------------------------------= ---------------------------------------------------------------------------= ---------------------------------------------------------- HashAggregate (cost=3D44223.20..44269.02 rows=3D2291 width=3D32) (actual ti= me =3D604.480..604.729rows=3D462 loops=3D1) Group Key: com.entity_id -> Hash L= eft Join ( cost=3D40805.80..42869.54 rows=3D49224 width=3D32) (actual time=3D591.912..= 597.284 rows=3D 3882loops=3D1) Hash Cond: (log.activity_id =3D act.id) -> Hash Join (cost =3D24077.93..25498.04rows=3D49224 width=3D36) (actual time=3D309.816..313.5= 42 rows=3D3882=20 loops=3D1) Hash Cond: (log.relation_id =3D com.entity_id) -> Merge Join (co= st =3D23968.63..24711.11rows=3D49435 width=3D32) (actual time=3D304.949..307.4= 74 rows=3D3882=20 loops=3D1) Merge Cond: (logfor.onp_user_id =3D log.logged_for) -> Index Sca= n using=20 onp_crm_person_onp_id_idxon onp_crm_person logfor (cost=3D0.14..41.05 rows= =3D3768=20 width=3D8) (actual time=3D0.194..1.321 rows=3D36 loops=3D1) Filter: (NOT is= _resource)=20 RowsRemoved by Filter: 5 -> Sort (cost=3D23968.49..24092.29 rows=3D49520 wi= dth=3D36)=20 (actualtime=3D304.579..305.137 rows=3D3893 loops=3D1) Sort Key: log.logged_= for Sort=20 Method: quicksort Memory:401kB -> Hash Anti Join (cost=3D9997.74..20106.99 = rows=3D 49520width=3D36) (actual time=3D147.213..302.609 rows=3D3893 loops=3D1) Has= h Cond: (log .id =3D cem.calendar_entry_id) -> Bitmap Heap Scanon onp_crm_activity_log l= og ( cost=3D976.83..10055.99 rows=3D90010 width=3D44) (actual time=3D28.286..158= .599 rows=3D 89486loops=3D1) Recheck Cond: ((status =3D ANY ('{1,2,3,5}'::integer[])) AN= D=20 is_chargableAND (start_date IS NOT NULL) AND ((entry_type)::text =3D=20 'TIMESHEET_ENTRY'::text)) Filter: (start_date <=3D '2016-06-27'::date) Rows= =20 Removedby Filter: 991 Heap Blocks: exact=3D6095 -> Bitmap Index Scan on=20 origo_calendar_entry_status_1_idx (cost=3D0.00..954.33 rows=3D93008 width= =3D0) (actual time=3D27.251..27.251 rows=3D101274 loops=3D1) Index Cond: (status =3D ANY = ('{1,2,3,5}' ::integer[])) -> Hash (cost=3D7964.73..7964.73 rows=3D84494 width=3D4) (act= ual time =3D118.106..118.106rows=3D85668 loops=3D1) Buckets: 131072 Batches: 1 Memor= y Usage:=20 4036kB -> Hash Join (cost=3D1069.29..7964.73 rows=3D84494 width=3D4) (actua= l time =3D15.605..102.721rows=3D85668 loops=3D1) Hash Cond: (il.invoice_id =3D inv= .entity_id)=20 ->Hash Anti Join (cost=3D536.55..6265.54 rows=3D85741 width=3D8) (actual ti= me =3D5.467..71.124rows=3D88731 loops=3D1) Hash Cond: (il.invoice_id =3D=20 creditnote.credit_against) -> MergeJoin (cost=3D0.61..4501.71 rows=3D89266 = width=3D8)=20 (actualtime=3D0.441..53.014 rows=3D89301 loops=3D1) Merge Cond: (il.id =3D= =20 cem.invoice_line_id) ->Index Only Scan using origo_invoice_line_id_invoice_= idx=20 ononp_crm_invoice_line il (cost=3D0.29..1530.95 rows=3D78707 width=3D8) (ac= tual time =3D0.226..15.901rows=3D78787 loops=3D1) Heap Fetches: 3131 -> Index Only Sc= an using=20 onp_crm_calendarentry_invoice_invoice_line_id_calendar_entr_keyon=20 onp_crm_calendarentry_invoice_membership cem (cost=3D0.29..1662.98 rows=3D8= 9266=20 width=3D8) (actual time=3D0.194..16.489 rows=3D89301 loops=3D1) Heap Fetche= s: 5027 ->=20 Hash(cost=3D390.36..390.36 rows=3D11647 width=3D8) (actual time=3D4.785..4.= 785 rows=3D372=20 loops=3D1) Buckets: 16384 Batches: 1 Memory Usage: 143kB -> Index Only Scan= using=20 origo_invoice_credit_against_status_sent_idxon onp_crm_invoice creditnote (= cost =3D0.29..390.36rows=3D11647 width=3D8) (actual time=3D0.020..4.213 rows=3D1= 1507 loops=3D1)=20 IndexCond: ((status_key =3D 'INVOICE_STATUS_INVOICED'::text) AND (sent_date= <=3D=20 '2016-06-27'::date)) Heap Fetches: 999 -> Hash (cost=3D387.15..387.15 rows= =3D11647=20 width=3D8) (actual time=3D10.063..10.063 rows=3D11507 loops=3D1) Buckets: 1= 6384 Batches: 1Memory Usage: 578kB -> Index Only Scan using origo_invoice_id_status_sent_= idx=20 ononp_crm_invoice inv (cost=3D0.29..387.15 rows=3D11647 width=3D8) (actual = time =3D0.233..8.133rows=3D11507 loops=3D1) Index Cond: ((status_key =3D=20 'INVOICE_STATUS_INVOICED'::text) AND (sent_date <=3D '2016-06-27'::date)) H= eap=20 Fetches:999 -> Hash (cost=3D80.66..80.66 rows=3D2291 width=3D8) (actual tim= e =3D4.831..4.831rows=3D2292 loops=3D1) Buckets: 4096 Batches: 1 Memory Usage= : 122kB ->=20 Index OnlyScan using onp_crm_relation_pkey on onp_crm_relation com (cost =3D0.28..80.66rows=3D2291 width=3D8) (actual time=3D0.235..4.370 rows=3D229= 2 loops=3D1)=20 Heap Fetches:1127 -> Hash (cost=3D14012.31..14012.31 rows=3D217245 width=3D= 4) (actual=20 time=3D281.015..281.015 rows=3D220147 loops=3D1) Buckets: 262144 Batches: 1= Memory=20 Usage: 9788kB -> Hash Join (cost=3D433.74..14012.31 rows=3D217245 width=3D4= ) (actual=20 time=3D29.243..235.289 rows=3D220147 loops=3D1) Hash Cond: (act.project_id = =3D proj.id)=20 -> Seq Scanon onp_crm_activity act (cost=3D0.00..10591.45 rows=3D217245 wid= th=3D8)=20 (actualtime=3D0.186..139.016 rows=3D220147 loops=3D1) -> Hash (cost=3D262.3= 5..262.35=20 rows=3D13711 width=3D4) (actual time=3D28.980..28.980 rows=3D13760 loops=3D= 1) Buckets:=20 16384Batches: 1 Memory Usage: 612kB -> Index Only Scan using=20 onp_crm_project_pkeyon onp_crm_project proj (cost=3D0.29..262.35 rows=3D137= 11 width=3D 4) (actual time=3D0.067..26.575 rows=3D13760 loops=3D1) Heap Fetches: 6146 = Planning=20 time: 41.021 ms Execution time: 605.530 ms (60 rows)=20 =C2=A0 That's 420195 / 605=C2=A0=3D 695x speedup! Thanks! =C2=A0 -- Andreas Joseph Krogh CTO / Partner - Visena AS Mobile: +47 909 56 963 andreas@visena.com www.visena.com =C2=A0 ------=_Part_2_677827721.1471207626079 Content-Type: text/html;charset=UTF-8 Content-Transfer-Encoding: quoted-printable
P=C3=A5 s=C3=B8ndag 07. august 2016 kl. 18:35:56, skrev Tom Lane <<= a href=3D"mailto:tgl@sss.pgh.pa.us">tgl@sss.pgh.pa.us>:
Dea= n Rasheed <dean.a.rasheed@gmail.com> writes:
> On 5 August 2016 at 21:48, Tom Lane <tgl@sss.pgh.pa.us> wrote: >> OK, thanks.=C2=A0 What shall we do about Andreas' request to back-= patch this?
>> I'm personally willing to do it, but there is the old bugaboo of &= quot;maybe
>> it will destabilize a plan that someone is happy with".

> My inclination would be to back-patch it because arguably it's a
> bug-fix -- at the very least the old behaviour didn't match the docs > for stadistinct:

Yeah.=C2=A0 I suspect that situations like this are pretty rare, or we'd ha= ve
recognized the problem sooner.=C2=A0 And at least for Andreas, it'd be fixi= ng
a real problem.=C2=A0 I'll apply the back-patch unless I hear objections in=
the next couple of hours.

> Additionally, I think that example is misleading because it's only
> really true if there are no null values in the column. Perhaps it
> would help to have a more explicit example to illustrate how nulls
> affect stadistinct, for example:

Good idea, will do.

regards, tom lane
=C2=A0
=C2=A0
(Sorry for the HTML-formatting, I'm using a web-based client which isn= 't very good at converting the HTML to plain-text)
=C2=A0
Just wanted to thank you for fixing this.
=C2=A0
Take this query (pg-9.5):
=C2=A0
explain analyze SELECT
      com.ent=
ity_id                AS company_id
    , sum((coalesce(log.adjusted_time, log.duration) / (3600 =
* 1000)::numeric) * coalesce(log.adjusted_hourly_rate, log.hourly_rate)) as not_invoiced_hours_amount_adjusted
    , sum((log.duration / (3600 * 1000)::numeric) * log.hourly_rate) as not_invoiced_hours_amount_original
    , sum(coalesce(log.adjusted_time, log.duration)) AS not_invoiced_hours_duration_adjusted
    , sum(log.duration) AS not_invoiced_hours_duration_original
FROM onp_crm_relatio=
n com
    JOIN onp_crm_act=
ivity_log log ON com=
.entity_id =3D log.r=
elation_id
    JOIN onp_crm_per=
son logfor ON logfor=
.onp_user_id =3D log=
.logged_for AND logf=
or.is_resource =3D FALSE
    LEFT OUTER JOIN =
(
            onp_crm_activity act INNER JOIN onp_crm_project proj ON act.project_id =3D proj.id
        ) ON log.act=
ivity_id =3D act.id
WHERE 1 =3D 1
      AND log.entry_type =3D 'TIMESHEET_ENTRY'
      AND log.start_date IS NOT NULL
      AND log=
.is_chargable =3D TRUE
      AND log=
.status IN (1,2,3,5)
      AND log.start_=
date <=3D '2016-06-27'
      --           AND com.entity_id =
=3D 2246068
      AND NOT EXISTS(
    SELECT * FROM onp_crm_calendarentry_=
invoice_membership cem
        JOIN onp_crm=
_invoice_line il ON =
cem.invoice_line_id =3D il.id
        JOIN onp_crm=
_invoice inv ON il.i=
nvoice_id =3D inv.entity_id
    WHERE cem.calend=
ar_entry_id =3D log.=
id
          AND inv.st=
atus_key =3D 'INVOICE_STATU=
S_INVOICED'
          AND inv.sent_date <=
=3D '2016-06-27'
          AND NOT EXISTS(
        SELECT * FROM onp_crm_invoice cr=
editnote
        WHERE il.inv=
oice_id =3D creditnote.credit_against
              AND cr=
editnote.status_key =3D 'IN=
VOICE_STATUS_INVOICED'
              AND creditnote.sent=
_date <=3D '2016-06-27'
    )
)

GROUP BY
    com.entit=
y_id;
=C2=A0
Before the fix, the plan was like this:
=C2=A0
                                           =
                                                                           =
  QUERY PLAN        =
                                                                           =
                                     =20
--------------------------=
---------------------------------------------------------------------------=
---------------------------------------------------------------------------=
---------------------------------------------------------------------------
 HashAggrega=
te  (cost=3D13874.94=
..13894.38 rows=3D972 width=3D32) (actual time=
=3D420188.465..420189.232 rows=3D462 loops=3D1)
   Group Key: com.en=
tity_id
   ->  Nested Loop Left =
Join  (cost=
=3D1710.21..13848.21 rows=3D972 width=3D32) (actual time=3D1004.858..420166.736 rows=3D3882 loop=
s=3D1)
         ->  Nested Loop=
 Anti Join  (=
cost=3D1709.51..1299=
0.02 rows=3D972 width=3D36=
) (actual time=3D1003.001..419356.749 ro=
ws=3D3882 loops=3D1)
               Join Filter<=
/span>: (cem.calendar_entry_id =3D log.id)
               Rows =
Removed by Join Filter: 3996874113
               ->  Hash Join  (cost=3D1172.69..10612.53 rows=3D972 width=3D44) (actual=
 time=3D29.085..333.=
323 rows=3D89431 loops=3D1=
)
                     Hash <=
/span>Cond: (log.rel=
ation_id =3D com.entity_id)
                     ->  Hash Join  (cost=3D1063.39..10489.85 ro=
ws=3D976 width=3D40) (actual time=3D27.598..261.822 rows=3D89431 lo=
ops=3D1)
                           =
Hash Cond: (log.logged_for =3D logfor.onp_user_id)
                           ->  Bitmap Heap Scan on onp_crm_activity_log log  (cost=3D976.83..10055.99 rows=3D90010 width=3D44) (actual time=3D27.402..223.407 rows=3D89486 loops=3D1)
                                 Recheck Cond: ((status =3D ANY ('{1,2,3,5}'::integer[])) AND is_chargable =
AND (start_date IS N=
OT NULL) AND =
((entry_type)::text =
=3D 'TIMESHEET_ENTRY'::text))
                                 Filter: (start_date <=3D '2016-06-27'::date)
                                 Rows Removed b=
y Filter: 991
                                 Heap Blocks: exact=3D6095
                                 ->  Bitmap Index Scan on origo_cal=
endar_entry_status_1_idx  (=
cost=3D0.00..954.33 =
rows=3D93008 width=3D0) (actual time=3D26.339..26.339 rows=3D101274 l=
oops=3D1)
                                       Index Cond: (status =3D ANY ('{1,2,3,5}'::intege=
r[]))
                           ->  Hash  (cost=3D39.46..39.46 rows=3D3768 width=3D8) (actual time=3D0.148..0.148 rows=3D36 loops=3D1)
                                 Buckets: 40=
96  Batches: 1  Memory Usage: 34kB
                                 ->  Bitmap Heap Scan on onp_crm_person logfor  (cost=3D3.69..39.46 rows=3D3768 width=3D8) (actual=
 time=3D0.049..0.137=
 rows=3D36 loops=3D1)
                                       Recheck Cond: (onp_user_id IS NOT NULL)
                                       Filter: (NOT is_resource)
                                       Rows Removed by Filter: 5
                                     =
  Heap Blocks: exact=3D11
                                     =
  ->  Bitmap Inde=
x Scan on onp=
_crm_person_onp_id_idx  (co=
st=3D0.00..2.75 rows=
=3D41 width=3D0) (actual time=3D0.021..0.021 rows=3D41 loops=3D1)
                     ->  Hash  (cost=
=3D80.66..80.66 rows=
=3D2291 width=3D8) (actual time=3D1.449..1.449 rows=3D2292 loops=3D1)
                           Buckets: 4096  Batches: 1  Memory Usage: 122kB
                           ->  Index Only Scan using onp_crm_relation_pkey on onp_crm_relation com  (cost=3D0.28..80.66 rows=3D2291 wi=
dth=3D8) (actual time=3D0.221..1.036 rows=3D22=
92 loops=3D1)
                                 Heap Fetches: 1127
               ->  Material=
ize  (cost=3D536.82.=
.2362.91 rows=3D1 width=3D=
4) (actual time=3D0.000..1.564 rows=3D44693 loops=3D89431)
                     ->  Nested Loop  (cost<=
/span>=3D536.82..2362.91 ro=
ws=3D1 width=3D4) (actual time=3D10.480..382.816 rows=3D85668 loops=
=3D1)
                           ->  Nested Loop  (cost=3D536.53..2362.59 rows=3D1 width=3D8) (actual time=3D10.437..176.495 rows=3D88731 l=
oops=3D1)
                                 ->  Hash Anti Join  (cost=3D536.24..2362.05 rows=3D1 width=3D8) (actual time=3D9.914..44.329 rows=3D76347 loops=3D1)
                                       Hash Cond: (il.invoice_id =3D creditnote.credit_against)
                                       ->  Index Only Scan using origo_invoice_line_id_invoice_idx on onp_crm_invoice_line il  (cost=3D0.29..1530.95 <=
span style=3D"color:#000080;font-weight:bold;">rows=3D78707 width=3D8) (actual time=3D0=
.213..20.450 rows=3D=
78787 loops=3D1)
                                             Heap Fetches: 3131
                                     =
  ->  Hash  (cost=3D390.36..39=
0.36 rows=3D11647 width=3D=
8) (actual time=3D9.589..9.589 rows=3D372 loops=3D1)
                                             Buckets: 16384  Batches: 1  M=
emory Usage: 143kB
                                             ->  Index Only Scan using origo_invoice_credit_against_status_sent_=
idx on onp_crm_invoi=
ce creditnote  (cost=
=3D0.29..390.36 rows=
=3D11647 width=3D8) (actual time=3D0.205..8.953 rows=3D11507 loops=3D1)
                                                   Index Cond: ((status_key =3D 'INVOICE_STATUS_INVOICED'::text) AND (sent_date <=3D '2016-06-27'::date))
                                                   Heap Fetches: 999
                                 ->  Index Only Scan using onp_crm_=
calendarentry_invoice_invoice_line_id_calendar_entr_key on onp_crm_calendarentry_invoice_member=
ship cem  (cost=3D0.=
29..0.45 rows=3D9 width=3D=
8) (actual time=3D0.001..0.001 rows=3D1 loops=3D76347)
                                       Index Cond: (invoice_line_id =3D il.id)
                                       Heap Fetches: 4951
                           -&g=
t;  Index Only Scan =
using origo_invoice_=
id_status_sent_idx on onp_crm_invoice inv  (cos=
t=3D0.29..0.31 rows<=
/span>=3D1 width=3D8) (actual time=3D0.002..0.002 rows=3D1 loops=3D88731)
                                 Index Cond: ((entity_id =3D il.invoice_id) AND (status_key =3D 'INVOICE_STATUS_INVOICED'::text) AND (sent_date <=3D '2016-06-27'::date))
                                 Heap Fetches: 9293
         ->  Nested Loop  (cost=3D0.71..0.87 rows=3D1 width=3D4) (actual time=3D0.207..0.207 rows=3D1 loops=3D3882)
               ->  Index=
 Scan using o=
np_crm_activity_pkey on onp_crm_activity act  (=
cost=3D0.42..0.56 ro=
ws=3D1 width=3D8) (actual time=3D0.199..0.199 rows=3D1 loops=3D3882)
                     Index =
Cond: (log.ac=
tivity_id =3D id)
               ->  Index=
 Only Scan using onp_crm_project_pkey on=
 onp_crm_project proj  (cost=3D0.29..0.30 rows=3D1 width=3D4) (actual time=3D0.006..0.006 rows=3D1 loops=3D3882)
                     Index =
Cond: (id =3D act.project_id)
                     Heap Fetches: 2782
 Planning time: 33.=
132 ms
 Execution time: 420195.041 ms
(55 rows)
=C2=A0
After the fix (using pg-9.5.4), the plan is this:
=C2=A0
                                           =
                                                                           =
                  QUERY PLAN                                                                   =
                                                                     =20
--------------------------=
---------------------------------------------------------------------------=
---------------------------------------------------------------------------=
---------------------------------------------------------------------------=
--------------------------------
 HashAggrega=
te  (cost=3D44223.20=
..44269.02 rows=3D2291 width=3D32) (actual tim=
e=3D604.480..604.729 rows=3D462 loops=3D1)
   Group Key: com.en=
tity_id
   ->  Hash Left Join  <=
/span>(cost=3D40805.=
80..42869.54 rows=3D=
49224 width=3D32) (actual =
time=3D591.912..597.284 rows=3D3882 loops=3D1)
         Hash Cond: =
(log.activity_id =3D=
 act.id)
         ->  Hash Join  =
(cost=3D24077=
.93..25498.04 rows=
=3D49224 width=3D36) (actual time=3D309.816..313.542 rows=3D3882 loops=3D1)
               Hash =
Cond: (log.relation_=
id =3D com.entity_id)
               ->  Merge Join  (cost=
=3D23968.63..24711.11 rows<=
/span>=3D49435 width=3D32) (actual time=3D304.949..307.474 rows=3D3882 loops=
=3D1)
                     Merge Cond: (logfor.onp_user_id =3D log.logged_for)
                     ->  Index Scan using onp_crm_person_onp_id_idx on onp_crm_person logfor  (cost=3D0.14..41.05 rows=3D3768 width=3D=
8) (actual time=3D0.194..1.321 rows=3D36 loops=3D1)
                           =
Filter: (NOT =
is_resource)
                           =
Rows Removed by Filt=
er: 5
                     ->  So=
rt  (cost=3D23968.49=
..24092.29 rows=3D49520 width=3D36) (actual ti=
me=3D304.579..305.137 rows=3D3893 loops=3D1)
                           Sort Key: log=
.logged_for
                           Sort Method: quicksort  Memory: 401kB
                           ->  Hash Anti Joi=
n  (cost=3D99=
97.74..20106.99 rows=
=3D49520 width=3D36) (actual time=3D147.213..302.609 rows=3D3893 loops=3D1)
                                 Hash Cond: (lo=
g.id =3D cem.calendar_entry_id)
                                 ->  Bitmap Heap Scan on onp_crm_activity_log log  (cost=3D976.83..10055.99 rows=3D90010 =
width=3D44) (actual time=3D28.286..158.599 rows=3D89486 loops=3D1)
                                       Recheck Cond: ((status =3D ANY ('{1,2,3,5}'::integer[])) AND is_chargable AND (start_date IS NOT NULL) AND <=
/span>((entry_type)::text <=
/span>=3D 'TIMESHEET_ENTRY'=
::text))
                                       Filter: (start_date <=3D '2016-06-27'::date)
                                       Rows Removed by Filter: 991
                                     =
  Heap Blocks: exact=3D6095
                                     =
  ->  Bitmap Inde=
x Scan on ori=
go_calendar_entry_status_1_idx  (cost=3D0.00..954.33 rows=3D93008 width=3D0) (actual time=3D27.251..27.251 rows=3D101274 loops=3D1)
                                             Index Cond: (status =3D ANY ('{1,2,3,5}'::=
integer[]))
                                 ->  Hash  (=
cost=3D7964.73..7964.73 rows=3D84494 width=3D4) (actual time=3D118.106..118.106 rows=3D85668 loops=3D1)
                                       Buckets: 131072  Batches: 1  Memory=
 Usage: 4036kB
                                       ->  Hash Join  (cost=3D1069.29..7964.73 rows=3D84494 wi=
dth=3D4) (actual time=3D15.605..102.721 rows=3D85668 loops=3D1)
                                             Hash Cond: (il.invoice_id =3D inv.entity_id)
                                             ->  Hash Anti Join  (cost=3D536.55..6265.54 rows=3D85741 width=3D8) (actual time=3D5.467..71.124 rows=3D88731 loops=3D1)
                                                   Hash Cond: (il.invoice_id =3D creditnote.cre=
dit_against)
                                                   ->  Merge Join  (cost=3D0.61..4501.71 rows=3D89266=
 width=3D8) (actual time=3D0.441..53.014 rows=3D89301 loops=3D1)
                                                         Merge Cond: (il.id=
 =3D cem.invoice_line_id)
                                                         ->  Index Only Scan using origo_invoice_line_id_invoice=
_idx on onp_crm_invo=
ice_line il  (cost=
=3D0.29..1530.95 rows=3D78707 width=3D8) (actual time=3D0.226..15.901 rows=3D78787 loops=3D1)
                                                               Heap Fetches=
: 3131
                                     =
                    ->  Index Only Scan using onp_crm_calendarentry_invoice_invoice_line_id_calendar_entr=
_key on onp_crm_cale=
ndarentry_invoice_membership cem  (cost=3D0.29..1662.98 rows=3D89266 width=3D=
8) (actual time=3D0.194..16.489 rows=3D89301 <=
/span>loops=3D1)
                                                               Heap Fetches=
: 5027
                                     =
              ->  Hash  (cost=
=3D390.36..390.36 rows=3D11647 width=3D8) (actual time=3D4.785..4.785 rows=3D372 loops=3D1)
                                                         Buckets: 16384  Batches: 1  Memory Usage: 143kB
                                                         ->  Index Only Scan using origo_invoice_credit_against_=
status_sent_idx on o=
np_crm_invoice creditnote  (cost=3D0.29..390.36 rows=3D11647 width=3D8) (actual time=3D0.020..4.213 rows=3D11507 loo=
ps=3D1)
                                                               Index Cond: ((status_key =3D 'INVOICE_STATUS_INVOICED'::text) AND (sent_date <=3D '2016-06-27'::date))
                                                               Heap Fetches=
: 999
                                     =
        ->  Hash =
 (cost=3D387.=
15..387.15 rows=3D11647 width=3D8) (actual tim=
e=3D10.063..10.063 r=
ows=3D11507 loops=3D1)
                                                   Buckets: 16384  Batches: 1  Memory Usage: <=
span style=3D"color:#0000ff;">578kB
                                                   ->  Index Only Scan using origo_invoice_id_status_sent_idx on onp_crm_invoice inv=
  (cost=3D0.29..387.=
15 rows=3D11647 width=3D8<=
/span>) (actual time=
=3D0.233..8.133 rows=
=3D11507 loops=3D1)
                                                         Index Cond: ((status_key =3D 'INVOICE_STATUS_INVOICED'::text) AND (sent_date <=3D '2016-06-27'::date))
                                                         Heap Fetches: 999
               ->  Hash  (cost=3D80.66..80.66 rows=3D229=
1 width=3D8) (actual time=3D4.831..4.831 rows=3D2292 loops=3D1)
                     Buckets: 4096  B=
atches: 1  Memory Usage: 1=
22kB
                     ->  Index Only Scan usi=
ng onp_crm_relation_pkey on onp_crm_relation com  (cost=3D0.28..80.66 rows=3D2291 width=3D=
8) (actual time=3D0.235..4.370 rows=3D2292 loops=3D1)
                           Heap Fetches: 112=
7
         ->  Hash  (cost=3D14012.31..14012.31 rows=3D217=
245 width=3D4) (actual time=3D281.015..281.015 rows=3D220147 loops=3D1)
               Buckets: 262144  Batch=
es: 1  Memory Usage: 9788<=
/span>kB
               ->  Hash =
Join  (cost=
=3D433.74..14012.31 rows=3D217245 width=3D4) (actual time=3D29.243..235.289 rows=3D220147 loops=
=3D1)
                     Hash <=
/span>Cond: (act.project_id =3D proj.id)
                     ->  Seq Scan on onp_crm_activity act  (cost=3D0.00..10591.45 rows=3D217245 width=3D8) (actual time=3D0.186..139.016 rows=3D220147 loops=3D1)
                     ->  Hash  (cost=
=3D262.35..262.35 rows=3D13711 width=3D4) (actual time=3D28.980..28.980 rows=3D13760 loops=3D1)
                           Buckets: 16384  <=
/span>Batches: 1  Memory Usage: 612kB
                           ->  Index Only Scan using onp_crm_project_pkey on onp_crm_project proj  (cost=3D0.29..262.35 rows=3D13711 w=
idth=3D4) (actual time=3D0.067..26.575 rows=3D=
13760 loops=3D1)
                                 Heap Fetches: 6146
 Planning time: 41.=
021 ms
 Execution time: 605.530 ms
(60 rows)
=C2=A0
That's 420195 / 605=C2=A0=3D 695x speedup!
Thanks!
=C2=A0
--
Andrea= s Joseph Krogh
CTO / Partner<= /span> - Visena AS
Mobile: +47 90= 9 56 963
=3D""
=C2=A0
------=_Part_2_677827721.1471207626079-- ------=_Part_1_869519999.1471207625606 Content-Type: image/png Content-Transfer-Encoding: base64 Content-Disposition: inline Content-ID: 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 ------=_Part_1_869519999.1471207625606-- ------=_Part_0_1631323581.1471207625485--