Subject Re: [ib-support] (RESULTS) Re: How to improve select performance
Author Svein Erling Tysvaer
Sorry for this late comment, I was offline while the discussion evolved.

>the execution plan is:
>PLAN JOIN (MO INDEX (RDB$FOREIGN209),T INDEX (RDB$PRIMARY206,SK_TURNO_DATA))

The reason for your initial select being slow, is that it starts off by
checking the criteria MO.MaquinaID = 17. Then it checks the result against
the index for the primary key of the Turno table as well as the index for
the data field. When I see this in my queries, I generally say 'No, too
many indexes, better prevent some of them being used'. Moreover, I hate it
when it selects two indexes for the latter table and not only for the
first. Basically, all you need is a simple lookup for the DATA field and
then check this against the MaquinaOcupacao table.

Daniels solution is good, but I think you could achieve much of the same
thing simpler like this:

select
min(T.Data)
from
Turno T join
MaquinaOcupacao MO on (MO.TurnoID = T.TurnoID)
where
T.Data > '2002-12-01' and
(MO.MaquinaID = 17 or 2=0)

Preventing FB to use indexes like this (cannot use indexes on ORed
expressions) if often a simple way to 'negotiate' the optimizer into
choosing a more decent behaviour.

Set