Advertisement
Guest User

Untitled

a guest
Dec 5th, 2014
1,096
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
T-SQL 3.96 KB | None | 0 0
  1. CREATE PROC #CompareStarVsConstant
  2. @NumberOfColumnsInTable INT = 1024,
  3. @BatchIterations INT = 10,
  4. @BatchTimeoutInSeconds INT  = 60,
  5. @NumberOfStarQueries INT = 0 OUTPUT,
  6. @NumberOfConstantQueries INT = 0 OUTPUT,
  7. @NumberOfPKQueries INT = 0 OUTPUT,
  8. @NumberOfColQueries INT = 0 OUTPUT
  9. AS
  10. IF(@NumberOfColumnsInTable NOT BETWEEN  2 AND 1024) OR
  11.    (@BatchIterations < 1) OR
  12.    (@BatchTimeoutInSeconds < 1)
  13. BEGIN
  14. RAISERROR('Invalid Params',16,1)
  15. RETURN
  16. END  
  17.  
  18. SET NOCOUNT ON;
  19.  
  20. IF OBJECT_ID('tempdb..##T') IS NOT NULL
  21.     DROP TABLE ##T
  22.  
  23. SELECT @NumberOfStarQueries = 0, @NumberOfConstantQueries=0, @NumberOfPKQueries=0, @NumberOfColQueries = 0
  24.  
  25. DECLARE @table_create_sql NVARCHAR(MAX)
  26.  
  27. SELECT @table_create_sql = isnull(@table_create_sql,'') + ',C' + LEFT(number,4) + ' INT'
  28. FROM master..spt_values
  29. WHERE type='P' AND
  30.      number BETWEEN 1 AND (@NumberOfColumnsInTable -2)
  31.  
  32. SET @table_create_sql = 'CREATE TABLE ##T (col1 int not null primary key, col2 int not null ' + ISNULL(@table_create_sql,'') + ')'
  33.  
  34. EXEC(@table_create_sql)
  35.  
  36.  
  37. DECLARE @BatchCounter INT = 1,
  38.         @CurrentBatchStarted DATETIME2,
  39.         @BatchTimeoutInMicroSeconds INT = @BatchTimeoutInSeconds * 1000000
  40.  
  41.  
  42. DECLARE @V int /*Holds results of execution to remove effect of results being sent back*/
  43.  
  44. WHILE @BatchCounter <= @BatchIterations
  45. BEGIN
  46.  
  47. SET @CurrentBatchStarted = SYSDATETIME()
  48. WHILE  DATEDIFF(MICROSECOND,@CurrentBatchStarted,SYSDATETIME()) < @BatchTimeoutInMicroSeconds
  49. BEGIN    
  50. SELECT @V=1 WHERE EXISTS (SELECT * FROM ##T) --OPTION(RECOMPILE)
  51. SET @NumberOfStarQueries +=1    
  52. END
  53.  
  54. SET @CurrentBatchStarted = SYSDATETIME()
  55. WHILE  DATEDIFF(MICROSECOND,@CurrentBatchStarted,SYSDATETIME()) < @BatchTimeoutInMicroSeconds
  56. BEGIN    
  57. SELECT @V=1 WHERE EXISTS (SELECT 1 FROM ##T) --OPTION(RECOMPILE)
  58. SET @NumberOfConstantQueries +=1    
  59. END
  60. SET @CurrentBatchStarted = SYSDATETIME()
  61. WHILE  DATEDIFF(MICROSECOND,@CurrentBatchStarted,SYSDATETIME()) < @BatchTimeoutInMicroSeconds
  62. BEGIN    
  63. SELECT @V=1 WHERE EXISTS (SELECT col1 FROM ##T) --OPTION(RECOMPILE)
  64. SET @NumberOfPKQueries +=1    
  65. END
  66.  
  67.  
  68. SET @CurrentBatchStarted = SYSDATETIME()
  69. WHILE  DATEDIFF(MICROSECOND,@CurrentBatchStarted,SYSDATETIME()) < @BatchTimeoutInMicroSeconds
  70. BEGIN    
  71. SELECT @V=1 WHERE EXISTS (SELECT col2 FROM ##T) --OPTION(RECOMPILE)
  72. SET @NumberOfColQueries +=1    
  73. END
  74.  
  75.  
  76. SET @BatchCounter +=1;
  77. END
  78.  
  79. DROP TABLE ##T
  80.  
  81. GO
  82.  
  83.  
  84.  
  85.  
  86. DECLARE @NumberOfStarQueries INT = 0,
  87.         @NumberOfConstantQueries INT = 0,      
  88.         @NumberOfPKQueries INT = 0,
  89.         @NumberOfColQueries INT = 0,
  90.         @BatchIterations INT  = 2,
  91.         @BatchTimeoutInSeconds INT  = 2
  92.  
  93. DECLARE @TestSeconds int = 40*@BatchIterations*@BatchTimeoutInSeconds
  94. DECLARE @SecondsPerBatch float = @BatchIterations*@BatchTimeoutInSeconds
  95.  
  96.  
  97. RAISERROR('Beginning Test, Expected Completion in a little over %d seconds.',0,1,@TestSeconds) WITH NOWAIT        
  98.  
  99. DECLARE @NumberOfColumnsInTable INT = 1024
  100.  
  101. --Do 10 iterations checking effect of column counts from 2 to 1024
  102. WHILE (@NumberOfColumnsInTable >= 2)
  103.     BEGIN
  104.         EXEC #CompareStarVsConstant
  105.             @NumberOfColumnsInTable,
  106.             @BatchIterations,
  107.             @BatchTimeoutInSeconds,
  108.             @NumberOfStarQueries OUTPUT,
  109.             @NumberOfConstantQueries OUTPUT,
  110.             @NumberOfPKQueries OUTPUT,
  111.             @NumberOfColQueries OUTPUT
  112.  
  113.         SELECT @NumberOfColumnsInTable AS [@NumberOfColumnsInTable],    
  114.                @NumberOfStarQueries/@SecondsPerBatch    AS [@NumberOfStarQueries per sec],  
  115.                @NumberOfConstantQueries/@SecondsPerBatch AS [@NumberOfConstantQueries per sec],    
  116.                @NumberOfPKQueries/@SecondsPerBatch    AS [@NumberOfPKQueries per sec],  
  117.                @NumberOfColQueries/@SecondsPerBatch AS [@NumberOfColQueries per sec]
  118.         RAISERROR('',0,1) WITH NOWAIT; /*Flush Buffer so see results sooner in SSMS*/  
  119.         SET @NumberOfColumnsInTable = @NumberOfColumnsInTable/2;
  120.     END
  121.  
  122.  
  123.  
  124. DROP PROC #CompareStarVsConstant
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement