Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.92) (envelope-from ) id 1jB2p3-0002UL-Tb for pgsql-general@arkaria.postgresql.org; Sun, 08 Mar 2020 20:46:54 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1jB2p1-0000ZQ-El for pgsql-general@arkaria.postgresql.org; Sun, 08 Mar 2020 20:46:51 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1jB2p1-0000Yx-0a for pgsql-general@lists.postgresql.org; Sun, 08 Mar 2020 20:46:51 +0000 Received: from mail.hjp.at ([212.17.106.138] helo=rorschach.hjp.at) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1jB2os-0002Nm-M1 for pgsql-general@lists.postgresql.org; Sun, 08 Mar 2020 20:46:48 +0000 Received: by rorschach.hjp.at (Postfix, from userid 1000) id 6ADB6274F; Sun, 8 Mar 2020 21:46:39 +0100 (CET) Date: Sun, 8 Mar 2020 21:46:39 +0100 From: "Peter J. Holzer" To: pgsql-general@lists.postgresql.org Subject: Re: Table with many NULLS for indexed column yields strange query plan Message-ID: <20200308204639.GE22585@hjp.at> References: <1583456933425-0.post@n3.nabble.com> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha512; protocol="application/pgp-signature"; boundary="2hMgfIw2X+zgXrFs" Content-Disposition: inline In-Reply-To: <1583456933425-0.post@n3.nabble.com> User-Agent: Mutt/1.10.1 (2018-07-13) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk --2hMgfIw2X+zgXrFs Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On 2020-03-05 18:08:53 -0700, greigwise wrote: > I have a query like this: >=20 > SELECT "table1".* FROM "table1" > INNER JOIN "table2" ON "table2"."table1_id" =3D "table1"."id" > INNER JOIN "table3" ON "table3"."id" =3D "table2"."table3_id" > WHERE "table3"."number" =3D 'xxxx' > AND ("table2"."type") IN ('Standard') ; >=20 > table2 has a large number of NULLS in the column table3_id. There is an > index on this column. Here is the result of explain analyze: >=20 > Merge Join (cost=3D1001.20..4076.67 rows=3D17278 width=3D167) (actual ti= me=3D284.918..300.167 rows=3D2244 loops=3D1) > Merge Cond: (table2.table3_id =3D table3.id) > -> Gather Merge (cost=3D1000.93..787825.78 rows=3D621995 width=3D175= ) (actual time=3D5.786..283.269 rows=3D64397 loops=3D1) > Workers Planned: 4 > Workers Launched: 4 > -> Nested Loop (cost=3D0.87..712740.12 rows=3D155499 width=3D1= 75) (actual time=3D0.091..102.708 rows=3D13107 loops=3D5) > -> Parallel Index Scan using index_table2_on_table3_id on= table2 (cost=3D0.43..489653.08 rows=3D155499 width=3D16) (actual time=3D0= =2E027..22.327 rows=3D13107 loops=3D5) > Filter: ((type)::text =3D 'Standard'::text) It looks like it postgresql is scanning the index here to get the entries in the right order for the merge join. It's strange that it thinks this is a good strategy even though it has to visit every row in the table (no index cond). How is the selectivity of "type"? Would an index on that column help? hp --=20 _ | Peter J. Holzer | Story must make more sense than reality. |_|_) | | | | | hjp@hjp.at | -- Charles Stross, "Creative writing __/ | http://www.hjp.at/ | challenge!" --2hMgfIw2X+zgXrFs Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQIzBAABCgAdFiEETtJbRjyPwVTYGJ5k8g5IURL+KF0FAl5lWaoACgkQ8g5IURL+ KF2fBw/+OuGSXR+4ilKMVFdvKp7Ea9ivIvV5J7UNboa8YciGq9LFx5eN6hkFCmzZ fksUf0xaknXjY8MF3QOM7IDSvF080i5kPUqJYtcgFfhnnaFMe7CW6qVtcDbmFDhc m00QQ/Jk1BryvRePJrLk8sSSuDYGs6aRBqD+KRQXR1+gCMYt5ghSU6meKAvTMlKC 4+rwndw3fxowiJxYMcwnEAhKjVl8Ojf2sRY6THOmQnV9weUzL82LplCiLoCjWJLc VeY2S6h1pONZyNtBMS7hsZOChnqAHC9VzzI8236QG2h1kmMbn2YFIstrsOahegrz jIxm+3+fMNqgY69HlhR+DdvZEezT9fhdB7A+XDto8bnq965twdTPHBp4AIJHM8Ab xFvDSzMdoda5VtQSAKKG9Bwj/OArzRSl3ZfgPS8pq+MDmJ+K8O8MMVHhwdvYxjjF +2xyaLyzThAAK7e5MZiNpc+RWlQDQNtAcjvXKCgCkTcGc8Amg4cinNjUF6SwQFbh XdGJ3MkODy9BlGxg3FIh3EqPnLl53AoX5qgC1DHcB6Q8p4YDnTUHKE6lebovdXni 6iX7V48OU+54M32lIeNv+PJQ2cwrSW5K1hudTGPdSSCnfp9O2aBIdzsNcstUyPq4 rCg+yOYnn/LuY0RU6l8LQ/eNiiPHKfb49D79EdxOkqib7GWuZEk= =q2Dj -----END PGP SIGNATURE----- --2hMgfIw2X+zgXrFs--