Subject Forcing SQL Plans
Author robert.gilland
How can I force a plan to be used?

I am using Classic Firebird 2.1.3 on linux.

By using monitoring I was able to deduce that the wrong plan is being used:

for the below SQL

SELECT CAST( SPG.STOREGROUPID AS VARCHAR(10))
,CAST( SPG.PROMID AS VARCHAR(14))
,MIN( SPG.COSTSTARTDATE )
FROM SUPPLIERPROMGROUP SPG
JOIN FILTERSTD F
ON( F.FILTERID = 174 )
AND( CAST( SPG.HOSTNUMBER AS VARCHAR(14)) = F.CODE1 )
AND( SPG.COSTSTARTDATE IS NOT NULL )
AND( SPG.OWNERID = 'G0000026' )
GROUP BY SPG.STOREGROUPID, SPG.PROMID

PLAN SORT (JOIN (SPG INDEX (IDX_SUPPROMGROUP_COSTEND), F INDEX (RDB$PRIMARY211)))

The plan used was using the IDX_SUPPROM_GROUP_COSTEND Index.

Where as I was querying COSTSTARTDATE so the plan should have been:

PLAN SORT (JOIN (SPG INDEX (IDX_SUPPROMGROUP_COSTSTART), F INDEX (RDB$PRIMARY211)))

This seems to be clearly a bug.

So I want to enforce the PLAN, to get around it.

How can I do this?

Kind Regards,

Robert.