Subject plan & performance
Author Sergio H. Gonzalez
Hello, I have a currency's table and a history of how the value of each currency
changes. I need to select all the currencys (they are less than 10) plus the id
of the last change in the history. I'm using this and it seems to work ok. But
with a very populated history table, the query slows down.

The first question I have is:

1) why FB is not using the descending index I've created on MONEDAS_VALORES to
extract the MAX id ?

2) is that the better way to do what I'm doing?

Thanks!!


--THIS IS THE SELECT--

select
m.id,
m.descripcion,
m.valor,
max( v.id )

from monedas_valores v
inner join monedas m on (v.id_monedas = m.id)

group by
m.id, m.descripcion, m.valor


--THIS IS THE PLAN FB CHOOSE--

Plan
PLAN SORT (JOIN (M NATURAL, V INDEX (FK_MONEDAS_VALORES_1)))

Adapted Plan
PLAN SORT (JOIN (M NATURAL, V INDEX (FK_MONEDAS_VALORES_1)))


--THESE ARE THE TABLES--


CREATE TABLE MONEDAS (
ID ID,
DESCRIPCION DESCRIPCION,
VALOR MONEY,
SIMBOLO DESCRIPCION_CORTA,
SISTEMA BOOLEAN );

ALTER TABLE MONEDAS ADD CONSTRAINT PK_MONEDAS PRIMARY KEY (ID);



CREATE TABLE MONEDAS_VALORES (
ID ID,
ID_MONEDAS ID,
FECHA FECHA,
VALOR MONEY );

ALTER TABLE MONEDAS_VALORES ADD CONSTRAINT PK_MONEDAS_VALORES PRIMARY KEY (ID);

ALTER TABLE MONEDAS_VALORES ADD CONSTRAINT FK_MONEDAS_VALORES_1 FOREIGN KEY
(ID_MONEDAS) REFERENCES MONEDAS (ID);

CREATE DESCENDING INDEX IDX_MONEDAS_VALORES ON MONEDAS_VALORES (FECHA);

CREATE DESCENDING INDEX IDX_MONEDAS_VALORES1 ON MONEDAS_VALORES (ID);