Advertisement
anchormodeling

Materializing Multistatement Table-valued Function Result

Sep 6th, 2013
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
T-SQL 2.51 KB | None | 0 0
  1. SET STATISTICS XML OFF;
  2. SET STATISTICS IO,TIME OFF;
  3.  
  4. --select @@VERSION;
  5. drop view lAN_forever;
  6. drop view lAN_quickly;
  7. drop function fAT_with_pk;
  8. drop function fAT_without_pk;
  9. drop table AT1;
  10. drop table AT2;
  11. drop table AN;
  12.  
  13. create table AN (
  14.     id int not null
  15. );
  16. go
  17.  
  18. create table AT1 (
  19.     id int not null
  20. );
  21. go
  22.  
  23. create table AT2 (
  24.     id int not null
  25. );
  26. go
  27.  
  28. create function fAT_with_pk (
  29.     @doNotLoopForever bit
  30. )
  31. returns @AT table  (
  32.     id int not null primary key
  33.     -- with the primary key definition
  34.     -- the optimizer should be able
  35.     -- to do table elimination and
  36.     -- in that case never materialize
  37.     -- this table
  38. ) as
  39. begin
  40.     declare @i int = 0;
  41.     while(@i >= @doNotLoopForever)
  42.     begin
  43.         set @i = @i + 1;
  44.         insert into @AT values(@i);
  45.     end
  46.     return
  47. end
  48. go
  49.  
  50. create function fAT_without_pk (
  51.     @dummy bit
  52. )
  53. returns @AT table  (
  54.     id int not null
  55. ) as
  56. begin
  57.     insert into @AT (id)
  58.     select id from AT1;
  59.     return
  60. end
  61. go
  62.  
  63. create view lAN_forever
  64. as
  65. select
  66.     an.id as AN_id,
  67.     at1.id as AT1_id,
  68.     at2.id as AT2_id,
  69.     at3.id as AT3_id
  70. from
  71.     AN an
  72. left join
  73.     AT1 at1
  74. on
  75.     at1.id = an.id
  76. left join
  77.     fAT_with_pk(0) at2
  78. on
  79.     at2.id = an.id
  80. left join
  81.     fAT_without_pk(0) at3
  82. on
  83.     at3.id = an.id;
  84. go
  85.  
  86. create view lAN_quickly
  87. as
  88. select
  89.     an.id as AN_id,
  90.     at1.id as AT1_id,
  91.     at2.id as AT2_id,
  92.     at3.id as AT3_id
  93. from
  94.     AN an
  95. left join
  96.     AT1 at1
  97. on
  98.     at1.id = an.id
  99. left join
  100.     fAT_with_pk(1) at2
  101. on
  102.     at2.id = an.id
  103. left join
  104.     fAT_without_pk(0) at3
  105. on
  106.     at3.id = an.id;
  107. go
  108.  
  109.  
  110. declare @numberOfRows int = 1000;
  111.  
  112. with rowGen(rowNum) as (
  113.     select
  114.         1
  115.     union all
  116.     select
  117.         rowNum + 1
  118.     from
  119.         rowGen
  120.     where
  121.         rowNum < @numberOfRows
  122. )
  123. insert into AN (id)
  124. select rowNum from rowGen
  125. option (maxrecursion 0);
  126.  
  127. insert into AT1 (id) select id from AN;
  128. insert into AT2 (id) select id from AN;
  129.  
  130. alter table AN add constraint pkAN primary key (id);
  131. alter table AT1 add constraint pkAT1 primary key (id);
  132. alter table AT1 add constraint fkAT1 foreign key (id) references AN(id);
  133.  
  134. update statistics AN;
  135. update statistics AT1;
  136. update statistics AT2;
  137. go
  138.  
  139. SET STATISTICS XML ON;
  140. SET STATISTICS IO,TIME ON;
  141. -------------------------------------
  142. select
  143.     avg(cast(AT1_id as bigint))
  144. from
  145.     lAN_quickly
  146. where
  147.     AT1_id is not null;
  148. -- this should execute and not run
  149. -- forever due to the infinite loop
  150. select
  151.     avg(cast(AT1_id as bigint))
  152. from
  153.     lAN_forever
  154. where
  155.     AT1_id is not null;
  156. -------------------------------------
  157. SET STATISTICS XML OFF;
  158. SET STATISTICS IO,TIME OFF;
  159. go
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement