Backwards index scan

From: Dmitry Tkach <dmitry(at)openratings(dot)com>
To: pgsql-bugs(at)postgresql(dot)org, pgsql-general(at)postgresql(dot)org
Subject: Backwards index scan
Date: 2003-07-07 18:18:55
Message-ID: 3F09B98F.8090805@openratings.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-bugs pgsql-general pgsql-hackers

I am not sure if this is really a bug, but it certainly looks like one
to me...

I have a table that looks something like this:

create table huge_table
(
int x,
int y
);
create index huge_table_idx on huge_table (x,y);

It contains about 80 million rows...
I am trying to get those rows that have a particular value for x,
ordered by y in descending order (and I am looking to get just a few
first ones), so I am running a query like:

declare mycursor cursor for select * from huge_table where x=10 order by
x desc, y desc;
fetch 10 from mycursor;

this query takes 10 to 20 minutes!

This is because there are *lots* (a few million) of matches for x=10,
and _bt_first () scans through them *all* sequentually to get to the
last one.

Now, if I change the query to look like:

declare mycursor cursor for select * from huge_table where x > 9 and x <
11 order by x desc, y desc;
(which is the same thing)
then fetch 10 from mycursor; returns right away (under a second), just
as I expected.

I understand that with the generic approach to operators in postgres it
is, probably, not very feasible to try and teach _bt_first () to handle
this situation automatically (it would need to know how to get
next/previous value for every indexable type)... I guess, that could be
done by adding another kind of strategy to pg_amop for example...
Another way to work around this would be to allow ordering spec to be a
part of CREATE INDEX (I know, that informix does that for example) - so
that, I could do
create index huge_table_idx on huge_table (x, y desc);
... and then select * from huge_table where x=10 order x, y desc;
would not require a backwards scan to begin with.

Can something like this be done? What do you think?

Thanks!

Dima

Responses

Browse pgsql-bugs by date

  From Date Subject
Next Message Stephan Szabo 2003-07-07 21:44:46 Re: Backwards index scan
Previous Message Stephan Szabo 2003-07-06 23:44:58 Re: case sensitivity

Browse pgsql-general by date

  From Date Subject
Next Message Richard Huxton 2003-07-07 18:38:35 SQL Functions and plan time
Previous Message Brian Tarbox 2003-07-07 18:16:16 Re: PostgreSQL vs. MySQL

Browse pgsql-hackers by date

  From Date Subject
Next Message ivan 2003-07-07 19:21:32 Re: pg_stat_activity
Previous Message Brian Tarbox 2003-07-07 18:16:16 Re: PostgreSQL vs. MySQL