Advertisement
anchormodeling

Statistics for bit columns

Feb 2nd, 2012
261
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
T-SQL 6.09 KB | None | 0 0
  1. -- Desired join order: D, C or E, B or F, A or G
  2.  
  3. /*
  4. drop table Bits;
  5. drop table BitLookup;
  6. drop table Tinyints;
  7. drop table TinyintLookup;
  8. */
  9.  
  10. ----- Setup the Tables used in the Testing -----
  11.  
  12. set nocount on;
  13.  
  14. create table BitLookup (
  15.     X bit not null primary key,
  16.     V char(5) not null unique
  17. );
  18.  
  19. create table TinyintLookup (
  20.     X tinyint not null primary key,
  21.     V char(5) not null unique
  22. );
  23.  
  24. insert into BitLookup
  25. output  inserted.X, inserted.V
  26. into    TinyintLookup
  27. values  (0, 'False');
  28.  
  29. insert into BitLookup
  30. output  inserted.X, inserted.V
  31. into    TinyintLookup
  32. values  (1, 'True');
  33.    
  34. create table Bits (
  35.     I int not null primary key,
  36.     A bit not null references BitLookup(X),
  37.     B bit not null references BitLookup(X),
  38.     C bit not null references BitLookup(X),
  39.     D bit not null references BitLookup(X),
  40.     E bit not null references BitLookup(X),
  41.     F bit not null references BitLookup(X),
  42.     G bit not null references BitLookup(X),
  43.     Z char(42) not null
  44. );
  45.  
  46. create table Tinyints (
  47.     I int not null primary key,
  48.     A tinyint not null,
  49.     B tinyint not null,
  50.     C tinyint not null,
  51.     D tinyint not null,
  52.     E tinyint not null,
  53.     F tinyint not null,
  54.     G tinyint not null,
  55.     Z char(42) not null
  56. );
  57.  
  58. with g(n) as
  59. (
  60.     select  1
  61.     union all
  62.     select  n + 1
  63.     from    g
  64.     where   n < 1000000 -- 1 million rows
  65. )
  66. insert into Bits
  67. output
  68.     inserted.I, inserted.A, inserted.B,
  69.     inserted.C, inserted.D, inserted.E,
  70.     inserted.F, inserted.G, inserted.Z
  71. into
  72.     Tinyints   
  73. select
  74.     n,
  75.     n % 2,
  76.     n % 5,
  77.     n % 100,
  78.     n % 10000, -- joining D first is best
  79.     n % 100,
  80.     n % 5,
  81.     n % 2,
  82.     replicate('.xX^Xx.', 6)
  83. from
  84.     g
  85. option(maxrecursion 0);
  86.  
  87. alter table Tinyints add
  88. foreign key (A) references TinyintLookup(X),
  89. foreign key (B) references TinyintLookup(X),
  90. foreign key (C) references TinyintLookup(X),
  91. foreign key (D) references TinyintLookup(X),
  92. foreign key (E) references TinyintLookup(X),
  93. foreign key (F) references TinyintLookup(X),
  94. foreign key (G) references TinyintLookup(X);
  95.  
  96. select @@version;
  97.  
  98. ------ Execution Plan Testing Starts Here ------
  99.  
  100. declare @iteration int = 0;
  101. -- 0 = cold state
  102. -- 1 = warm state
  103. while(@iteration <= 1)
  104. begin
  105.     if(@iteration = 0)
  106.     begin
  107.         print '
  108.         --- COLD STATE ---';
  109.         checkpoint;
  110.         dbcc dropcleanbuffers with NO_INFOMSGS;
  111.         dbcc freeproccache with NO_INFOMSGS;
  112.     end
  113.     else
  114.         print '
  115.         --- WARM STATE ---';
  116.  
  117.     print '
  118.     Bits: Using lookup values
  119.     ';
  120.     set statistics profile on;
  121.     set statistics io on;
  122.     set statistics time on;
  123.  
  124.     select count(*)
  125.     from Bits t
  126.     join BitLookup a on a.X = t.A
  127.     join BitLookup b on b.X = t.B
  128.     join BitLookup c on c.X = t.C
  129.     join BitLookup d on d.X = t.D
  130.     join BitLookup e on e.X = t.E
  131.     join BitLookup f on f.X = t.F
  132.     join BitLookup g on g.X = t.G
  133.     where
  134.         a.V = 'False'
  135.     and b.V = 'False'
  136.     and c.V = 'False'
  137.     and d.V = 'False'
  138.     and e.V = 'False'
  139.     and f.V = 'False'
  140.     and g.V = 'False';
  141.  
  142.     set statistics time off;
  143.     set statistics io off;
  144.     set statistics profile off;
  145.  
  146.     print '
  147.     Tinyints: Using lookup values
  148.     ';
  149.     set statistics profile on;
  150.     set statistics io on;
  151.     set statistics time on;
  152.  
  153.     select count(*)
  154.     from Tinyints t
  155.     join TinyintLookup a on a.X = t.A
  156.     join TinyintLookup b on b.X = t.B
  157.     join TinyintLookup c on c.X = t.C
  158.     join TinyintLookup d on d.X = t.D
  159.     join TinyintLookup e on e.X = t.E
  160.     join TinyintLookup f on f.X = t.F
  161.     join TinyintLookup g on g.X = t.G
  162.     where
  163.         a.V = 'False'
  164.     and b.V = 'False'
  165.     and c.V = 'False'
  166.     and d.V = 'False'
  167.     and e.V = 'False'
  168.     and f.V = 'False'
  169.     and g.V = 'False';
  170.  
  171.     set statistics time off;
  172.     set statistics io off;
  173.     set statistics profile off;
  174.  
  175.     if(@iteration = 0)
  176.     begin
  177.         checkpoint;
  178.         dbcc dropcleanbuffers with NO_INFOMSGS;
  179.         dbcc freeproccache with NO_INFOMSGS;
  180.     end
  181.  
  182.     print '
  183.     Bits: Using key values (preprocessing)
  184.     ';
  185.     declare @BitKey bit = (
  186.         select X from BitLookup where V = 'False'
  187.     );
  188.     set statistics profile on;
  189.     set statistics io on;
  190.     set statistics time on;
  191.  
  192.     select count(*)
  193.     from Bits t
  194.     join BitLookup a on a.X = t.A
  195.     join BitLookup b on b.X = t.B
  196.     join BitLookup c on c.X = t.C
  197.     join BitLookup d on d.X = t.D
  198.     join BitLookup e on e.X = t.E
  199.     join BitLookup f on f.X = t.F
  200.     join BitLookup g on g.X = t.G
  201.     where
  202.         t.A = @BitKey
  203.     and t.B = @BitKey
  204.     and t.C = @BitKey
  205.     and t.D = @BitKey
  206.     and t.E = @BitKey
  207.     and t.F = @BitKey
  208.     and t.G = @BitKey;
  209.  
  210.     set statistics time off;
  211.     set statistics io off;
  212.     set statistics profile off;
  213.  
  214.     print '
  215.     Tinyints: Using key values (preprocessing)
  216.     ';
  217.     declare @TinyintKey tinyint = (
  218.         select X from TinyintLookup where V = 'False'
  219.     );
  220.     set statistics profile on;
  221.     set statistics io on;
  222.     set statistics time on;
  223.  
  224.     select count(*)
  225.     from Tinyints t
  226.     join TinyintLookup a on a.X = t.A
  227.     join TinyintLookup b on b.X = t.B
  228.     join TinyintLookup c on c.X = t.C
  229.     join TinyintLookup d on d.X = t.D
  230.     join TinyintLookup e on e.X = t.E
  231.     join TinyintLookup f on f.X = t.F
  232.     join TinyintLookup g on g.X = t.G
  233.     where
  234.         t.A = @TinyintKey
  235.     and t.B = @TinyintKey
  236.     and t.C = @TinyintKey
  237.     and t.D = @TinyintKey
  238.     and t.E = @TinyintKey
  239.     and t.F = @TinyintKey
  240.     and t.G = @TinyintKey;
  241.        
  242.     set statistics time off;
  243.     set statistics io off;
  244.     set statistics profile off;
  245.  
  246.     set @iteration = @iteration + 1;
  247. end
  248.  
  249. -- stats have been automatically generated for the bit columns
  250. dbcc SHOW_STATISTICS ('Bits', 'A') with NO_INFOMSGS;
  251. dbcc SHOW_STATISTICS ('Bits', 'B') with NO_INFOMSGS;
  252. dbcc SHOW_STATISTICS ('Bits', 'C') with NO_INFOMSGS;
  253. dbcc SHOW_STATISTICS ('Bits', 'D') with NO_INFOMSGS;
  254. dbcc SHOW_STATISTICS ('Bits', 'E') with NO_INFOMSGS;
  255. dbcc SHOW_STATISTICS ('Bits', 'F') with NO_INFOMSGS;
  256. dbcc SHOW_STATISTICS ('Bits', 'G') with NO_INFOMSGS;
  257.  
  258. -- stats have been automatically generated for the tinyint columns
  259. dbcc SHOW_STATISTICS ('Tinyints', 'A') with NO_INFOMSGS;
  260. dbcc SHOW_STATISTICS ('Tinyints', 'B') with NO_INFOMSGS;
  261. dbcc SHOW_STATISTICS ('Tinyints', 'C') with NO_INFOMSGS;
  262. dbcc SHOW_STATISTICS ('Tinyints', 'D') with NO_INFOMSGS;
  263. dbcc SHOW_STATISTICS ('Tinyints', 'E') with NO_INFOMSGS;
  264. dbcc SHOW_STATISTICS ('Tinyints', 'F') with NO_INFOMSGS;
  265. dbcc SHOW_STATISTICS ('Tinyints', 'G') with NO_INFOMSGS;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement