Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1WJxhl-0006UY-6C for pgsql-sql@arkaria.postgresql.org; Sun, 02 Mar 2014 04:08:45 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1WJxhk-0004WX-IO for pgsql-sql@arkaria.postgresql.org; Sun, 02 Mar 2014 04:08:44 +0000 Received: from makus.postgresql.org ([2001:4800:7903:4::125]) by malur.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1WJxhj-0004WR-SE for pgsql-sql@postgresql.org; Sun, 02 Mar 2014 04:08:44 +0000 Received: from sam.nabble.com ([216.139.236.26]) by makus.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1WJxhg-00049W-Dr for pgsql-sql@postgresql.org; Sun, 02 Mar 2014 04:08:43 +0000 Received: from [192.168.236.26] (helo=sam.nabble.com) by sam.nabble.com with esmtp (Exim 4.72) (envelope-from ) id 1WJxhf-0007DP-Gj for pgsql-sql@postgresql.org; Sat, 01 Mar 2014 20:08:39 -0800 Date: Sat, 1 Mar 2014 20:08:39 -0800 (PST) From: David Johnston To: pgsql-sql@postgresql.org Message-ID: <1393733319509-5794267.post@n5.nabble.com> 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> Subject: Re: how to effectively SELECT new "customers" MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Pg-Spam-Score: 4.5 (++++) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-sql Precedence: bulk Sender: pgsql-sql-owner@postgresql.org 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. -- Sent via pgsql-sql mailing list (pgsql-sql@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-sql