NEERAJPRASADSHARMA

sql server in performance the problem VARCHAR AND RANDOM VAL

May 2nd, 2016
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
T-SQL 4.66 KB | None | 0 0
  1.  
  2. site :https://sqlserverqueryperformance.wordpress.com/2016/04/28/sql-server-in-performance-the-problem/
  3. by NEERAJ PRASAD SHARMA
  4.  
  5.  
  6. -- ID Varchar
  7.  
  8.  
  9. Create table #counter (variablecount int primary key , milliseconds varchar(10))
  10.  
  11. declare @values nvarchar (max)
  12. select  @values=  ('''3279615'',  ''2619534'',  ''2932146'',  ''4953386'',  ''2818224'',  ''5244141'',  ''2321234'',  ''4670287'',  ''3600354'',  ''4307158'',  ''4668756'',  ''4049137'',  ''3037542'',  
  13.  
  14. ''4483522'',  ''3660501'',  ''4426646'',  ''3110987'',  ''2709820'',  ''2818736'',  ''4875826'',  ''2029820'',  ''5467223'',  ''2316672'',  ''2080036'',  ''3781872'',  ''5120161'',  ''4231500'',  ''2885329'',  
  15.  
  16. ''2186636'',  ''4273746'',  ''4469182'',  ''4682959'',  ''3905551'',  ''3206172'',  ''2911743'',  ''4275209'',  ''3789552'',  ''2341547'',  ''2943986'',  ''5229815'',  ''2867115'',  ''2402580'',  ''2943328'',  
  17.  
  18. ''3125357'',  ''4816147'',  ''4219152'',  ''3825625'',  ''3221610'',  ''5052178'',  ''4742174'',  ''2182772'',  ''4934910'',  ''5196299'',  ''3803372'',  ''5041037'',  ''4261499'',  ''5266382'',  ''3667876'',  
  19.  
  20. ''3467411'',  ''2574704'',  ''3941358'',  ''3785855'',  ''4258958'',  ''4565076'',  ''5274231'',  ''3203938'',  ''5329843'',  ''4438383'',  ''2312628'',  ''3248408'',  ''3743514'',  ''4552594'',  ''4109833'',  
  21.  
  22. ''4388393'',  ''5064054'',  ''2054373'',  ''3715411'',  ''3761234'',  ''5470921'',  ''5197951'',  ''4356672'',  ''3309015'',  ''3767928'',  ''2372922'',  ''3858804'',  ''3617027'',  ''3247204'',  ''2105261'',  
  23.  
  24. ''2846260'',  ''3053944'',  ''4398447'',  ''3246406'',  ''4326008'',  ''2506261'',  ''5404760'',  ''2410146'',  ''3578365'',  ''2753024'',  ''3164078'',  ''4087679''')
  25. Declare @dynamicsql nvarchar(max)
  26. declare @loop int ,@LoopCount int
  27. set @loop = 9
  28. Set @LoopCount= 1
  29.  
  30. while (@loop <997)
  31. BEGIN
  32. SET @dynamicsql =
  33. 'declare @Beforeexecutiontime datetime
  34. declare @Aeforeexecutiontime datetime
  35. declare @ID int
  36. declare @IDvarchar varchar(50)
  37. declare @INTCLOUMN int
  38. declare @Name varchar(50)
  39. declare @Age int
  40. declare @Randomvalue bigint
  41. Set @Beforeexecutiontime=getdate()
  42. Select @ID=ID , @IDVARCHAR=IDvarchar ,@INTCLOUMN=intcolumn, @NAME=Name ,@AGE=Age ,@RANDOMVALUE=Randomvalue from TestTable
  43. where IDVARCHAR  in
  44. (' +Convert (Nvarchar(max), left ( @values,@loop))+ ')
  45. option (MAXDOP 1)
  46. SET @Aeforeexecutiontime =getdate()
  47. INSERT INTO #counter
  48. select '+ Convert (Nvarchar(10),@LoopCount)+',datediff (MS ,@Beforeexecutiontime ,@Aeforeexecutiontime)
  49. '
  50.  
  51.  
  52.  
  53. EXEC( @dynamicsql)
  54. set @loop= @loop+12
  55. SET @LoopCount =@LoopCount+1
  56. SET @dynamicsql =''
  57. END
  58.  
  59.  
  60.  
  61.  
  62.  
  63. select * from #counter
  64. order by variablecount
  65.  
  66.  
  67.  
  68.  
  69.  
  70.  
  71.  
  72. --- Random Integer
  73.  
  74. Create table #counter (variablecount int primary key , milliseconds varchar(10))
  75.  
  76. declare @values nvarchar (max)
  77. select  @values=  ('3279615,  2619534,  2932146,  4953386,  2818224,  5244141,  2321234,  4670287,  3600354,  4307158,  4668756,  4049137,  3037542,  4483522,  3660501,  
  78.  
  79. 4426646,  3110987,  2709820,  2818736,  4875826,  2029820,  5467223,  2316672,  2080036,  3781872,  5120161,  4231500,  2885329,  2186636,  4273746,  4469182,  4682959,  
  80.  
  81. 3905551,  3206172,  2911743,  4275209,  3789552,  2341547,  2943986,  5229815,  2867115,  2402580,  2943328,  3125357,  4816147,  4219152,  3825625,  3221610,  5052178,  
  82.  
  83. 4742174,  2182772,  4934910,  5196299,  3803372,  5041037,  4261499,  5266382,  3667876,  3467411,  2574704,  3941358,  3785855,  4258958,  4565076,  5274231,  3203938,  
  84.  
  85. 5329843,  4438383,  2312628,  3248408,  3743514,  4552594,  4109833,  4388393,  5064054,  2054373,  3715411,  3761234,  5470921,  5197951,  4356672,  3309015,  3767928,  
  86.  
  87. 2372922,  3858804,  3617027,  3247204,  2105261,  2846260,  3053944,  4398447,  3246406,  4326008,  2506261,  5404760,  2410146,  3578365,  2753024,  3164078,  4087679')
  88. Declare @dynamicsql nvarchar(max)
  89. declare @loop int ,@LoopCount int
  90. set @loop = 7
  91. Set @LoopCount= 1
  92.  
  93. while (@loop <997)
  94. BEGIN
  95. SET @dynamicsql =
  96. 'declare @Beforeexecutiontime datetime
  97. declare @Aeforeexecutiontime datetime
  98. declare @ID int
  99. declare @IDvarchar varchar(50)
  100. declare @INTCLOUMN int
  101. declare @Name varchar(50)
  102. declare @Age int
  103. declare @Randomvalue bigint
  104. Set @Beforeexecutiontime=getdate()
  105. Select @ID=ID , @IDVARCHAR=IDvarchar ,@INTCLOUMN=intcolumn, @NAME=Name ,@AGE=Age ,@RANDOMVALUE=Randomvalue from TestTable
  106. where Randomvalue in
  107. (' +Convert (Nvarchar(max), left ( @values,@loop))+ ')
  108. option (MAXDOP 1)
  109. SET @Aeforeexecutiontime =getdate()
  110. INSERT INTO #counter
  111. select '+ Convert (Nvarchar(10),@LoopCount)+',datediff (MS ,@Beforeexecutiontime ,@Aeforeexecutiontime)
  112. '
  113.  
  114.  
  115. EXEC( @dynamicsql)
  116. set @loop= @loop+10
  117. SET @LoopCount =@LoopCount+1
  118. SET @dynamicsql =''
  119. END
Add Comment
Please, Sign In to add comment