select ( select first 1 r.rdb$relation_name
from rdb$relations r
where r.rdb$relation_id = d.rdb$relation_id - 1 )
from (
select * from rdb$database
union all
select * from rdb$database
) d
PLAN (R NATURAL)
-- R is scanned naturally
PLAN (D RDB$DATABASE NATURAL)
PLAN (D RDB$DATABASE NATURAL)
But (without union):
select ( select first 1 r.rdb$relation_name
from rdb$relations r
where r.rdb$relation_id = d.rdb$relation_id - 1 )
from (
select * from rdb$database
) d
PLAN (R INDEX (RDB$INDEX_1))
-- R is read through the index
PLAN (D RDB$DATABASE NATURAL)
Test case 2 (with union):
select 1
from (
select * from rdb$relations
union all
select * from rdb$relations
) r
where exists ( select * from rdb$relation_fields f where f.rdb$relation_name = r.rdb$relation_name )
PLAN (F NATURAL)
PLAN (R RDB$RELATIONS NATURAL)
PLAN (R RDB$RELATIONS NATURAL)
But (without union):
select 1
from ( select * from rdb$relations ) r
where exists ( select * from rdb$relation_fields f where f.rdb$relation_name = r.rdb$relation_name )
PLAN (F INDEX (RDB$INDEX_4))
PLAN (R RDB$RELATIONS NATURAL)
Description
Test case 1 (with union):
select ( select first 1 r.rdb$relation_name
from rdb$relations r
where r.rdb$relation_id = d.rdb$relation_id - 1 )
from (
select * from rdb$database
union all
select * from rdb$database
) d
PLAN (R NATURAL)
-- R is scanned naturally
PLAN (D RDB$DATABASE NATURAL)
PLAN (D RDB$DATABASE NATURAL)
But (without union):
select ( select first 1 r.rdb$relation_name
from rdb$relations r
where r.rdb$relation_id = d.rdb$relation_id - 1 )
from (
select * from rdb$database
) d
PLAN (R INDEX (RDB$INDEX_1))
-- R is read through the index
PLAN (D RDB$DATABASE NATURAL)
Test case 2 (with union):
select 1
from (
select * from rdb$relations
union all
select * from rdb$relations
) r
where exists ( select * from rdb$relation_fields f where f.rdb$relation_name = r.rdb$relation_name )
PLAN (F NATURAL)
PLAN (R RDB$RELATIONS NATURAL)
PLAN (R RDB$RELATIONS NATURAL)
But (without union):
select 1
from ( select * from rdb$relations ) r
where exists ( select * from rdb$relation_fields f where f.rdb$relation_name = r.rdb$relation_name )
PLAN (F INDEX (RDB$INDEX_4))
PLAN (R RDB$RELATIONS NATURAL)
One more indirectly related test case (which internally involves an implicit union):
select 1
from rdb$relations r
where exists ( select * from rdb$relation_fields f where f.rdb$relation_name = r.rdb$relation_name )
-- PLAN (F INDEX (RDB$INDEX_4))
-- PLAN (R NATURAL)
select 1
from ( select rdb$relation_name, ( select 1 from rdb$database ) as c from rdb$relations ) r
where exists ( select * from rdb$relation_fields f where f.rdb$relation_name = r.rdb$relation_name )
--PLAN (R RDB$DATABASE NATURAL)
--PLAN (F NATURAL)
--PLAN (R RDB$RELATIONS NATURAL)
Dmitry Yemanov added a comment - 21/Dec/08 03:05 AM One more indirectly related test case (which internally involves an implicit union):
select 1
from rdb$relations r
where exists ( select * from rdb$relation_fields f where f.rdb$relation_name = r.rdb$relation_name )
-- PLAN (F INDEX (RDB$INDEX_4))
-- PLAN (R NATURAL)
select 1
from ( select rdb$relation_name, ( select 1 from rdb$database ) as c from rdb$relations ) r
where exists ( select * from rdb$relation_fields f where f.rdb$relation_name = r.rdb$relation_name )
--PLAN (R RDB$DATABASE NATURAL)
--PLAN (F NATURAL)
--PLAN (R RDB$RELATIONS NATURAL)
select 1
from rdb$relations r
where exists ( select * from rdb$relation_fields f where f.rdb$relation_name = r.rdb$relation_name )
-- PLAN (F INDEX (RDB$INDEX_4))
-- PLAN (R NATURAL)
select 1
from ( select rdb$relation_name, ( select 1 from rdb$database ) as c from rdb$relations ) r
where exists ( select * from rdb$relation_fields f where f.rdb$relation_name = r.rdb$relation_name )
--PLAN (R RDB$DATABASE NATURAL)
--PLAN (F NATURAL)
--PLAN (R RDB$RELATIONS NATURAL)