From: Tom Lane <tgl@sss.pgh.pa.us>
To: Wells Oliver <wells.oliver@gmail.com>
Cc: pgsql-admin <pgsql-admin@postgresql.org>
Subject: Re: Joining to views & the query planner
Date: Wed, 15 Jun 2022 15:04:34 -0400
Message-ID: <534505.1655319874@sss.pgh.pa.us> (raw)
In-Reply-To: <CAOC+FBX1rXLZi9o+TjJWGfYWzZmAg6s2GUTUtqhDKA6cQtzaYA@mail.gmail.com>
References: <CAOC+FBX1rXLZi9o+TjJWGfYWzZmAg6s2GUTUtqhDKA6cQtzaYA@mail.gmail.com>
Wells Oliver <wells.oliver@gmail.com> writes:
> Hi: we do this kind of thing a lot, where we CREATE VIEW v AS SELECT
> a.col1, b.col2 FROM a JOIN b ON a.b_id = b.id -- and then we write
> downstream queries to do something like SELECT * FROM v WHERE col2 = 123.
> Assuming here both a.col1 and a.col2 are indexed using BTREE.
> It seems from my experience that this bogs down, that the query planner
> doesn't necessarily know how to use indexes well from the joined table (b),
> and it ends up scanning a lot more rows than we might think necessary.
A simple test of that pattern gave me a reasonable-looking plan,
so I suspect you are leaving out critical details. How about
a concrete example?
(pgsql-performance would likely be a better venue, btw.)
regards, tom lane
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients using the --to and --cc options:
reply via email
To: pgsql-admin@postgresql.org
Cc: tgl@sss.pgh.pa.us, wells.oliver@gmail.com
Subject: Re: Joining to views & the query planner
In-Reply-To: <534505.1655319874@sss.pgh.pa.us>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox