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 1smWnP-006W4p-0b for pgsql-general@arkaria.postgresql.org; Fri, 06 Sep 2024 11:06:31 +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 1smWnN-00CZa0-K9 for pgsql-general@arkaria.postgresql.org; Fri, 06 Sep 2024 11:06:30 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1smFp6-00E5ZW-DU for pgsql-general@lists.postgresql.org; Thu, 05 Sep 2024 16:59:08 +0000 Received: from mail-vs1-xe43.google.com ([2607:f8b0:4864:20::e43]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1smFp4-000Jed-LR for pgsql-general@postgresql.org; Thu, 05 Sep 2024 16:59:08 +0000 Received: by mail-vs1-xe43.google.com with SMTP id ada2fe7eead31-49bc13c3a47so399592137.3 for ; Thu, 05 Sep 2024 09:59:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1725555544; x=1726160344; darn=postgresql.org; h=to:subject:message-id:date:from:mime-version:from:to:cc:subject :date:message-id:reply-to; bh=Sc93Zsl4W4lk4wz59CJdGWyvDMdvyGpcVZBaKtMKDfk=; b=ZtoCnzK8idNWTOlfe2215uZK4PpjGhjUhYhEfYiI+cInzl/vMpQ4J2DRvZfCNXsZbK EE7DT8D70m0zhesa+MC/WwazC9YvqkykS65E4GT5Jayq3nqPrLxVK1Q0jwUxNE8CkBrM tg0DnlJep76V3M6q3kBamU7seg5yCki9MpIYqDas2u+jksa9M84BrcmGD/YrljScJJK+ aysN1s7WDiGpSJa3ts7pOy0tq1h25931pC4JCzCsWO0h9YeuMI6rjxe2Vt284yuUxP0+ A2G4mIBSaqAFWXckKSXnyEH9GaNnxFxSfQQvj9u6hF2MWlbthHbEcDq1V0ePptBCzHQR BlgQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1725555544; x=1726160344; h=to:subject:message-id:date:from:mime-version:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=Sc93Zsl4W4lk4wz59CJdGWyvDMdvyGpcVZBaKtMKDfk=; b=rRSASNS8RCikwD+J8HdJuVdh0ELfxIXh8k82mwuUaWApjI2RNuHONjrKNOXeN9w2d3 UL4ZfJDKqyBbD6/X/A9jjjBMQf6G8kKCEjlFyux4Mh8ZLF2Ql21hVvJqhACEesqFjwM3 Mq/w8zc0DnKUlc8yjBo+7pAbZE5WRouyA8rWff0jJuw062WNa2fAb08m9JXivcRgzQ8m ocZVlU+3dzqrZHSYILhlswGWvhptvzNVY/njaLsYRGPU2dvEaVmZ3R+I0ala4yfmt7xu 9i+3/9Dl1lsoe+LBBA8eSz+Pqlaj9VZ3YzFdnN8z79JF4hI7Et8QB4JYBCBAvw9mP/Gp CanQ== X-Gm-Message-State: AOJu0YyRZ8I/BSid9KqCuBOUspUygLDUs3FIVRAa/o/ug22tT1mHiyqW PyCbsH7VJtURkw5jv24+9hMVOvrKnuO69QvU6AI6YGNg3cKJytZeo6LWdTf57K3p/qG2RzMCuAj NwXBCt3omUZMY0p4btFYyxgds0kCWQ7u+x8Q= X-Google-Smtp-Source: AGHT+IFKN32wOr4atFe3SXu6FPxO0WYFT+EW8uazjCN7LeKrBnZaV7gTpTawWvUtjt8WIUppXtrX6lE9ZY07ekbvZto= X-Received: by 2002:a05:6102:3747:b0:493:d325:4d71 with SMTP id ada2fe7eead31-49ba89d5de1mr13150489137.9.1725555544056; Thu, 05 Sep 2024 09:59:04 -0700 (PDT) MIME-Version: 1.0 From: Xavier Solomon Date: Thu, 5 Sep 2024 18:58:53 +0200 Message-ID: Subject: Using left joins instead of inner joins as an optimization To: pgsql-general@postgresql.org Content-Type: multipart/alternative; boundary="0000000000003e7021062162359f" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --0000000000003e7021062162359f Content-Type: text/plain; charset="UTF-8" Hi All! I'm currently writing a view that joins many tables and I was wondering how PostgreSQL optimizes projections on such a view. In particular I was wondering if it is a correct and valid optimization technique to use left joins when they are equivalent to an inner join. I have created a minimal example. Suppose we have two tables: > create table a(a_id int primary key generated always as identity, a_data text); > create table b(b_id int primary key generated always as identity, a_id int not null references a(a_id), b_data text); Then the query > 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 believe because b.a_id is not null and references a.a_id a left and an inner join are exactly equivalent. My questions are: - Am I wrong that in such a situation a left and inner join are equivalent? - Why does PostgreSQL not automatically optimize this? - Is it a bad idea to use left joins to optimize this even if semantically an inner join would be correct? Thank you for your help! --0000000000003e7021062162359f Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
Hi All!

I'm currently writing a view that joins= many tables and I was wondering how=C2=A0PostgreSQL optimizes
proj= ections on such a view. In particular I was wondering if it is a correct an= d valid optimization
technique to use left joins when they are eq= uivalent to an inner join.

I have created a minimal example. Suppo= se we have two tables:

> create table a(a_id int primary key gene= rated always as identity, a_data text);
> create table b(b_id int pri= mary key generated always as identity, a_id int not null references a(a_id)= , b_data text);

Then the query
> explain select b_id from b na= tural left join a;
results in a `Seq Scan on b`. Whereas the query
&g= t; explain select b_id from b natural join a;
results in a join with seq= uential scans on both a and b.

I believe because b.a_id is not null = and references a.a_id a left and an inner join are exactly equivalent.
<= br>My questions are:
- Am I wrong that in such a situation a left and in= ner join are equivalent?
- Why does PostgreSQL not automatically op= timize this?
- Is it a bad idea to use left joins to optimize thi= s even if semantically an inner join would be correct?

=
Thank you for your help!
--0000000000003e7021062162359f--