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 1jO15I-0001VE-2R for pgsql-admin@arkaria.postgresql.org; Mon, 13 Apr 2020 15:33:16 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1jO15G-0007zV-RZ for pgsql-admin@arkaria.postgresql.org; Mon, 13 Apr 2020 15:33:14 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1jO15G-0007zO-Ju for pgsql-admin@lists.postgresql.org; Mon, 13 Apr 2020 15:33:14 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1jO15E-0005sP-Ht for pgsql-admin@postgresql.org; Mon, 13 Apr 2020 15:33:13 +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 03DFXAkg002042; Mon, 13 Apr 2020 11:33:10 -0400 From: Tom Lane To: Mark Steben cc: pgsql-admin Subject: Re: Help needed on optimizing query In-reply-to: References: Comments: In-reply-to Mark Steben message dated "Mon, 13 Apr 2020 10:07:47 -0400" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <2040.1586791990.1@sss.pgh.pa.us> Content-Transfer-Encoding: quoted-printable Date: Mon, 13 Apr 2020 11:33:10 -0400 Message-ID: <2041.1586791990@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Mark Steben writes: > -> *Materialize (cost=3D0.57..654.71 rows=3D5190 width=3D8) (= actual > time=3D0.738..269.930 rows=3D5797068 loops=3D1)* > -> Nested Loop Left Join (cost=3D0.57..641.73 rows=3D51= 90 > width=3D8) (actual time=3D0.736..6.183 rows=3D5190 loops=3D1) > My specific question to you is: I notice in the boldened lines the > discrepancy between the 'estimated row count' and 'actual row count' Mos= t > blatant is one Materialize where estimated rowcount as calculated in the > explain is 5190 and actual rowcount is 5,797.068. How do I fix this so > that the estimated is closer to the actual. I don't think there's any estimation failure here at all: notice that the input left-join node's estimate is dead on. The reason the Materialize's output row count is higher is that the same rows are being read from it over and over, which is something that a Merge Join will do to its right-hand input when the left-hand input has a lot of duplicate join keys. The reason the planner stuck a Materialize here is exactly to make that as cheap as it could. It looks to me like the really bad aspect of this plan is that the most aggressive filter can't be applied till the very end: Filter: ((roles_users.id IS NOT NULL) OR (access_keys_users.id IS= NOT NULL) OR (accounts_users.id IS NOT NULL)) Rows Removed by Filter: 5613165 You're likely not going to be able to do much about that without a significant rethinking of your table layout. But having to do a seven-way join in a performance-critical query is already a sign that you're in for pain. regards, tom lane