Received: from malur.postgresql.org ([2a02:16a8:dc51::56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fdS01-00039X-8l for pgsql-performance@arkaria.postgresql.org; Thu, 12 Jul 2018 03:10:33 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1fdRzy-0007GM-NH for pgsql-performance@arkaria.postgresql.org; Thu, 12 Jul 2018 03:10:30 +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.89) (envelope-from ) id 1fdRzy-0007GF-Ca for pgsql-performance@lists.postgresql.org; Thu, 12 Jul 2018 03:10:30 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fdRzr-0004iU-Nf for pgsql-performance@postgresql.org; Thu, 12 Jul 2018 03:10:29 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.4/8.14.4) with ESMTP id w6C3AIEF023356; Wed, 11 Jul 2018 23:10:18 -0400 From: Tom Lane To: Lincoln Swaine-Moore cc: legrand legrand , pgsql-performance@postgresql.org Subject: Re: Improving Performance of Query ~ Filter by A, Sort by B In-reply-to: References: <1531345315560-0.post@n3.nabble.com> Comments: In-reply-to Lincoln Swaine-Moore message dated "Wed, 11 Jul 2018 19:31:46 -0400" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <23354.1531365018.1@sss.pgh.pa.us> Content-Transfer-Encoding: quoted-printable Date: Wed, 11 Jul 2018 23:10:18 -0400 Message-ID: <23355.1531365018@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Lincoln Swaine-Moore writes: > Here's the result (I turned off the timeout and got it to finish): > ... I think the core of the problem here is bad rowcount estimation. We can't tell from your output how many rows really match > WHERE "a"."parent_id" IN ( > 49188,14816,14758,8402 > ) but the planner is guessing there are 5823 of them. In the case with only three IN items, we have > -> Index Scan using a_parent_id_idx1 on a_partition1 a (cost=3D= 0.43..4888.37 rows=3D4367 width=3D12) (actual time=3D5.581..36.270 rows=3D= 50 loops=3D1) > Index Cond: (parent_id =3D ANY ('{19948,21436,41220}'::in= teger[])) so the planner thinks there are 4367 matching rows but there are only 50. Anytime you've got a factor-of-100 estimation error, you're going to be really lucky if you get a decent plan. I suggest increasing the statistics target for the parent_id column in hopes of getting better estimates for the number of matches. regards, tom lane