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 1jA1U8-000057-6l for pgsql-general@arkaria.postgresql.org; Fri, 06 Mar 2020 01:09:04 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1jA1U6-0001Ar-LS for pgsql-general@arkaria.postgresql.org; Fri, 06 Mar 2020 01:09:02 +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 1jA1U6-0001Ak-8E for pgsql-general@lists.postgresql.org; Fri, 06 Mar 2020 01:09:02 +0000 Received: from n3.nabble.com ([162.255.23.22]) by makus.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1jA1Ty-0004l9-Ly for pgsql-general@postgresql.org; Fri, 06 Mar 2020 01:09:00 +0000 Received: from n3.nabble.com (localhost [127.0.0.1]) by n3.nabble.com (Postfix) with ESMTP id 6855C1B7B54C7 for ; Thu, 5 Mar 2020 18:08:53 -0700 (MST) Date: Thu, 5 Mar 2020 18:08:53 -0700 (MST) From: greigwise To: pgsql-general@postgresql.org Message-ID: <1583456933425-0.post@n3.nabble.com> Subject: Table with many NULLS for indexed column yields strange query plan MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk I have a query like this: 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') ; 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: Merge Join (cost=3D1001.20..4076.67 rows=3D17278 width=3D167) (actual time=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=3D175= ) (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.027..22.327 rows=3D13107 loops=3D5) Filter: ((type)::text =3D 'Standard'::text) -> Index Scan using table1_pk on table1 (cost=3D0.44..1.43 rows=3D1 width=3D167) (actual time=3D0.005..0.005 rows=3D1 loops=3D65535) Index Cond: (id =3D table2.table1_id) -> Index Scan using table3_pkey on table3 (cost=3D0.27..53.40 rows=3D1 width=3D8) (actual time=3D0.041..0.048 rows=3D1 loops=3D1) Filter: ((number)::text =3D 'xxxx'::text) Rows Removed by Filter: 35 Planning time: 0.450 ms Execution time: 310.230 ms You can see the row estimate there is way off on the Parallel Index Scan. = =20 I suspect that this is because it's including the rows with null in the selectivity estimate even though the table3_id can't possibly be null here due to the inner join. =20 If I modify the query like this: 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') and table3_id is not null; Just adding in table3_id is not null at the end there, I get a much better plan. Nested Loop (cost=3D1.14..290.04 rows=3D66 width=3D167) (actual time=3D0.058..11.258 rows=3D2244 loops=3D1) -> Nested Loop (cost=3D0.70..64.46 rows=3D66 width=3D8) (actual time=3D0.049..2.873 rows=3D2244 loops=3D1) -> Index Scan using table3_pkey on table3 (cost=3D0.27..53.40 rows=3D1 width=3D8) (actual time=3D0.030..0.035 rows=3D1 loops=3D1) Filter: ((number)::text =3D 'xxxx'::text) Rows Removed by Filter: 35 -> Index Scan using index_table2_on_table3_id on table2=20 (cost=3D0.43..11.05 rows=3D1 width=3D16) (actual time=3D0.017..2.102 rows= =3D2244 loops=3D1) Index Cond: ((table3_id =3D table3.id) AND (table3_id IS NOT NULL)) Filter: ((type)::text =3D 'Standard'::text) -> Index Scan using table1_pk on table1 (cost=3D0.44..3.42 rows=3D1 width=3D167) (actual time=3D0.003..0.003 rows=3D1 loops=3D2244) Index Cond: (id =3D table2.=C2=A0id) Planning time: 0.403 ms Execution time: 11.672 ms Can I do anything statistics wise so that I get a better plan here or do I have to modify the query. It seems kinda hacky that I would have to specif= y is not null on that column since like I said it can't possibly be null. Thanks, Greig Wise -- Sent from: https://www.postgresql-archive.org/PostgreSQL-general-f1843780.h= tml