Advertisement
anchormodeling

No table elimination for window functions

Sep 6th, 2013
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
T-SQL 7.31 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. -- which version?
  6. select @@VERSION;
  7.  
  8. -- drop if they exist
  9. drop view lAN;
  10. drop view lAN_CTE;
  11. drop view vAt2;
  12. drop view vAt5;
  13. drop function fAt1;
  14. drop function fAt4;
  15. drop table At1;
  16. drop table At2;
  17. drop table At3;
  18. drop table At4;
  19. drop table At5;
  20. drop table AN;
  21.  
  22. -- create the tables
  23. create table AN (
  24.     id int not null primary key
  25. );
  26.  
  27. create table At1 (
  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.  
  36. create table At2 (
  37.     id int not null foreign key references AN(id),
  38.     stamp datetime not null,
  39.     primary key (
  40.         id,
  41.         stamp
  42.     )
  43. );
  44.  
  45. create table At3 (
  46.     id int not null foreign key references AN(id),
  47.     stamp datetime not null,
  48.     primary key (
  49.         id,
  50.         stamp
  51.     )
  52. );
  53.  
  54. create table At4 (
  55.     id int not null foreign key references AN(id),
  56.     stamp datetime not null,
  57.     primary key (
  58.         id,
  59.         stamp
  60.     )
  61. );
  62.  
  63. create table At5 (
  64.     id int not null foreign key references AN(id),
  65.     stamp datetime not null,
  66.     primary key (
  67.         id,
  68.         stamp
  69.     )
  70. );
  71. go
  72.  
  73. -- create the functions
  74. create function fAt1 (@version int)
  75. returns table as return
  76. select
  77.     id,
  78.     stamp      
  79. from (
  80.     select 
  81.         id,
  82.         stamp,
  83.         ROW_NUMBER() over (
  84.             partition by
  85.                 id
  86.             order by
  87.                 stamp desc
  88.         ) as v
  89.     from
  90.         At1
  91. ) t
  92. where
  93.     t.v = @version 
  94. go
  95.  
  96. create function fAt4 (@version int)
  97. returns @t table (
  98.     id int not null primary key,
  99.     stamp datetime not null
  100. ) as
  101. begin
  102.     insert into @t
  103.     select
  104.         id,
  105.         stamp      
  106.     from (
  107.         select 
  108.             id,
  109.             stamp,
  110.             ROW_NUMBER() over (
  111.                 partition by
  112.                     id
  113.                 order by
  114.                     stamp desc
  115.             ) as v
  116.         from
  117.             At4
  118.     ) t
  119.     where
  120.         t.v = @version;
  121.     return
  122. end
  123. go
  124.  
  125. -- create a view
  126. create view vAt2
  127. as
  128. select 
  129.     id,
  130.     stamp,
  131.     ROW_NUMBER() over (
  132.         partition by
  133.             id
  134.         order by
  135.             stamp desc
  136.     ) as v
  137. from
  138.     At2
  139. go
  140.  
  141. create view vAt5
  142. as
  143. select
  144.     id,
  145.     stamp,
  146.     lag(stamp, 1, stamp) over (partition by id order by stamp desc) as stamp_prev
  147. from
  148.     At5
  149. go
  150.  
  151. -- create the latest view
  152. create view lAN
  153. as
  154. select
  155.     an.id as AN_id,
  156.     at1.id as AT1_id,
  157.     at1.stamp as AT1_stamp,
  158.     at2.id as AT2_id,
  159.     at2.stamp as AT2_stamp,
  160.     at3.id as AT3_id,
  161.     at3.stamp as AT3_stamp,
  162.     at4.id as AT4_id,
  163.     at4.stamp as AT4_stamp,
  164.     at5.id as AT5_id,
  165.     at5.stamp as AT5_stamp
  166. from
  167.     AN an
  168. left join
  169.     fAt1(1) at1
  170. on
  171.     at1.id = an.id
  172. left join
  173.     vAt2 at2
  174. on
  175.     at2.id = an.id
  176. and
  177.     at2.v = 1
  178. left join
  179.     At3 at3
  180. on
  181.     at3.id = an.id
  182. and
  183.     at3.stamp = (
  184.         select
  185.             sub.stamp
  186.         from
  187.             At3 sub
  188.         where
  189.             sub.id = an.id
  190.         order by
  191.             sub.stamp desc
  192.         offset 0 rows fetch next 1 rows only
  193.     )
  194. left join
  195.     fAT4(1) at4
  196. on
  197.     at4.id = an.id
  198. left join
  199.     vAt5 at5
  200. on
  201.     at5.id = an.id
  202. and
  203.     at5.stamp = (
  204.         select
  205.             sub.stamp
  206.         from
  207.             vAt5 sub
  208.         where
  209.             sub.id = an.id
  210.         and
  211.             sub.stamp = sub.stamp_prev
  212.     );
  213.  
  214. go
  215.  
  216. create view lAN_CTE
  217. as
  218. with
  219. a1 as (
  220.     select TOP (2147483647)
  221.         *
  222.     from
  223.         fAt1(1)
  224.     order by
  225.         id asc
  226. ),
  227. a2 as (
  228.     select
  229.         *
  230.     from
  231.         vAt2
  232.     where
  233.         v = 1
  234. ),
  235. a3 as (
  236.     select
  237.         *
  238.     from
  239.         at3
  240.     where
  241.         at3.stamp = (
  242.             select
  243.                 sub.stamp
  244.             from
  245.                 At3 sub
  246.             where
  247.                 sub.id = at3.id
  248.             order by
  249.                 sub.stamp desc
  250.             offset 0 rows fetch next 1 rows only
  251.         )
  252. ),
  253. a4 as (
  254.     select
  255.         *
  256.     from
  257.         fAt4(1)
  258. ),
  259. a5 as (
  260.     select
  261.         *
  262.     from
  263.         vAt5
  264.     where
  265.         stamp = stamp_prev
  266. )
  267. select
  268.     an.id as AN_id,
  269.     (select id from a1 where a1.id = an.id) as AT1_id,
  270.     (select stamp from a1 where a1.id = an.id) as AT1_stamp,
  271.     (select stamp from a1 where a1.id = an.id) as AT1_stamp_again,
  272.     (select id from a2 where a2.id = an.id) as AT2_id,
  273.     (select stamp from a2 where a2.id = an.id) as AT2_stamp,
  274.     (select id from a3 where a3.id = an.id) as AT3_id,
  275.     (select stamp from a3 where a3.id = an.id) as AT3_stamp,
  276.     (select id from a4 where a4.id = an.id) as AT4_id,
  277.     (select stamp from a4 where a4.id = an.id) as AT4_stamp,
  278.     (select id from a5 where a5.id = an.id) as AT5_id,
  279.     (select stamp from a5 where a5.id = an.id) as AT5_stamp
  280. from
  281.     AN an;
  282. go
  283.  
  284. -- populate the tables
  285. declare @numberOfRows int = 1000;
  286.  
  287. with rowGen(rowNum) as (
  288.     select
  289.         1
  290.     union all
  291.     select
  292.         rowNum + 1
  293.     from
  294.         rowGen
  295.     where
  296.         rowNum < @numberOfRows
  297. )
  298. insert into AN (id)
  299. select rowNum from rowGen
  300. option (maxrecursion 0);
  301.  
  302. declare @now datetime = getdate();
  303. declare @then datetime = dateadd(day, -333, @now);
  304.  
  305. insert into At1 (id, stamp) select id, @now from AN;
  306. insert into At1 (id, stamp) select id, @then from AN;
  307. insert into At2 (id, stamp) select id, @now from AN;
  308. insert into At2 (id, stamp) select id, @then from AN;
  309. insert into At3 (id, stamp) select id, @now from AN;
  310. insert into At3 (id, stamp) select id, @then from AN;
  311. insert into At4 (id, stamp) select id, @now from AN;
  312. insert into At4 (id, stamp) select id, @then from AN;
  313. insert into At5 (id, stamp) select id, @now from AN;
  314. insert into At5 (id, stamp) select id, @then from AN;
  315. go
  316.  
  317. SET STATISTICS XML ON;
  318. SET STATISTICS IO,TIME ON;
  319. -------------------------------------------------------------
  320. -- query the view and check if table elimination is in effect
  321. select
  322.     'Anchor id, left joined view',
  323.     avg(cast(AN_id as bigint))
  324. from
  325.     lAN;
  326.  
  327. select
  328.     'Anchor id, CTEs',
  329.     avg(cast(AN_id as bigint))
  330. from
  331.     lAN_CTE;
  332.  
  333.  
  334. ------ 
  335. select
  336.     'AT1 id, left joined view',
  337.     avg(cast(AT1_id as bigint))
  338. from
  339.     lAN
  340. where
  341.     AT1_id is not null;
  342.  
  343. select
  344.     'AT1 id, CTEs',
  345.     avg(cast(AT1_id as bigint))
  346. from
  347.     lAN_CTE
  348. where
  349.     AT1_id is not null;
  350.  
  351. select
  352.     'AT1 id, AT1 stamp, CTEs',
  353.     avg(cast(AT1_id as bigint)),
  354.     min(AT1_stamp)
  355. from
  356.     lAN_CTE
  357. where
  358.     AT1_id is not null;
  359.  
  360. select
  361.     'AT1 id, AT1 stamp, AT1 stamp again, CTEs',
  362.     avg(cast(AT1_id as bigint)),
  363.     min(AT1_stamp),
  364.     min(AT1_stamp_again)
  365. from
  366.     lAN_CTE
  367. where
  368.     AT1_id is not null;
  369.  
  370. ------ 
  371. select
  372.     'AT2 id, left joined view',
  373.     avg(cast(AT2_id as bigint))
  374. from
  375.     lAN
  376. where
  377.     AT2_id is not null;
  378.  
  379. select
  380.     'AT2 id, CTEs',
  381.     avg(cast(AT2_id as bigint))
  382. from
  383.     lAN_CTE
  384. where
  385.     AT2_id is not null;
  386.  
  387. select
  388.     'AT2 id, AT2 stamp, CTEs',
  389.     avg(cast(AT2_id as bigint)),
  390.     min(AT2_stamp)
  391. from
  392.     lAN_CTE
  393. where
  394.     AT2_id is not null;
  395.  
  396. ------ 
  397. select
  398.     'AT3 id, left joined view',
  399.     avg(cast(AT3_id as bigint))
  400. from
  401.     lAN
  402. where
  403.     AT3_id is not null;
  404.  
  405. select
  406.     'AT3 id, CTEs',
  407.     avg(cast(AT3_id as bigint))
  408. from
  409.     lAN_CTE
  410. where
  411.     AT3_id is not null;
  412.  
  413. select
  414.     'AT3 id, AT3 stamp, CTEs',
  415.     avg(cast(AT3_id as bigint)),
  416.     min(AT3_stamp)
  417. from
  418.     lAN_CTE
  419. where
  420.     AT3_id is not null;
  421.  
  422.  
  423. ------ 
  424. select
  425.     'AT4 id, left joined view',
  426.     avg(cast(AT4_id as bigint))
  427. from
  428.     lAN
  429. where
  430.     AT4_id is not null;
  431.  
  432. select
  433.     'AT4 id, CTEs',
  434.     avg(cast(AT4_id as bigint))
  435. from
  436.     lAN_CTE
  437. where
  438.     AT4_id is not null;
  439.  
  440. select
  441.     'AT4 id, AT4 stamp, CTEs',
  442.     avg(cast(AT4_id as bigint)),
  443.     min(AT4_stamp)
  444. from
  445.     lAN_CTE
  446. where
  447.     AT4_id is not null;
  448.  
  449. ------ 
  450. select
  451.     'AT5 id, left joined view',
  452.     avg(cast(AT5_id as bigint))
  453. from
  454.     lAN
  455. where
  456.     AT5_id is not null;
  457.  
  458. select
  459.     'AT5 id, CTEs',
  460.     avg(cast(AT5_id as bigint))
  461. from
  462.     lAN_CTE
  463. where
  464.     AT5_id is not null;
  465.  
  466. select
  467.     'AT5 id, AT5 stamp, CTEs',
  468.     avg(cast(AT5_id as bigint)),
  469.     min(AT5_stamp)
  470. from
  471.     lAN_CTE
  472. where
  473.     AT5_id is not null;
  474. -------------------------------------------------------------
  475. SET STATISTICS XML OFF;
  476. SET STATISTICS IO,TIME OFF;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement