Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1jW8et-0001Pg-Iy for pgsql-general@arkaria.postgresql.org; Wed, 06 May 2020 01:15:35 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1jW8es-0005XE-Ep for pgsql-general@arkaria.postgresql.org; Wed, 06 May 2020 01:15:34 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1jW8er-0005X7-Ms for pgsql-general@lists.postgresql.org; Wed, 06 May 2020 01:15:34 +0000 Received: from sonic317-33.consmr.mail.ne1.yahoo.com ([66.163.184.44]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1jW8en-0005Xw-So for pgsql-general@postgresql.org; Wed, 06 May 2020 01:15:33 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yahoo.com; s=s2048; t=1588727726; bh=XwKU9uAvNPY9EPHwaNn80LwgBFAOwGwaxlP2Y2PFvIc=; h=Date:From:Reply-To:To:Subject:References:From:Subject; b=JaR3NZ+tBWUG8Rox+iE81lWGdErpbX0555qKYROURYldXCfgzaMlnWN8oEsRFiy22FT/XEonuMk/jjqkBMi4O2vA/lVIlsHWK1gh+AHVUZT5njTi3VozmwWJwUM+cPCaSVHpiROCe4UDqNZIOXs184T/E6D2iXY0W07z4e68kXQVwBagX6TLGe4Fpv6uqg7v2wPuK9647qxA8ASnGdJN6aFRYAaMxh17kkUKLsKqc0qAK3FyAffO1fxmhv1EQJWlBX7CO0BN6ATtp7CntnVgR3lWTHAX3po6qo9dAzbeeB246GGh0vzAUouh+kamqp+O9XzeTJdlUFfh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oubA3jXsX28ZdJnQvdj7QwsUcrBwp9GBDWWXKDniNVW7epTJIq5ExxPhDZex.Oc9Q nKGFrnZ_8gvZ0Koksv2T1aV8OOao7egOXmPjYHo.yRyWlCAm2VCH.6iOMsAJAbLwG74GcgqVoD7B T8RyM2xwU5d03JxYvoiPBAit1N5Sod3J4m79pbyhP2uUK53LO6j.dRHt5gpcr2gHrHoLP_7OWV3Y DRC2Xoz49N8A3rQ8N82V8cyqUfjJJ47HDZNgfV2iFiPNt6q2Mt7vlLSr0aQQBzbTG6PfdGgoZpIp suMPuSAQnL5cIkjIT Received: from sonic.gate.mail.ne1.yahoo.com by sonic317.consmr.mail.ne1.yahoo.com with HTTP; Wed, 6 May 2020 01:15:26 +0000 Date: Wed, 6 May 2020 01:15:21 +0000 (UTC) From: Virendra Kumar Reply-To: Virendra Kumar To: Pgsql-general General Message-ID: <484413316.1946041.1588727721718@mail.yahoo.com> Subject: Wrong PostgreSQL Plan MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="----=_Part_1946040_467972957.1588727721716" References: <484413316.1946041.1588727721718.ref@mail.yahoo.com> X-Mailer: WebService/1.1.15756 YMailNorrin Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:68.0) Gecko/20100101 Firefox/68.0 Content-Length: 9905 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk ------=_Part_1946040_467972957.1588727721716 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit 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 ------=_Part_1946040_467972957.1588727721716 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable
Hi Everyone,

PG Version - RDS PostgreSQL 10.11

We have very simpl= e query of 3 table joins and a few filter clause. Optimizer is behaving lit= tle weird in the sense that for change in one filter it is using NESTED LOO= P 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)=20 FROM COSTMAX C, UNITMAX U, UNITTYP T=20 WHERE C.UCE=3DU.UCE=20 AND C.MADESC=3DT.MADESC=20 AND C.STC=3DT.STC=20 AND C.PTC=3DT.PTC AND C.MADESC=3D'LAX'=20 AND C.RD=3D'5May2020'=20 AND upper(T.STYPE)=3D'DA' =20 AND upper(T.TYPE)=3D'ACT' =20 AND upper(U.UNAME)=3D'I'


<= /div>

Good Plan
Hash Join (cost=3D193.18..1653= .63 rows=3D1 width=3D32) Hash Cond: ((u.uce)::numeric =3D c.uce) -> Seq Scan on unitmax u (cost=3D0.00..1457.67 rows=3D185 width=3D4) Filter: (upper(uname) =3D 'I'::text) -> Hash (cost=3D192.79..192.79 rows=3D31 width=3D11) -> Nested Loop (cost=3D0.70..192.79 rows=3D31 width=3D11) -> Index Scan using pk_styppe on unittyp t (cost=3D0.14.= .2.47 rows=3D1 width=3D15) Index Cond: (madesc =3D 'LAX'::text) Filter: ((upper(stype) =3D 'DA'::text) AND (upper(type)= =3D 'ACT'::text)) -> Index Scan using costmax_pk on costmax c (cost=3D0.56= ..189.85 rows=3D47 width=3D25) Index Cond: (((madesc)::text =3D 'LAX'::text) AND ((stc= )::text =3D t.stc) AND ((ptc)::text =3D t.ptc) AND (rd =3D '2020-04-27'= ::date))



Bad Plan
Nested Loop (cost= =3D0.70..1619.45 rows=3D1 width=3D32) Join Filter: (c.uce =3D (u.uce)::numeric) -> Nested Loop (cost=3D0.70..159.01 rows=3D1 width=3D11) -> Index Scan using pk_styppe on unittyp t (cost=3D0.14..2.47 = rows=3D1 width=3D15) Index Cond: (madesc =3D 'LAX'::text) Filter: ((upper(stype) =3D 'DA'::text) AND (upper(type) =3D '= ACT'::text)) -> Index Scan using costmax_pk on costmax c (cost=3D0.56..156.= 52 rows=3D1 width=3D25) Index Cond: (((madesc)::text =3D 'LAX'::text) AND ((stc)::tex= t =3D t.stc) AND ((ptc)::text =3D t.ptc) AND (rd =3D '2020-05-01'::date= )) -> Seq Scan on unitmax u (cost=3D0.00..1457.67 rows=3D185 width=3D4) Filter: (upper(uname) =3D 'I'::text)



We have pl= ayed 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<= br>
------=_Part_1946040_467972957.1588727721716--