Advertisement
anchormodeling

Substitute Primary Keys

Sep 10th, 2013
8,472
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
T-SQL 2.35 KB | None | 0 0
  1. -- if accidentally left on from previous runs
  2. SET STATISTICS XML OFF;
  3. SET STATISTICS IO,TIME OFF;
  4.  
  5. -- drop if they exist
  6. drop view lAN;
  7. drop view vAt2;
  8. drop function fAt1;
  9. drop table At1;
  10. drop table At2;
  11. drop table AN;
  12.  
  13. -- create the tables
  14. create table AN (
  15.     id int not null primary key
  16. );
  17.  
  18. create table At1 (
  19.     id int not null foreign key references AN(id),
  20.     stamp datetime not null,
  21.     primary key (
  22.         id,
  23.         stamp
  24.     )
  25. );
  26.  
  27. create table At2 (
  28.     id int not null foreign key references AN(id),
  29.     stamp datetime not null,
  30.     primary key (
  31.         id,
  32.         stamp
  33.     )
  34. );
  35. go
  36.  
  37. -- create the function
  38. create function fAt1 (@version int)
  39. returns table as return
  40. select
  41.     id,
  42.     stamp      
  43. from (
  44.     select 
  45.         id,
  46.         stamp,
  47.         ROW_NUMBER() over (
  48.             partition by
  49.                 id
  50.             order by
  51.                 stamp desc
  52.         ) as v
  53.     from
  54.         At1
  55. ) t
  56. where
  57.     t.v = @version 
  58. go
  59.  
  60. -- create a view
  61. create view vAt2
  62. as
  63. select 
  64.     id,
  65.     stamp,
  66.     ROW_NUMBER() over (
  67.         partition by
  68.             id
  69.         order by
  70.             stamp desc
  71.     ) as v
  72. from
  73.     At2
  74. go
  75.  
  76. -- create the view
  77. create view lAN
  78. as
  79. select
  80.     an.id as AN_id,
  81.     at1.id as AT1_id,
  82.     at1.stamp as AT1_stamp,
  83.     at2.id as AT2_id,
  84.     at2.stamp as AT2_stamp
  85. from
  86.     AN an
  87. left join
  88.     fAt1(1) at1
  89. on
  90.     at1.id = an.id
  91. left join
  92.     vAt2 at2
  93. on
  94.     at2.id = an.id
  95. and
  96.     at2.v = 1
  97. go
  98.  
  99. -- populate the tables
  100. declare @numberOfRows int = 1000;
  101.  
  102. with rowGen(rowNum) as (
  103.     select
  104.         1
  105.     union all
  106.     select
  107.         rowNum + 1
  108.     from
  109.         rowGen
  110.     where
  111.         rowNum < @numberOfRows
  112. )
  113. insert into AN (id)
  114. select rowNum from rowGen
  115. option (maxrecursion 0);
  116.  
  117. declare @now datetime = getdate();
  118. declare @then datetime = dateadd(day, -333, @now);
  119.  
  120. insert into At1 (id, stamp) select id, @now from AN;
  121. insert into At1 (id, stamp) select id, @then from AN;
  122. insert into At2 (id, stamp) select id, @now from AN;
  123. insert into At2 (id, stamp) select id, @then from AN;
  124. go
  125.  
  126. SET STATISTICS XML ON;
  127. SET STATISTICS IO,TIME ON;
  128. -------------------------------------------------------------
  129. -- query the view and check if table elimination is in effect
  130. select
  131.     avg(cast(AN_id as bigint))
  132. from
  133.     lAN;
  134.    
  135. select
  136.     avg(cast(AT1_id as bigint))
  137. from
  138.     lAN
  139. where
  140.     AT1_id is not null;
  141.  
  142. select
  143.     avg(cast(AT2_id as bigint))
  144. from
  145.     lAN
  146. where
  147.     AT2_id is not null;
  148. -------------------------------------------------------------
  149. SET STATISTICS XML OFF;
  150. SET STATISTICS IO,TIME OFF;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement