Subject RE: RE: RE: [firebird-support] Re: Possible to write this in a way that indices will be used
Author Svein Erling Tysvær
>>I Wrote:
>>Did you try combining specifying a plan and converting to a range?
>>Though you still cannot change the plan depending on input params.
>That's the problem ;-)

Yes, it can be a problem. But I just did a quick test on a table with one million rows and two selective fields:

Select <whatever>
From tablea a
Where a.VerySelectiveField between :fromvalue1 and :tovalue1
And a.AlmostAsSelectiveField between :fromvalue2 and :tovalue2

Without specifying the plan, IndexVerySelectiveField is used. This is great with

Fromvalue1 = 435424
Tovalue1 = 435424
Fromvalue2 = 0
Tovalue2 = 10000000

but slowish with

Fromvalue1 = 0
Tovalue1 = 10000000
Fromvalue2 = 435424
Tovalue2 = 435424

However, if I specify the plan:

Select <whatever>
From tablea a
Where a.VerySelectiveField between :fromvalue1 and :tovalue1
And a.AlmostAsSelectiveField between :fromvalue2 and :tovalue2
PLAN (A INDEX(IndexVerySelectiveField, IndexAlmostAsSelectiveField))

then the performance is decent in both cases, although not as excellent as if the best plan had been chosen depending on the parameters.

>>With EXECUTE STATEMENT I suppose you could try to double any quotes
>>that you find, so that the statement at least could work.
>Hmm.., I've tested it now, and I see it's just single quotes that's the
>problem - I assumed %, ", :, etc would have been problematic too, but
>I'm using '=' not 'like', so the other's shouldn't be an issue here.
>Great.

If you want a single quote within a string, it has to be doubled, e.g.

Select 'Maya''s problem'
From rdb$database

would return Maya's problem

Doing the same with execute statement, I would expect to have to do

Execute statement 'Select ''Maya''''s problem'' From rdb$database'

HTH,
Set