Received: from maia.hub.org (unknown [200.46.204.183]) by mail.postgresql.org (Postfix) with ESMTP id A8CCF633557 for ; Fri, 18 Dec 2009 09:45:13 -0400 (AST) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.183]) (amavisd-maia, port 10024) with ESMTP id 91962-02-8 for ; Fri, 18 Dec 2009 13:45:04 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from smtp-out112.alice.it (smtp-out112.alice.it [85.37.17.112]) by mail.postgresql.org (Postfix) with ESMTP id 5479E633461 for ; Fri, 18 Dec 2009 09:45:04 -0400 (AST) Received: from FBCMMO03.fbc.local ([192.168.68.197]) by smtp-out112.alice.it with Microsoft SMTPSVC(6.0.3790.3959); Fri, 18 Dec 2009 14:45:01 +0100 Received: from FBCMCL01B02.fbc.local ([192.168.69.83]) by FBCMMO03.fbc.local with Microsoft SMTPSVC(6.0.3790.3959); Fri, 18 Dec 2009 14:45:01 +0100 Received: from lan.webthatworks.it ([79.0.145.79]) by FBCMCL01B02.fbc.local with Microsoft SMTPSVC(6.0.3790.3959); Fri, 18 Dec 2009 14:45:00 +0100 Received: from dawn.webthatworks.it (dawn.webthatworks.it [192.168.1.12]) by lan.webthatworks.it (Postfix) with ESMTP id 14377300719 for ; Fri, 18 Dec 2009 14:45:00 +0100 (CET) Received: from dawn.webthatworks.it (localhost [127.0.0.1]) by dawn.webthatworks.it (Postfix) with ESMTP id C857C3DB59 for ; Fri, 18 Dec 2009 14:44:59 +0100 (CET) Date: Fri, 18 Dec 2009 14:44:59 +0100 From: Ivan Sergio Borgonovo To: pgsql-sql@postgresql.org Subject: Re: get distinct + group by then filter Message-ID: <20091218144459.1520d620@dawn.webthatworks.it> In-Reply-To: <92869e660912180420h548ec7dak2f85f0780a8d0318@mail.gmail.com> References: <20091217162809.07f871e3@dawn.webthatworks.it> <92869e660912180420h548ec7dak2f85f0780a8d0318@mail.gmail.com> Organization: http://www.webthatworks.it X-Mailer: Claws Mail 3.7.3 (GTK+ 2.18.5; x86_64-pc-linux-gnu) Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable X-OriginalArrivalTime: 18 Dec 2009 13:45:00.0892 (UTC) FILETIME=[4B7A31C0:01CA7FE8] X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-2.599 tagged_above=-10 required=5 tests=BAYES_00=-2.599 X-Spam-Level: X-Archive-Number: 200912/47 X-Sequence-Number: 33485 On Fri, 18 Dec 2009 13:20:00 +0100 Filip Rembia=C5=82kowski wrote: > 2009/12/17 Ivan Sergio Borgonovo >=20 > > I've a web application and I'm trying to do some reporting on > > affiliate commission > > > > create table tracky_hit ( > > hitid serial, > > esid varchar(32), -- related to browser session > > track_time timestamp, > > aid varchar(32), -- affiliate code > > -- some other tracking stuff > > ); > > > > create table tracky_event ( > > eventid serial, > > esid varchar(32) references tracky_hit (esid) > This imples that tracky_hit.esid is at least UNIQUE. > ); my fault esid is not unique and that reference was added just to imply that tracky_hit.esid and tracky_event.esid are related. I'm still thinking if it's worth for extra safety to have a create table tracky_esid( esid varchar(32) primary key ); but I can't see any use of it other than recording the session starting time... and I'm not sure it could come handy, since the hit is recorded later, so I couldn't relate hit time and session time. > > create table tracky_ordergroup_event ( > > ordergroupid int references ..., > > eventid int references tracky_event (eventid) > > ); > > > > Now I'd like to pick up the first hit for each esid in a given > > interval of time for a given aid and relate them with > > ordergroupid. > > > > aid may change across the same esid. > > >=20 > If tracky_hit.esid is unique, then why same esid can have many > aids? >=20 > Can you specify more complete schema (at least PKeys would be > nice)? I just made a pk out of (hitid, track_time) The rest of the schema is the same and I have the query below working... I was just wondering if there was a "cleaner" way. > > Getting the first hit for each esid can be done: > > > > select min(hitid) as h > > from tracky_hit > > group by esid; > > > > or > > > > select distinct on (esid) hitid > > from tracky_hit > > order by esid, track_time; > > > DISTINCT ON seems a good aproach tu such queries. Yeah, they both seems to work... but is there any difference? These are the 2 plans: 1) HashAggregate (cost=3D10.60..11.10 rows=3D40 width=3D72) (actual time=3D0.041..0.041 rows=3D4 loops=3D1) -> Seq Scan on tracky_hit (cost=3D0.00..10.40 rows=3D40 width=3D72) (actual time=3D0.017..0.019 rows=3D7 loops=3D1) Total runtime: 0.111 ms (3 rows) 2) Unique (cost=3D11.46..11.66 rows=3D40 width=3D80) (actual time=3D0.061..0.066 rows=3D4 loops=3D1) -> Sort (cost=3D11.46..11.56 rows=3D40 width=3D80) (actual time=3D0.06= 0..0.063 rows=3D7 loops=3D1) Sort Key: esid, track_time Sort Method: quicksort Memory: 25kB -> Seq Scan on tracky_hit (cost=3D0.00..10.40 rows=3D40 width=3D80) (actual time=3D0.007..0.010 rows=3D7 loops=3D1) Total runtime: 0.102 ms (6 rows) But a) I'm not that skilled in reading plans b) there are nearli no record to test on But considering the plan is pretty different I'd say one should have a major performance advantage over the other. BTW this is working: > > select oe.ordergroupid from > > tracky_ordergroup_event oe > > join tracky_event e on e.eventid=3Doe.eventid > > join tracky_hit th on th.esid=3De.esid > > where th.hitid in > > (select distinct on (esid) hitid > > from tracky_hit > > where track_time between > > ('2009-12-01'::timestamp - interval '1 days') > > and > > ('2009-12-01'::timestamp + interval '1 months' + interval > > '1 days') > > order by esid, track_time > > ) > > and th.aid=3D'someaid' > > and th.track_time between > > ('2009-12-01'::timestamp) > > and > > ('2009-12-01'::timestamp + interval '1 months'); > > > > but this looks awful. Any better way? thanks --=20 Ivan Sergio Borgonovo http://www.webthatworks.it