Re: Suggestion for aggregate function

From: Manfred Koizar <mkoi-pg(at)aon(dot)at>
To: Greg Stark <gsstark(at)mit(dot)edu>
Cc: Bruno Wolff III <bruno(at)wolff(dot)to>, pgsql-hackers(at)postgresql(dot)org
Subject: Re: Suggestion for aggregate function
Date: 2003-01-19 18:14:06
Message-ID: u3ql2vspo0c0ucgerp3sac86f9sjde26v9@4ax.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On 17 Jan 2003 19:08:06 -0500, Greg Stark <gsstark(at)mit(dot)edu> wrote:
>Would this query be efficient if there's an index on item_id, price ? That is,
>would it know to do an index scan

Yes, at least to avoid the sort step.

> and be able to skip to the next item_id in
>the index as soon as a price was found?

I don't think so. Look at how the index scan retrieves all rows:

=> EXPLAIN ANALYZE
-> SELECT DISTINCT ON (item) item, price, store FROM sale ORDER BY item, price;
NOTICE: QUERY PLAN:

Unique (cost=0.00..412.24 rows=1024 width=12)
(actual time=0.93..549.95 rows=101 loops=1)
-> Index Scan using s_x1 on sale (cost=0.00..386.64 rows=10240 width=12)
(actual time=0.90..399.52 rows=10240 loops=1)
Total runtime: 551.55 msec

EXPLAIN
=> DROP INDEX s_x1;
DROP
=> EXPLAIN ANALYZE
-> SELECT DISTINCT ON (item) item, price, store FROM sale ORDER BY item, price;
NOTICE: QUERY PLAN:

Unique (cost=845.48..871.08 rows=1024 width=12)
(actual time=941.83..1152.25 rows=101 loops=1)
-> Sort (cost=845.48..845.48 rows=10240 width=12)
(actual time=941.71..1061.93 rows=10240 loops=1)
-> Seq Scan on sale (cost=0.00..163.40 rows=10240 width=12)
(actual time=0.37..273.41 rows=10240 loops=1)
Total runtime: 1304.63 msec

Servus
Manfred

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Robert Treat 2003-01-19 18:19:03 Re: Survey results from the PostgreSQL portal page
Previous Message Neil Conway 2003-01-19 17:52:34 Re: Can we revisit the thought of PostgreSQL 7.2.4?