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 1o1YJp-00019L-DQ for pgsql-admin@arkaria.postgresql.org; Wed, 15 Jun 2022 19:04:45 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1o1YJn-0007rw-Bp for pgsql-admin@arkaria.postgresql.org; Wed, 15 Jun 2022 19:04:43 +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 1o1YJn-0007rn-1s for pgsql-admin@lists.postgresql.org; Wed, 15 Jun 2022 19:04:43 +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 1o1YJg-0005y9-Je for pgsql-admin@postgresql.org; Wed, 15 Jun 2022 19:04:42 +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 25FJ4Y3L534506; Wed, 15 Jun 2022 15:04:34 -0400 From: Tom Lane To: Wells Oliver cc: pgsql-admin Subject: Re: Joining to views & the query planner In-reply-to: References: Comments: In-reply-to Wells Oliver message dated "Wed, 15 Jun 2022 11:39:26 -0700" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <534504.1655319874.1@sss.pgh.pa.us> Date: Wed, 15 Jun 2022 15:04:34 -0400 Message-ID: <534505.1655319874@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Wells Oliver 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