вторник, 13 октября 2009 г.

MongoDB VS MySQL


Just insert test.


  1. #!/usr/bin/ruby



  2. require 'rubygems'

  3. require 'benchmark'



  4. require 'mongo'

  5. require 'mysql'



  6. # init benchmark cycle

  7. def cycle

  8. 10000.times{ |i|

  9. doc = {

  10. "text" => "i am any text and a little tail with me: #{i}",

  11. "count" => i,

  12. "coords" => {"x" => 203 + i, "y" => 102 + i}

  13. }



  14. yield doc

  15. }

  16. end



  17. # init mysql

  18. mysql = Mysql.new('localhost', 'testuser', 'testpass', 'test')

  19. mysql.query("DROP TABLE IF EXISTS `insert_bm`")

  20. mysql.query("DROP TABLE IF EXISTS `insert_bm_coords`")



  21. mysql.query("CREATE TABLE `insert_bm_coords`(

  22. `id` int unsigned not null auto_increment,

  23. `x` int not null,

  24. `y` int not null,

  25. PRIMARY KEY (`id`)

  26. )")



  27. mysql.query("CREATE TABLE `insert_bm`(

  28. `id` int unsigned not null auto_increment,

  29. `text` varchar(255) not null,

  30. `count` int not null,

  31. `coords_id` int unsigned not null,

  32. PRIMARY KEY (`id`)

  33. )")



  34. # init mongodb

  35. mongo_db = Mongo::Connection.new.db("ololo_db")

  36. mongo_db.collection("beta").drop

  37. mongo = mongo_db.collection("beta")



  38. # do benchmarking

  39. Benchmark.bm {|x|



  40. x.report("mysql:") {

  41. cycle{|doc|

  42. mysql.query("INSERT insert_bm_coords(x,y) VALUES(#{doc["coords"]["x"]}, #{doc["coords"]["y"]})")

  43. mysql.query("INSERT insert_bm(text, count, coords_id) VALUES('#{doc["text"]}', #{doc["count"]}, #{mysql.insert_id()})")

  44. }

  45. }



  46. x.report("mongodb:") {

  47. cycle{|doc|

  48. mongo.insert(doc)

  49. }

  50. }

  51. }





Result:

10_000:
user system total real
mysql: 0.300000 0.150000 0.450000 ( 1.763819)
mongodb: 5.690000 0.680000 6.370000 ( 7.201129)

1_000_000:
user system total real
mysql: 30.960000 12.950000 43.910000 (179.318003)
mongodb:569.650000 18.410000 588.060000 (678.889538)

UPDATE: after installing C extension (`gem install mongo_ext`).


10_000:
user system total real
mysql: 0.280000 0.110000 0.390000 ( 1.679100)
mongodb: 3.110000 0.270000 3.380000 ( 4.089022)

1_000_000:
user system total real
mysql: 32.400000 13.030000 45.430000 (179.684186)
mongodb:326.150000 17.000000 343.150000 (423.631974)


I'm confused. Can anybody explain it?

Что-то тут не так, учитывая заявления о скорости MongoDB.
Кто-то может подтвердить или опровергнуть?

UPDATE: here is explanation: http://blog.knopkodav.ru/2009/11/mongo-mysql-ruby-and-php.html