Subject RE: [firebird-support] improving select statement
Author Maya Opperman
>Hello!! I just need to select the last occurence of the field "UNITARIO" and
>it's ID

>This does the job, but I wander if there is a better option.

>ID and ID_STOCK has indexes, and the plan reports that they are being used


>SELECT
> F.UNITARIO, F.ID

>FROM
> FAC_COM_DETALLE F

>WHERE
> (F.ID_STOCK=:ID_STOCK)
> AND
> (F.ID = (select MAX(D.ID) from fac_com_detalle D WHERE D.id_stock =
>:ID_STOCK))

I have a similar requirement, and have found that replacing

select MAX(D.ID) from fac_com_detalle D WHERE D.id_stock = :ID_STOCK

with

select MAX first 1 D.ID from fac_com_detalle D WHERE D.id_stock = :ID_STOCK or by D.ID desc

can actually be faster (but does seem to defy logic for being faster...)

Maya