Subject | RE: [firebird-support] Slow query because an index is not picked up during sort |
---|---|
Author | Svein Erling Tysvær |
Post date | 2012-06-07T07:07:25Z |
>SET thanks, it makes sense. So, Firebird is unable to pick the right plan for >= because it doesn't have histograms, right?Yes, it doesn't have enough information to know that PHYSICAL_COPY.COMMIT_NUMBER >= 10000000 is quite selective.
I've no knowledge about Firebird 3, so the rest of this answer is based on my thinking rather than how Firebird implement things.
As long as you use constants as in your case then histograms might show the optimizer that only a small fraction of the records in the table satisfy this criteria and hence choose to use the index on this field. However, as Ann said, this isn't possible if you use parameters:
TIB_CURSOR1.SQL.Add(...'WHERE PHYSICAL_COPY.COMMIT_NUMBER >= :MyParam');
TIB_CURSOR1.Prepare;
TIB_CURSOR1.ParamByName('MyParam').AsInteger:=10000000;
couldn't take advantage of the histogram since the plan is determined upon prepare and the actual value for the parameter isn't set until later.
Set