09.06.2022 14:18, Michael Vilhelmsen wrote:
>
> /In essence, the problem that with the wrong plan engine tries to order
> with index 216M of records first, and only then apply the filter for BonNr./
Not exactly. At the first stage, engine scans the index POINT_KVIT_IDX1
for matches (BonNr=6899403) and remembers the resulting rows (their
DBKEYs). Then the engine scans the primary key index until it gets the
key with DBKEY equal to one of the priorly remembered ones. In your
case, it appears that ID of record(s) with BonNr=6899403 are pretty far
from the beginning (in the ID order). Hence the slowdown.
> Above explains why - I was just wondering why it was this way.
There's no other way to use an index for ordering in your query. And
Firebird always tries to use an index-based ordering (if possible)
despite the external sort (SORT plan) could in fact be better.
Dmitry