Advertisement
IvanovDmitriy

Test: SELECT VS SET + Multi Statement

Jul 19th, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.65 KB | None | 0 0
  1. DECLARE @Test1 int, @Test2 int, @Test3 int, @TestVar1 int, @TestVar2 int
  2. DECLARE @Loop int, @Start datetime, @CTR int, @TimesToLoop1 int, @TimesToLoop2 int
  3.  
  4. SET @Test1 = 0
  5. SET @Test2 = 0
  6. SET @Test3 = 0
  7. SET @Loop = 0
  8. SET @TestVar2 = 0
  9. SET @TimesToLoop1 = 10
  10. SET @TimesToLoop2 = 50000
  11. WHILE @Loop < @TimesToLoop1
  12. BEGIN
  13. SET @Start = CURRENT_TIMESTAMP
  14. SET @CTR = 0
  15.  
  16. /* Testing the performance of SET */
  17. WHILE @CTR < @TimesToLoop2
  18. BEGIN
  19. SET @TestVar1 = 1
  20. SET @TestVar2 = @TestVar2 - @TestVar1
  21. SET @CTR = @CTR + 1
  22. END
  23.  
  24. SET @Loop = @Loop + 1
  25. SET @Test1 = @Test1 + DATEDIFF(ms, @Start, CURRENT_TIMESTAMP)
  26. END
  27.  
  28. SET @Loop = 0
  29. SET @TestVar2 = 0
  30. WHILE @Loop < @TimesToLoop1
  31. BEGIN
  32. SELECT @Start = CURRENT_TIMESTAMP
  33. SELECT @CTR = 0
  34.  
  35. /* Testing the performance of SELECT */
  36. WHILE @CTR < @TimesToLoop2
  37. BEGIN
  38. SELECT @TestVar1 = 1
  39. SELECT @TestVar2 = @TestVar2 - @TestVar1
  40. SELECT @CTR = @CTR + 1
  41. END
  42.  
  43. SELECT @Loop = @Loop + 1
  44. SELECT @Test2 = @Test2 + DATEDIFF(ms, @Start, CURRENT_TIMESTAMP)
  45. END
  46.  
  47. SET @Loop = 0
  48. SET @TestVar2 = 0
  49. WHILE @Loop < @TimesToLoop1
  50. BEGIN
  51. SELECT @Start = CURRENT_TIMESTAMP, @CTR = 0
  52.  
  53. /* Testing the performance of SELECT with multiple variable assignments */
  54. WHILE @CTR < @TimesToLoop2
  55. BEGIN
  56. SELECT @TestVar1 = 1, @TestVar2 = @TestVar2 - @TestVar1, @CTR = @CTR + 1
  57. END
  58.  
  59. SELECT @Loop = @Loop + 1, @Test3 = @Test3 + DATEDIFF(ms, @Start, CURRENT_TIMESTAMP)
  60. END
  61.  
  62. SELECT (@Test1/CAST(@TimesToLoop1 AS decimal(7,2)))/1000.00 AS [SET],
  63. (@Test2/CAST(@TimesToLoop1 AS decimal(7,2)))/1000.00 AS [SELECT],
  64. (@Test3/CAST(@TimesToLoop1 AS decimal(7,2)))/1000.00 AS [SELECT with Multiple Assignments]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement