Advertisement
anchormodeling

Multistatement Table-valued Functions and Table Elimination

Sep 4th, 2013
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
T-SQL 2.00 KB | None | 0 0
  1. drop view lAN;
  2. drop function fAT3;
  3. drop function fAT2;
  4. drop table AT1;
  5. drop table AT2;
  6. drop table AN;
  7.  
  8. create table AN (
  9.     id int not null
  10. );
  11. go
  12.  
  13. create table AT1 (
  14.     id int not null
  15. );
  16. go
  17.  
  18. create table AT2 (
  19.     id int not null
  20. );
  21. go
  22.  
  23. create function fAT2 (
  24.     @dummy int
  25. )
  26. returns @AT table  (
  27.     id int not null primary key
  28.     -- with the primary key definition
  29.     -- the optimizer should be able
  30.     -- to do table elimination
  31. ) as
  32. begin
  33.     insert into @AT (id)
  34.     select id from AT2;
  35.     return
  36. end
  37. go
  38.  
  39. create function fAT3 (
  40.     @dummy int
  41. )
  42. returns @AT table  (
  43.     id int not null
  44.     -- without the primary key definition
  45.     -- there is no way for the optimizer
  46.     -- to know if the join will multiply
  47. ) as
  48. begin
  49.     insert into @AT (id)
  50.     select id from AT2;
  51.     return
  52. end
  53. go
  54.  
  55. create view lAN
  56. as
  57. select
  58.     an.id as AN_id,
  59.     at1.id as AT1_id,
  60.     at2.id as AT2_id,
  61.     at3.id as AT3_id
  62. from
  63.     AN an
  64. left join
  65.     AT1 at1
  66. on
  67.     at1.id = an.id
  68. left join
  69.     fAT2(0) at2
  70. on
  71.     at2.id = an.id
  72. left join
  73.     fAT3(0) at3
  74. on
  75.     at3.id = an.id;
  76. go
  77.  
  78. with rowGen(rowNum) as (
  79.     select
  80.         1
  81.     union all
  82.     select
  83.         rowNum + 1
  84.     from
  85.         rowGen
  86.     where
  87.         rowNum < 100000
  88. )
  89. insert into AN (id)
  90. select rowNum from rowGen
  91. option (maxrecursion 0);
  92.  
  93. insert into AT1 (id) select id from AN;
  94. insert into AT2 (id) select id from AN;
  95.  
  96. alter table AN add constraint pkAN primary key (id);
  97. alter table AT1 add constraint pkAT1 primary key (id);
  98. alter table AT1 add constraint fkAT1 foreign key (id) references AN(id);
  99.  
  100. update statistics AN;
  101. update statistics AT1;
  102. update statistics AT2;
  103. go
  104.  
  105. SET STATISTICS XML ON;
  106. -------------------------------------
  107. select
  108.     avg(cast(AN_id as bigint))
  109. from
  110.     lAN;
  111. go
  112.    
  113. select
  114.     avg(cast(AT1_id as bigint))
  115. from
  116.     lAN
  117. where
  118.     AT1_id is not null;
  119. go
  120.    
  121. select
  122.     avg(cast(AT2_id as bigint))
  123. from
  124.     lAN
  125. where
  126.     AT2_id is not null;
  127. go
  128.  
  129. select
  130.     avg(cast(AT3_id as bigint))
  131. from
  132.     lAN
  133. where
  134.     AT3_id is not null;
  135. go
  136. -------------------------------------
  137. SET STATISTICS XML OFF;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement