Re: count(*), EXISTS, indexes - Mailing list pgsql-sql

From Itai Zukerman
Subject Re: count(*), EXISTS, indexes
Date
Msg-id 8765pkr830.fsf@matt.w80.math-hat.com
Whole thread Raw
In response to Re: count(*), EXISTS, indexes  (Stephan Szabo <sszabo@megazone23.bigpanda.com>)
List pgsql-sql
>> >   CREATE TABLE A (x int PRIMARY KEY, real v);
>> >   CREATE TABLE B (x int);
>> >
>> >   SELECT sum(v) FROM A WHERE EXISTS (SELECT 1 FROM B WHERE A.x=B.x);
>>
>> This seems to be a reasonably-performing workaround:
>>
>>   SELECT DISTINCT x INTO TEMP C FROM A,B WHERE A.x=B.x;
>>   SELECT sum(v) FROM A,C WHERE A.x=C.x;
>
> Hmm, given that, would something like:
>
> select sum(v) from
>  (select distinct on(x) x,v from a,b where a.x=b.x) as foo;

Excellent.  Thanks!

-- 
Itai Zukerman  <http://www.math-hat.com/~zukerman/>



pgsql-sql by date:

Previous
From: Itai Zukerman
Date:
Subject: Re: SELECT INTO TEMP in Trigger?
Next
From: Josh Berkus
Date:
Subject: Re: count(*), EXISTS, indexes