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 1nwcVt-00037j-Ky for pgsql-general@arkaria.postgresql.org; Thu, 02 Jun 2022 04:32:49 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1nwcVs-0005bC-D4 for pgsql-general@arkaria.postgresql.org; Thu, 02 Jun 2022 04:32:48 +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 1nwcVs-0005b3-2Q for pgsql-general@lists.postgresql.org; Thu, 02 Jun 2022 04:32:48 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1nwcVp-0000jJ-Qq for pgsql-general@postgresql.org; Thu, 02 Jun 2022 04:32:47 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.15.2/8.15.2) with ESMTP id 2524Wers843005; Thu, 2 Jun 2022 00:32:40 -0400 From: Tom Lane To: Jeff Janes cc: Tim Kelly , Postgres General Subject: Re: unoptimized nested loops In-reply-to: References: <62965CE8.7030500@dialectronics.com> Comments: In-reply-to Jeff Janes message dated "Thu, 02 Jun 2022 00:05:46 -0400" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <843003.1654144360.1@sss.pgh.pa.us> Date: Thu, 02 Jun 2022 00:32:40 -0400 Message-ID: <843004.1654144360@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Jeff Janes writes: > On Tue, May 31, 2022 at 4:04 PM Tim Kelly wrote: >> I do not see evidence that the nested loop is trying to reduce overhead >> by using the smaller set. It seems to want to scan on data first either >> way. > The planner probably doesn't know which one is smaller. There is not a lot of daylight between the cost estimates for "a nestloop-join b" and "b nestloop-join a", if we're considering plain seqscans on both tables and all else is equal. It tends to come down to factors like which one is more densely populated. As best I can tell, the issue Tim's unhappy about is not so much the use of a nestloop as the lack of use of any index. But "string like '%foo%'" is not at all optimizable with a btree index. You might be able to get somewhere with a pg_trgm GIN or GIST index. regards, tom lane