FirebirdSQL/firebird

Cardinality estimation should use primary record versions only

hvlad opened this issue · 1 comments

The issue was found on FB3 due to failed QA test for CORE-5602.
While the test runs successfully on FB4-FB6, the issue is common for all versions.

The test case uses database backup from QA test for CORE-5602.

The test query:

execute block
as
declare relname varchar(32);
declare cnt int;
begin
  for select X.RDB$RELATION_NAME
        from RDB$RELATION_FIELDS X
       where X.RDB$FIELD_SOURCE = 'BOOL_EMUL'
      into :relname
  do begin
     select count(*)
       from RDB$DEPENDENCIES DEP, RDB$PROCEDURES PRC
      where DEP.RDB$DEPENDED_ON_NAME = :relname
        AND DEP.RDB$FIELD_NAME = :relname
        AND DEP.RDB$DEPENDED_ON_TYPE = 0 /* obj_relation */
        AND DEP.RDB$DEPENDENT_TYPE = 5 /* obj_procedure */
        AND DEP.RDB$DEPENDENT_NAME = PRC.RDB$PROCEDURE_NAME
        AND PRC.RDB$PACKAGE_NAME IS NULL
    into :cnt;
  end
end

Bad plan and stats:

PLAN JOIN (PRC INDEX (RDB$INDEX_21), DEP INDEX (RDB$INDEX_27, RDB$INDEX_28))
PLAN (X INDEX (RDB$INDEX_3))

Execute : 94,00 ms
Fetches: 164 751

Table Name Indexed reads
RDB$DEPENDENCIES 27000
RDB$INDICES 9
RDB$PROCEDURES 27000
RDB$RELATION_FIELDS 900

Good plan and stats:

PLAN JOIN (DEP INDEX (RDB$INDEX_28), PRC INDEX (RDB$INDEX_21))
PLAN (X INDEX (RDB$INDEX_3))

Execute : 47,00 ms
Fetches: 29 746

Table Name Indexed reads
RDB$DEPENDENCIES 27000
RDB$INDICES 7
RDB$RELATION_FIELDS 900

I find this fix to be extremely usefull. I have already a few cases when a complex SELECT ran for a few minutes, while on 3.0.12 the results are almost immediate. Thanks!