agora inbox for pgsql-sql@postgresql.org
help / color / mirror / Atom feedFrom: David Johnston <polobo@yahoo.com>
To: pgsql-sql@postgresql.org
Subject: Re: how to effectively SELECT new "customers"
Date: Sat, 1 Mar 2014 20:08:39 -0800 (PST)
Message-ID: <1393733319509-5794267.post@n5.nabble.com> (raw)
In-Reply-To: <2042881838.607913.1393690147570.JavaMail.root@mobiletech.sk>
References: <1001132708.567696.1393510818245.JavaMail.root@mobiletech.sk>
<1393513774376-5793882.post@n5.nabble.com>
<2042881838.607913.1393690147570.JavaMail.root@mobiletech.sk>
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-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.ht...
Sent from the PostgreSQL - sql mailing list archive at Nabble.com.
--
Sent via pgsql-sql mailing list (pgsql-sql@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-sql
view thread (10+ messages)
Message-ID: <1393733319509-5794267.post@n5.nabble.com>
Permalink: ../1393733319509-5794267.post@n5.nabble.com/
Also on: postgresql.org/message-id/1393733319509-5794267.post@n5.nabble.com
reply
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients using the --to and --cc options:
reply via email
To: pgsql-sql@postgresql.org
Cc: polobo@yahoo.com
Subject: Re: how to effectively SELECT new "customers"
In-Reply-To: <1393733319509-5794267.post@n5.nabble.com>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by agora; see mirroring instructions
for how to clone and mirror all data and code used for this inbox