Re: how to effectively SELECT new "customers" - Mailing list pgsql-sql

From David Johnston
Subject Re: how to effectively SELECT new "customers"
Date
Msg-id 1393733319509-5794267.post@n5.nabble.com
Whole thread Raw
In response to Re: how to effectively SELECT new "customers"  (Jan Ostrochovsky <ostrochovsky@mobiletech.sk>)
List 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.



pgsql-sql by date:

Previous
From: Jan Ostrochovsky
Date:
Subject: Re: how to effectively SELECT new "customers"
Next
From: ALMA TAHIR
Date:
Subject: Re: Function Issue