Re: Query for filtering records

From: eric soroos <eric-psql(at)soroos(dot)net>
To: pgsql-sql(at)postgresql(dot)org
Subject: Re: Query for filtering records
Date: 2002-12-03 20:52:04
Message-ID: 57416401.1173202572@[4.42.179.151]
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql


> SELECT * ...
> FROM ...
> WHERE NOT IN (SELECT contactnum FROM groups WHERE groupnum='c' or
> groupnum='d' OR ... )
>
> is bound to be _much_ faster!

Yeah, that's an obvious optimization. Unfortunately, due to needing to match semantics of a previous non-sql version and some pathological group specifications from clients, it can't be applied as often as I'd like. Yes, I call client specs pathological sometimes. No, they don't know that.

> And even better is
>
> SELECT *
> FROM ... contacts c1
> WHERE NOT EXISTS (SELECT * FROM groups WHERE groupname='c' or
> groupnum='d' or groupnume='e' ... AND groups.contactnum=c1.contactnum)
>
>
> EXISTS is almost always faster in PG.

Exists seems to be the answer. It even gives the right answer, which has been a problem for queries of this sort.

Rewriting the queries so that the subtraction clauses use exists are giving me reasonable runtimes (~5 sec) and believable answers. (instead of the other two extremes of 5 minutes and either no answers or everyone in the database) What's useful here is that I'm getting multiple exists index scans instead of nested loops or table scans. What's more, exists clauses are really easy to integrate into my query generation routine.

thanks

eric

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Vijay Uppal 2002-12-04 02:03:22 sql query
Previous Message mark carew 2002-12-03 20:49:59 Re: import sql script