Re: Query Problem

From: Tomas Berndtsson <tomas(at)nocrew(dot)org>
To: josh(at)agliodbs(dot)com
Cc: sqllist <pgsql-sql(at)postgresql(dot)org>
Subject: Re: Query Problem
Date: 2000-10-26 13:53:09
Message-ID: 80og07675m.fsf@junk.nocrew.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

Josh Berkus <josh(at)agliodbs(dot)com> writes:

> Folks:
>
> Here's the problem, in abstract: I need to select every record in table
> A that does not have a link in table B Join Table C where Table
> C.account = 11
>
> The relevant fields:
>
> Table_A
> CaseID
> Data
>
> Table_B
> GroupID
> CaseID
> Amount
>
> Table_C
> GroupID
> AccountID
>
> Thus, I need to select:
>
> SELECT Data FROM Table A
> WHERE CaseID NOT IN (
> SELECT CaseID FROM Table_B, Table_C
> WHERE Table_B.GroupID = TableC.GroupID
> AND TableC.AccountID = 11)
>
> The problem is, since Table_B and Table_C are large (10,000 records +)
> this exclusion query takes several *minutes* to run.
>
> I've fooled around with drectional joins, views, and temporary tables,
> but I can seem to find anything that works faster. Suggestions?

Without having tried, something like this might be faster:

SELECT Data FROM Table A
WHERE CaseID NOT IN (
SELECT CaseID FROM Table_B
WHERE Table_B.GroupID IN (
SELECT GroupID FROM Table_C
WHERE TableC.AccountID = 11))

Tomas

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Tom Lane 2000-10-26 14:02:13 Re: Alternate Database Locations
Previous Message Dronamraju Rajesh 2000-10-26 12:17:47 pg_atoi: error in "template1": can't parse "template1"