Thread: query should use an index?

query should use an index?

From
Bopolissimus Platypus
Date:
hello,

i've got a table that looks like this (irrelevant data removed);

create table test (
  id serial primary key,
  t_end timestamp);

there's an index:
  create index test_t_end on test(t_end);

can or should a query like:

  select login,t_end from test order by t_end desc;

use the index? explain says it does a sequential scan
and sort.  but it seems to me that if it's possible for the
postgresql to use the index to seek to the last record and
then step backward, then it could use the index. but of
course i don't know anything about postgresql internals
so i'm really just guessing or going with what seems likely,
but from a position of ignorance.

tiger

--
Gerald Timothy Quimpo  gquimpo*hotmail.com tiger*sni*ph
http://bopolissimus.sni.ph
Public Key: "gpg --keyserver pgp.mit.edu --recv-keys 672F4C78"

    The best way to get rid of worries is to let them die of
     neglect.

Re: query should use an index?

From
Peter Eisentraut
Date:
Bopolissimus Platypus wrote:
> create table test (
>   id serial primary key,
>   t_end timestamp);
>
> there's an index:
>   create index test_t_end on test(t_end);
>
> can or should a query like:
>
>   select login,t_end from test order by t_end desc;
>
> use the index?

It can, but that does not mean that it is unconditionally the best
choice.  The planner will make the decision.