Subject Natural plan on indexed column
Author Matthias Hanft
Hello,

I have a table called "requests" with a TIMESTAMP column
called "datetime". Beside the primary key on column ID,
there is an index on the "datetime" column to speed up
searches like "select * from requests where datetime
between ? and ?". This works without any problems.

Now I'd like to get the first and the last timestamp, and
the number of total entries. I tried

select min(datetime), max(datetime), count(*) from requests;

which does work, but I recognized that Firebird uses a
"natural" plan with non-indexed reads only.

If I include

where datetime <= '31.12.2999';

in the select statement, the datetime index is used, and
there are indexed reads only.

I wonder why a "where" clause causes Firebird to use the
index, whereas just "min" does obviously not - although
for a naive human being, just "min" should be predestinated
for using an index - isn't it?

I can live with that (and, on December 31st, 2999, I'll
be dead anyway :-)), but I'd be glad if anyone could
explain the reason for this.

Best regards

Matthias