Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1smZMQ-006siy-UK for pgsql-general@arkaria.postgresql.org; Fri, 06 Sep 2024 13:50:51 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1smZMO-00FHEY-PY for pgsql-general@arkaria.postgresql.org; Fri, 06 Sep 2024 13:50:49 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1smZMO-00FH7S-Ch for pgsql-general@lists.postgresql.org; Fri, 06 Sep 2024 13:50:48 +0000 Received: from mail-lj1-x22a.google.com ([2a00:1450:4864:20::22a]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1smZMM-000RnY-6x for pgsql-general@postgresql.org; Fri, 06 Sep 2024 13:50:47 +0000 Received: by mail-lj1-x22a.google.com with SMTP id 38308e7fff4ca-2f6580c2bbfso26297421fa.1 for ; Fri, 06 Sep 2024 06:50:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1725630644; x=1726235444; darn=postgresql.org; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=eg9oCKSwfhOjT7FWyVkCFm9L5a9cSTZuz1zAhyUY6v0=; b=d35ih8mssnP3+n5lXyslan3gIv3UGDT2BSSKsT0mCyWSzwG6WAMbZY8XDo43ysVK2Q ++lqYGRR/CqPp+doE6gk2YPg/Xb+JDEuogJgZbrEELQP38VFWBdLcn9tC2HWht+8jmss QA61LpNWBAUSyR3AYxMaD1HFzcwWuZromYolUK+QLaD7xfr4pKPbBG4SpS2u6e5LBSLS pJdeKWcntcCpLacuXPYvb/9vWZcCZlw7kWcGy6UEVfCKzmvPJWaTIhVSLmGU0J+iObmA g4NJcLxi47tdojXG2WkbSfD9i3EhDEimUIAzr1bbNW+nh5OZMVxl/t+N4HJVH7dTHWjD zSBw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1725630644; x=1726235444; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=eg9oCKSwfhOjT7FWyVkCFm9L5a9cSTZuz1zAhyUY6v0=; b=VxcUkaBsOe4xVli3ftwHGKTtzZ9XyybTxbt6AXXCkcEwAuVJYTNQBitRgXoWhE4dIr 4Mj2v9ecoJaP0Tc3Eg+SxQXej7Ad8tdIgAC/i/mk4fl3jJFeJNmDKqI1hIvvfVTWv5bY brXc/5F9RahPwemd/Z/d6YtoIlRWlEtncZm/9u7wGlr6S3/b2d3his939nEPXouoMHqV SkZbIuLYhTRSW1Yq7waTcbejCwtBxIVjqM2vE7StHmAN1UUdOYIAq0C8embCzgVaaLz8 A/ADhf3paAcq3jZMkNfOc4zbB+OgKSz8wi09wvgKqA1ouneNUhxRNiQ5klmjW+EWhofh 4/vw== X-Gm-Message-State: AOJu0YxxW5YoS5+pvGOrG5gWpP5a769cgvAH7BzboOa6yqRiI4qIITcd u4VxLitdK20f1QnXARWqY5RvQEgLca0XWkCt7FyRpcvEF6EL5FQ5+cPAb1e5zKXI7ipxZunoCdn A7Q4mYiHG3v1EFqGAMYdfXmvLV8U= X-Google-Smtp-Source: AGHT+IHap+3GNsx4iYV3v6KkRcsNpHoouy/iU27eWbWevyYf/4/GS7bBKqAittbkE/Yz7qJ7f+tIvSfjG1ldmH8T0E4= X-Received: by 2002:a05:651c:501:b0:2f4:1d7:e286 with SMTP id 38308e7fff4ca-2f751a1a7d6mr9015961fa.18.1725630642970; Fri, 06 Sep 2024 06:50:42 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: From: Greg Sabino Mullane Date: Fri, 6 Sep 2024 09:50:04 -0400 Message-ID: Subject: Re: Using left joins instead of inner joins as an optimization To: Xavier Solomon Cc: pgsql-general@postgresql.org Content-Type: multipart/alternative; boundary="0000000000007ceb1d062173b172" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --0000000000007ceb1d062173b172 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Fri, Sep 6, 2024 at 7:05=E2=80=AFAM Xavier Solomon wrote: > > explain select b_id from b natural left join a; > results in a `Seq Scan on b`. Whereas the query > > explain select b_id from b natural join a; > results in a join with sequential scans on both a and b. > I think your example is a little too contrived. Try explaining select * from b natural left join a; and you should see the plans become equivalent again. I would expect a query that left joins but only pulls data from one table to be not feasible in real life. Yes, in an ideal world the non-left join would be smart enough to not even do the scan on a, but it's kind of a moot point outside of odd select clauses. - Is it a bad idea to use left joins to optimize this even if semantically > an inner join would be correct? > Not at all - if it works for you, go ahead. But I'm dubious you will gain much for queries that actually make use of the left join, at least for relatively simply selects. Cheers, Greg --0000000000007ceb1d062173b172 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
On Fri, Sep 6, 2024 at 7:05=E2=80=AFAM Xa= vier Solomon <xavier.solo= mon515@gmail.com> wrote:
> explain se= lect b_id from b natural left join a;
results in a `Seq Scan on b`. Wher= eas the query
> explain select b_id from b natural join a;
results= in a join with sequential scans on both a and b.

I think your example is a little too contrived. Try explai= ning=C2=A0
select * from b natural left join a;
and you= should see the plans become equivalent again.

I w= ould expect a query that left joins but only pulls data from one table to b= e not feasible in real life. Yes, in an ideal world the non-left join would= be smart enough to not even do the scan on a, but it's kind of a moot = point outside of odd select clauses.

- Is it a bad idea t= o use left joins to optimize this even if semantically an inner join would = be correct?

Not at all - if it = works for you, go ahead. But I'm dubious you will gain much for queries= that actually make use of the left join, at least for relatively simply se= lects.

Cheers,
Greg
=C2=A0
=
--0000000000007ceb1d062173b172--