Subject Confused...
Author Svein Erling Tysvaer
I am still on Fb 1.0.2, so this is almost offtopic. The problem kind of
solved itself, so I'm just looking for some help to aid my confusion. OK,
here goes:

I had a query

SELECT ...
FROM B JOIN A ON A.PK = B.FIELD
WHERE (B.F2 STARTING 'AAAAA' OR
B.F2 STARTING 'BBBBB' OR
B.F2 STARTING 'CCCCC' OR
B.F2 STARTING 'DDDDD' OR
B.F3 = 'AAAA')

Preparing this, I got a lousy plan:
PLAN SORT( JOIN (A NATURAL, B (xPK_A, xF2, xF2, xF2, xF2, xF3)))

I tried to change the plan and include it in my statement (using the cursor
tab of IB_SQL):

PLAN SORT( JOIN (B (xF2, xF2, xF2, xF2, xF3), A (pkA)))

This left me with an error approximately like "Index xF3 cannot be used in
the specified plan". Then I realized that I had forgotten to exclude a few
records, modified my SQL and removed my desired plan:

SELECT ...
FROM B JOIN A ON A.PK = B.FIELD
WHERE B.BooleanField = 'N' AND
(B.F2 STARTING '90503' OR
B.F2 STARTING '90513' OR
B.F2 STARTING '90523' OR
B.F2 STARTING '90533' OR
B.F3 = '9053')

BooleanField is not indexed (of course), but still the suggested plan changed:

PLAN SORT( JOIN (B (xF2, xF2, xF2, xF2, xF3), A (pkA)))

Since this was the very same plan as I had tried to enforce earlier, I
copied it into the statement to see if it could be hardcoded. But no, same
error as last time. So I removed the hardcoded plan and just ran the query,
retrieving the few records that matched my requirements (100 or 200 records
- both A and B contains about 1M records). So, I managed to get my work
done using an effective plan, but I am still confused as to why the
optimizer changed its suggested plan due to a non-indexed field and why it
insisted on doing this by itself and refused the very same plan when I
hardcoded it (cut and paste removes the small possibility of a spelling
mistake).

By the way, xF2 and xF3 are indexes combining the F2 and F3 fields with the
primary key to increase selectivity. The selectivity is approximately
0.0000009, whereas the fields F2 and F3 contains a six- and fourdigit code
respectively (and certain codes are far more common than others).

Set