Re: how to effectively SELECT new "customers"

From: David Johnston <polobo(at)yahoo(dot)com>
To: pgsql-sql(at)postgresql(dot)org
Subject: Re: how to effectively SELECT new "customers"
Date: 2014-03-02 04:08:39
Message-ID: 1393733319509-5794267.post@n5.nabble.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

Jan Ostrochovsky wrote
>> Without incorporating additional meta-data about the purchases onto
>> the
>> customer table the most basic solution would be:
>
>> SELECT DISTINCT customer_id FROM products WHERE date > (now() - '12
>> months'::interval)
>> EXCEPT
>> SELECT DISTINCT customer_id FROM products WHERE date <= (now() - '12
>> months'::interval)
>
>> ---
>
>> Another solution:
>> WHERE ... >12 AND NOT EXISTS (SELECT ... WHERE <= 12)
>
>> David J.
>
> subsidiary matter: in what circumstances is better to use EXCEPT and in
> what NOT EXISTS?
>
> are those equivalents? tried to google their comparison, but no relevant
> results found for PostgreSQL

I don't know; it somewhat depends on how smart the planner is which is out
of my league. I would expect that NOT EXISTS is typically a better first
option since EXCEPT needs to do sorting and de-duplicating (maybe?) of large
amounts of data while the NOT EXISTS method seems to require some level of
nested looping to process but only needs to find a single matching record to
return false so less memory constraints.

Someone more familiar with the internals may be able to give a more detailed
answer from the top of their head.

But in a critical (or under-performing) piece of code you should probably
test both to see in your reality which one perform better as I would guess
hardware is going to have an impact.

David J.

--
View this message in context: http://postgresql.1045698.n5.nabble.com/how-to-effectively-SELECT-new-customers-tp5793867p5794267.html
Sent from the PostgreSQL - sql mailing list archive at Nabble.com.

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message ALMA TAHIR 2014-03-03 11:32:02 Re: Function Issue
Previous Message Jan Ostrochovsky 2014-03-01 16:09:07 Re: how to effectively SELECT new "customers"