Q: will GROUP BY make use of an index to return tuples early?

From: Gunther Schadow <gunther(at)aurora(dot)regenstrief(dot)org>
To: pgsql-sql(at)postgresql(dot)org
Subject: Q: will GROUP BY make use of an index to return tuples early?
Date: 2002-06-09 17:07:35
Message-ID: 3D038B57.8000007@aurora.regenstrief.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

Hi pgsql optimizer gurus,

If I have a table

CREATE TABLE Foo(
id OID,
time TIMESTAMP,
value INTEGER
);

CREATE INDEX Foo_id_idx ON Foo(id);

and I have a query for

SELECT id, MIN(foo.time)
FROM Foo foo
GROUP BY foo.id;

can one tell the query executor to do an index scan on Foo_id_idx
such as to be sure the Foo tuples are being considered ordered by
foo.id in order to produce tuples before having worked through the
whole table? If we don't use that assumption of the index order and
would instead to a full table scan, the select could not return
anything until the full table scan is completed.

I am asking because if I wanted to stream the tuples of the first
query into anothe system to do a distributed semijoin, I would
like data to flow at all times while queries are still being executed.

If the answer is yes, there is a way, then how about if we do this:

CREATE INDEX Foo_id_time_idx ON Foo(id, time);

now, considering the same query, it could be executed even faster,
because we could do an index scan on Foo_id_time_idx and only need
to consider the first data tuple of every Foo.id group (because
the ordering of Foo_id_time_idx guarantees that the MIN(time) is
in the first tuple.

thank you,
-Gunther

--
Gunther Schadow, M.D., Ph.D. gschadow(at)regenstrief(dot)org
Medical Information Scientist Regenstrief Institute for Health Care
Adjunct Assistant Professor Indiana University School of Medicine
tel:1(317)630-7960 http://aurora.regenstrief.org

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Gunther Schadow 2002-06-09 17:43:44 Support for distributed queries with semijoins could be possible, if ...
Previous Message Stephan Szabo 2002-06-08 04:15:00 Re: Indexing timestamps