Advertisement
Guest User

Untitled

a guest
Apr 11th, 2017
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.93 KB | None | 0 0
  1. require 'tiny_tds'
  2. @client = TinyTds::Client.new username: 'sa', password: 'password',
  3. host: 'localhost', port: 1433
  4.  
  5. # time difference in milliseconds
  6. def time_diff_milli(start, finish)
  7. (finish - start) * 1000.0
  8. end
  9.  
  10. def execute(sql)
  11. @client.execute(sql).do
  12. true
  13. end
  14.  
  15. # create database
  16. puts "Dropping and creating database 'SampleDB'"
  17. execute("DROP DATABASE IF EXISTS [SampleDB]; CREATE DATABASE [SampleDB];")
  18.  
  19. # Insert 5 million rows into the table 'Table_with_5M_rows'
  20. puts "Inserting 5 million rows into table 'Table_with_5M_rows'. This takes ~1 minute, please wait."
  21. execute("USE SampleDB; WITH a AS (SELECT * FROM (VALUES(1),(2),(3),(4),(5),(6),(7),(8),(9),(10)) AS a(a))
  22. SELECT TOP(5000000)
  23. ROW_NUMBER() OVER (ORDER BY a.a) AS OrderItemId
  24. ,a.a + b.a + c.a + d.a + e.a + f.a + g.a + h.a AS OrderId
  25. ,a.a * 10 AS Price
  26. ,CONCAT(a.a, N' ', b.a, N' ', c.a, N' ', d.a, N' ', e.a, N' ', f.a, N' ', g.a, N' ', h.a) AS ProductName
  27. INTO Table_with_5M_rows
  28. FROM a, a AS b, a AS c, a AS d, a AS e, a AS f, a AS g, a AS h;")
  29.  
  30. # execute query without columnstore index
  31. t1 = Time.now
  32. execute("SELECT SUM(Price) as sum FROM Table_with_5M_rows")
  33. t2 = Time.now
  34. elapsedTimeWithoutIndex = time_diff_milli t1, t2
  35. puts "Query time without columnstore index: #{elapsedTimeWithoutIndex}"
  36.  
  37. # Create columnnstore index on table 'Table_with_5M_rows'
  38. puts("Adding a columnstore index to table 'Table_with_5M_rows'")
  39. execute("CREATE CLUSTERED COLUMNSTORE INDEX columnstoreindex ON Table_with_5M_rows;")
  40.  
  41. # execute the same query with columnstore index
  42. t3 = Time.now
  43. execute("SELECT SUM(Price) as sum FROM Table_with_5M_rows")
  44. t4 = Time.now
  45. elapsedTimeWithIndex = time_diff_milli t3, t4
  46. puts "Query time WITH columnstore index: #{elapsedTimeWithIndex}"
  47.  
  48. # calculate performance improvement
  49. perf_improvement = (elapsedTimeWithoutIndex / elapsedTimeWithIndex)
  50. puts "Performance improvement with columnstore index: #{perf_improvement}"
  51.  
  52. @client.close
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement