Re: Optimizing DISTINCT with LIMIT - Mailing list pgsql-hackers

From Tom Lane
Subject Re: Optimizing DISTINCT with LIMIT
Date
Msg-id 24128.1228401326@sss.pgh.pa.us
Whole thread Raw
In response to Re: Optimizing DISTINCT with LIMIT  (Heikki Linnakangas <heikki.linnakangas@enterprisedb.com>)
Responses Re: Optimizing DISTINCT with LIMIT  (tmp <skrald@amossen.dk>)
List pgsql-hackers
Heikki Linnakangas <heikki.linnakangas@enterprisedb.com> writes:
> Gregory Stark wrote:
>> You mean like this?
>> 
>> postgres=# explain select distinct x  from i limit 5;
>> QUERY PLAN                             
>> -------------------------------------------------------------------
>> Limit  (cost=54.50..54.51 rows=1 width=304)
>> ->  HashAggregate  (cost=54.50..54.51 rows=1 width=304)
>> ->  Seq Scan on i  (cost=0.00..52.00 rows=1000 width=304)
>> (3 rows)

> Does that know to stop scanning as soon as it has seen 5 distinct values?

In principle, if there are no aggregate functions, then nodeAgg could
return a row immediately upon making any new entry into the hash table.
Whether it's worth the code uglification is debatable ... I think it
would require a third major pathway through nodeAgg.
        regards, tom lane


pgsql-hackers by date:

Previous
From: Gregory Stark
Date:
Subject: Re: Optimizing DISTINCT with LIMIT
Next
From: Alvaro Herrera
Date:
Subject: Re: snapshot leak and core dump with serializable transactions