firebird icon indicating copy to clipboard operation
firebird copied to clipboard

UNION ALL optimalization with fix false condition

Open EPluribusUnum opened this issue 1 year ago • 2 comments

Hi *!

CREATE OR ALTER VIEW union_opt AS SELECT 'A' union_id FROM rdb$database UNION ALL SELECT 'B' FROM rdb$database

SELECT * FROM union_opt WHERE union_id = 'A'

The select does 2 non-index reads. I think it should be only 1 non-index read, as the second select expressions conditon is fix false, no row-by-row evaluation is needed.

Thank you!

EPluribusUnum avatar May 13 '24 10:05 EPluribusUnum

It will need to get that row to be able to evaluate that it should be filtered out.

mrotteveel avatar May 13 '24 16:05 mrotteveel

The filtered column is a constant, so the column value is known at prepare time. The condition on the constant column also constant, so the condition can be evaluated at prepare time without needing to fetch any record from the table at exec time.

EPluribusUnum avatar May 14 '24 06:05 EPluribusUnum

Plan and statistics after the fix:

Select Expression
    -> Filter
        -> Union
            -> Filter (preliminary)
                -> Table "RDB$DATABASE" as "UNION_OPT RDB$DATABASE" Full Scan
            -> Filter (preliminary)
                -> Table "RDB$DATABASE" as "UNION_OPT RDB$DATABASE" Full Scan

UNION_ID 
======== 
A        

Per table statistics:
--------------------------------+---------+---------+---------+---------+---------+---------+---------+---------+
 Table name                     | Natural | Index   | Insert  | Update  | Delete  | Backout | Purge   | Expunge |
--------------------------------+---------+---------+---------+---------+---------+---------+---------+---------+
RDB$DATABASE                    |        1|         |         |         |         |         |         |         |
--------------------------------+---------+---------+---------+---------+---------+---------+---------+---------+

dyemanov avatar Nov 15 '24 09:11 dyemanov

Related to #1708.

dyemanov avatar Nov 15 '24 09:11 dyemanov