pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Wrong PostgreSQL Plan
3+ messages / 2 participants
[nested] [flat]

* Wrong PostgreSQL Plan
@ 2020-05-06 01:15 Virendra Kumar <viru_7683@yahoo.com>
  2020-05-06 01:26 ` Re: Wrong PostgreSQL Plan David G. Johnston <david.g.johnston@gmail.com>
  0 siblings, 1 reply; 3+ messages in thread

From: Virendra Kumar @ 2020-05-06 01:15 UTC (permalink / raw)
  To: pgsql-general

Hi Everyone,
PG Version - RDS PostgreSQL 10.11
We have very simple query of 3 table joins and a few filter clause. Optimizer is behaving little weird in the sense that for change in one filter it is using NESTED LOOP JOIN and running for over 30 seconds whereas for other filter is working fine using HASH JOIN and under mili seconds. Here are two plans:
Query: SELECT COALESCE(TicketEXT,0) FROM COSTMAX C, UNITMAX U, UNITTYP T WHERE C.UCE=U.UCE AND C.MADESC=T.MADESC AND C.STC=T.STC AND C.PTC=T.PTC AND C.MADESC='LAX' AND C.RD='5May2020' AND upper(T.STYPE)='DA' AND upper(T.TYPE)='ACT' AND upper(U.UNAME)='I'


Good PlanHash Join (cost=193.18..1653.63 rows=1 width=32) Hash Cond: ((u.uce)::numeric = c.uce) -> Seq Scan on unitmax u (cost=0.00..1457.67 rows=185 width=4) Filter: (upper(uname) = 'I'::text) -> Hash (cost=192.79..192.79 rows=31 width=11) -> Nested Loop (cost=0.70..192.79 rows=31 width=11) -> Index Scan using pk_styppe on unittyp t (cost=0.14..2.47 rows=1 width=15) Index Cond: (madesc = 'LAX'::text) Filter: ((upper(stype) = 'DA'::text) AND (upper(type) = 'ACT'::text)) -> Index Scan using costmax_pk on costmax c (cost=0.56..189.85 rows=47 width=25) Index Cond: (((madesc)::text = 'LAX'::text) AND ((stc)::text = t.stc) AND ((ptc)::text = t.ptc) AND (rd = '2020-04-27'::date))


Bad PlanNested Loop (cost=0.70..1619.45 rows=1 width=32) Join Filter: (c.uce = (u.uce)::numeric) -> Nested Loop (cost=0.70..159.01 rows=1 width=11) -> Index Scan using pk_styppe on unittyp t (cost=0.14..2.47 rows=1 width=15) Index Cond: (madesc = 'LAX'::text) Filter: ((upper(stype) = 'DA'::text) AND (upper(type) = 'ACT'::text)) -> Index Scan using costmax_pk on costmax c (cost=0.56..156.52 rows=1 width=25) Index Cond: (((madesc)::text = 'LAX'::text) AND ((stc)::text = t.stc) AND ((ptc)::text = t.ptc) AND (rd = '2020-05-01'::date)) -> Seq Scan on unitmax u (cost=0.00..1457.67 rows=185 width=4) Filter: (upper(uname) = 'I'::text)


We have played little bit around default_statistics_target, sometimes it worked when the setting is around 1500 other times it doesn't work even with setting as high as 5000. Is there anything community can suggest us in resolving this?


Regards,
Virendra

^ permalink  raw  reply  [nested|flat] 3+ messages in thread

* Re: Wrong PostgreSQL Plan
  2020-05-06 01:15 Wrong PostgreSQL Plan Virendra Kumar <viru_7683@yahoo.com>
@ 2020-05-06 01:26 ` David G. Johnston <david.g.johnston@gmail.com>
  2020-05-06 01:41   ` Re: Wrong PostgreSQL Plan Virendra Kumar <viru_7683@yahoo.com>
  0 siblings, 1 reply; 3+ messages in thread

From: David G. Johnston @ 2020-05-06 01:26 UTC (permalink / raw)
  To: Virendra Kumar <viru_7683@yahoo.com>; +Cc: pgsql-general

On Tue, May 5, 2020 at 6:15 PM Virendra Kumar <viru_7683@yahoo.com> wrote:

> Optimizer is behaving little weird in the sense that for change in one
> filter
>

You mean when you change the date equality filter to a more recent date?


> it is using NESTED LOOP JOIN and running for over 30 seconds whereas for
> other filter is working fine using HASH JOIN and under mili seconds.
>

Which means it runs to completion which means EXPLAIN ANALYZE is possible
and considerably more helpful.

We have played little bit around default_statistics_target, sometimes it
> worked when the setting is around 1500 other times it doesn't work even
> with setting as high as 5000. Is there anything community can suggest us in
> resolving this?
>

Given that adding more data causes the good plan to stop working I would
have to say that increasing how often you run ANALYZE on the table,
irrespective of default_statistics_target, is probably a good thing to try.

David J.

^ permalink  raw  reply  [nested|flat] 3+ messages in thread

* Re: Wrong PostgreSQL Plan
  2020-05-06 01:15 Wrong PostgreSQL Plan Virendra Kumar <viru_7683@yahoo.com>
  2020-05-06 01:26 ` Re: Wrong PostgreSQL Plan David G. Johnston <david.g.johnston@gmail.com>
@ 2020-05-06 01:41   ` Virendra Kumar <viru_7683@yahoo.com>
  0 siblings, 0 replies; 3+ messages in thread

From: Virendra Kumar @ 2020-05-06 01:41 UTC (permalink / raw)
  To: David G. Johnston <david.g.johnston@gmail.com>; +Cc: pgsql-general

Hi David,
Thank you for your reply!
You mean when you change the date equality filter to a more recent date? - This is correct we have faced this when we change equality filter to more recent date.

Will be sending explain analyze on the thread when I get that.

We ran analyze many time to see if that resolves but looks like it is not buzzing. Also for regular analyze we don't run manually, they are all left to autoanalyze process.

Regards,Virendra

    On Tuesday, May 5, 2020, 6:34:37 PM PDT, David G. Johnston <david.g.johnston@gmail.com> wrote:  
 
 On Tue, May 5, 2020 at 6:15 PM Virendra Kumar <viru_7683@yahoo.com> wrote:

Optimizer is behaving little weird in the sense that for change in one filter


You mean when you change the date equality filter to a more recent date? 
 it is using NESTED LOOP JOIN and running for over 30 seconds whereas for other filter is working fine using HASH JOIN and under mili seconds.

Which means it runs to completion which means EXPLAIN ANALYZE is possible and considerably more helpful.

We have played little bit around default_statistics_target, sometimes it worked when the setting is around 1500 other times it doesn't work even with setting as high as 5000. Is there anything community can suggest us in resolving this?

Given that adding more data causes the good plan to stop working I would have to say that increasing how often you run ANALYZE on the table, irrespective of default_statistics_target, is probably a good thing to try.
David J.
  

^ permalink  raw  reply  [nested|flat] 3+ messages in thread


end of thread, other threads:[~2020-05-06 01:41 UTC | newest]

Thread overview: 3+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2020-05-06 01:15 Wrong PostgreSQL Plan Virendra Kumar <viru_7683@yahoo.com>
2020-05-06 01:26 ` David G. Johnston <david.g.johnston@gmail.com>
2020-05-06 01:41   ` Virendra Kumar <viru_7683@yahoo.com>

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