pg.ddx.io  pgsql-performance@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Jay Levitt <jay.levitt@gmail.com>
To: pgsql-performance@postgresql.org
Subject: Subquery in a JOIN not getting restricted?
Date: Mon, 07 Nov 2011 16:25:08 -0500
Message-ID: <4EB84CB4.2010102@gmail.com> (raw)

When I run the following query:

select questions.id
from questions
join (
     select u.id as user_id
     from users as u
     left join scores as s
     on s.user_id = u.id
) as subquery
on subquery.user_id = questions.user_id;

the subquery is scanning my entire user table, even though it's restricted 
by the outer query.  (My real subquery is much more complicated, of course, 
but this is the minimal fail case.)

Is this just not a thing the optimizer can do?  Are there ways to rewrite 
this, still as a subquery, that will be smart enough to only produce the one 
row of subquery that matches questions.user_id?

Jay Levitt



view thread (15+ messages)  latest in thread

Message-ID: <4EB84CB4.2010102@gmail.com>
Permalink:  ../4EB84CB4.2010102@gmail.com/
Also on:    postgresql.org/message-id/4EB84CB4.2010102@gmail.com

 · 

reply

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-performance@postgresql.org
  Cc: jay.levitt@gmail.com
  Subject: Re: Subquery in a JOIN not getting restricted?
  In-Reply-To: <4EB84CB4.2010102@gmail.com>

* 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