Subject Re: [firebird-support] Different and slower query plan in 2.x compared to 1.5.x
Author Svein Erling Tysvaer
I haven't noticed any reply to your question, Michiel. So, here is my
guess (though, admittedly, not a brilliant answer):

Firebird now have knowledge on index selectivity on each part of the
index that can be used, rather than only on the index as a whole. I
suppose that if ACT_DT has poor selectivity, whereas GRPDLN_ID has
considerably better selectivity, then Firebird now knows that the index
will probably not be very selective for this query, and that is
knowledge that the optimizer didn't have before.

I don't think it is the ATP NATURAL that is the main cause of things
being slow, it doesn't take long to use NATURAL on a table with 1423
records, I'd rather suspect that one of the indexes used for ACT has
poor selectivity. Though, except the old +0 (or LEFT JOIN) trick (which
often works great if you have problems with a particular query, but
isn't a good answer to a general question), I don't know how to improve
your situation. It is not surprising that Firebird chooses a completely
different plan when you try the same query on an empty database - after
all, the selectivity of indexes will be completely different.

Set

Michiel Spoor wrote:

select org.naam AS ORGANISATIE, zrg.client_id AS CLIENT_NR, act.act_dt
AS DATUM, atp.NAAM AS ACTIVITEIT_NAAM
from organisatie org
inner join zorgtraject zrg on zrg.org_id = org.org_id
inner join grp_deelname dln on dln.zorg_id = zrg.zorg_id
inner join activiteit act on act.grpdln_id = dln.grpdln_id
inner join activiteittype atp on atp.atype_id = act.atype_id
where org.org_id = 1
and (act.act_dt>='2008-01-01' AND
act.act_dt<='2008-12-31')

Firebird 1.5
PLAN JOIN (ORG INDEX (RDB$PRIMARY42),ACT INDEX
(IDX_ACT_ACTDT_GRPDLN),DLN INDEX (RDB$PRIMARY5),ZRG INDEX
(RDB$PRIMARY15),ATP INDEX (RDB$PRIMARY10))

Firebird 2.x
PLAN JOIN (ATP NATURAL, ACT INDEX (RDB$FOREIGN76, IDX_ACT_ACTDT_GRPDLN),
DLN INDEX (RDB$PRIMARY5), ZRG INDEX (RDB$PRIMARY15), ORG INDEX
(RDB$PRIMARY42))

The foreign key RDB$FOREIGN76 refers to the link to ACTIVITEITTYPE
(ATYPE_ID)

The secondary index used in all cases is:

CREATE ASC INDEX IDX_ACT_ACTDT_GRPDLN ON ACTIVITEIT (ACT_DT, GRPDLN_ID);

Is there a logical explanation for this behaviour?