Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xtender/f0871ffa99b1413232e6 to your computer and use it in GitHub Desktop.
Save xtender/f0871ffa99b1413232e6 to your computer and use it in GitHub Desktop.
-- dropping new tabs:
drop table xt_big_tab purge;
drop table xt_nums purge;
-- parent table with 10 rows only:
create table xt_nums(id int primary key);
insert into xt_nums select level from dual connect by level<=10;
-- fact table:
create table xt_big_tab as
with nums as (select level nums_id from dual connect by level<=10)
,dates as (select sysdate-30+numtodsinterval(level,'minute') dt from dual connect by level<=35*24*60)
select nums_id, dt, nums_id || dt as padding_str from nums,dates;
alter table xt_big_tab modify nums_id not null;
-- foreign key to XT_NUMS(ID):
alter table xt_big_tab add constraint fk_big_table foreign key (nums_id) references xt_nums(id);
-- first column in the index is NUMS_ID - ID from XT_NUMS
create index ix_big_tab on xt_big_tab(nums_id, dt);
begin
dbms_stats.gather_table_stats('','XT_NUMS');
dbms_stats.gather_table_stats('','XT_BIG_TAB');
end;
/
-- join was eliminated and the cost is high:
select t.*
from xt_big_tab t, xt_nums n
where t.nums_id=n.id
and t.dt between sysdate-0.01 and sysdate
/
-- lower cost without join_elimination:
select/*+ NO_ELIMINATE_JOIN(n)*/ t.*
from xt_big_tab t, xt_nums n
where t.nums_id=n.id
and t.dt between sysdate-0.01 and sysdate
/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment