Re: How to optimize this query ?

From: Stephan Szabo <sszabo(at)megazone(dot)bigpanda(dot)com>
To: krystoffff <proghome(at)silesky(dot)com>
Cc: <pgsql-sql(at)postgresql(dot)org>
Subject: Re: How to optimize this query ?
Date: 2003-08-13 00:52:47
Message-ID: 20030812174840.R27097-100000@megazone.bigpanda.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

On 12 Aug 2003, krystoffff wrote:

> Hi all
>
> I have to optmize this query, because it takes a while to run (about
> 30s)
>
> Here are the tables (with the keys):
> affiliate_lockout (6 rows) (member_id, affiliate_id)
> lead (4490 rows) (id, ...)
> member (6 rows) (id, ...)
> member_exclusion (3 rows) (member_id, member_id_to_exclude)
> purchase (10000 rows) (lead_id, member_id, ...)
>

I think you can possibly get better results in 7.3 and earlier
by using NOT EXISTS.

> Here is the query:
> SELECT *
> FROM lead
> WHERE
> (exclusive IS NULL OR (exclusive = 0 AND nb_purchases < 3)) AND
>

NOT EXISTS (
select lead_id from
purchase where lead_id=lead.id
and member_id=21101)

> id NOT IN (
> SELECT lead_id
> FROM purchase
> WHERE member_id = 21101

AND NOT EXISTS (
select * from affiliate_lockout
WHERE member_id=21101 and
affiliate_locked_id=lead.affiliate_id)

> ) AND affiliate_id NOT
> IN (
>
> SELECT affiliate_locked_id
> FROM affiliate_lockout
> WHERE member_id = 21101
> ) AND id NOT
> IN (
>
> SELECT lead_id
> FROM purchase
> INNER JOIN member_exclusion
> WHERE purchase.member_id = member_exclusion.member_id_to_exclude AND
> purchase.member_id = 21101

Doesn't this condition end up giving you a subset of the rows in the first
one?

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Franco Bruno Borghesi 2003-08-13 01:04:32 Re: How to optimize this query ?
Previous Message Vernon Smith 2003-08-12 23:06:33 Re: INSERT INTO ... SELECT