<< >>

2009-07-02 [長年日記]

RDB終了のお知らせ

  • CouchDB「データストレージのデファクトの看板は今日限りおろしてもらう」
    • RDB「おまえにゃ無理だCouchDB」
  • CouchDB「俺じゃない。うちのMongoDBがやる」

[MongoDB] MongoDB : C++ で書かれた高速なドキュメント指向DB

Install

2009.07現在のversionは0.9.5。

% wget http://downloads.mongodb.org/linux/mongodb-linux-i686-0.9.5.tgz
% tar zxvf mongodb-linux-i686-0.9.5.tgz
% ls bin
mongo  mongod  mongodump  mongoexport  mongofiles  mongoimportjson  mongorestore  mongos

Server

データの格納場所は "--dbpath" オプションで指定可能。(デフォルトは /data/db/)

% mkdir /data/mongodb
% ./bin/mongod --dbpath /data/mongodb run &

Client

% ./bin/mongo
> db.aho.save({name : 'maiha'})
> db.aho.findOne()
{"_id" : "4a4bf95e61f0f5db8e14d6ea" , "name" : "maiha"}

first class object

> db.aho.save
function (obj) {
    if (typeof obj._id == "undefined") {
        obj._id = new ObjectId;
        return this.insert(obj);
    } else {
        return this.update({_id:obj._id}, obj, true);
    }
}
> db.aho.save = function(obj) { print("aho\n"); }
> db.aho.save({name:'maiha'})
aho

ちょw保存できなくなったww
first class objectでソース見れるとか楽し杉!

bench.rb

require 'rubygems'
 
######################################################################
### ActiveRecord (PostgreSQL)
 
require 'active_record'
ActiveRecord::Base.establish_connection(:adapter=>"postgresql", :database=>"jpop", :user=>"maiha")
module PG
  class Song < ActiveRecord::Base
    # Indexes: "index_songs_singer" btree (singer)
  end
end
# p PG::Song.count # => 64482
 
######################################################################
### MongoRecord (MongoDB)
 
require 'mongo'
require 'mongo_record'
MongoRecord::Base.connection = XGen::Mongo::Driver::Mongo.new.db('jpop')
module MD
  class Song < MongoRecord::Base
    collection_name :songs
    fields :code, :name, :singer, :word, :music, :intro, :tieup, :body, :created_at
    index :singer
  end
end
# p MD::Song.count # => 64482
 
######################################################################
### Bench
 
require 'rbench'
 
puts "Bench1: using index"
[10,100,1000].each do |times|
  puts "#{times} times:"
  RBench.run(times) do
    report("PostgreSQL") { PG::Song.count({:conditions => {:singer => "℃-ute" }}) } # matches 19
    report("MongoDB   ") { MD::Song.count({:conditions => {:singer => "℃-ute" }}) } # matches 19
  end
  puts ""
end
 
puts "Bench2: using seq scan"
[1,10,100].each do |times|
  puts "#{times} times:"
  RBench.run(times) do
    report("PostgreSQL") { PG::Song.count(:conditions => {:body => "LOVE"}) } # matches 0
    report("MongoDB   ") { MD::Song.count(:conditions => {:body => "LOVE"}) } # matches 0
  end
  puts ""
end

puts "Bench3: using seq scan with regexp"
[1,10,100].each do |times|
  puts "#{times} times:"
  RBench.run(times) do
    report("PostgreSQL") { PG::Song.count(:conditions => ["body ~ '大きな愛で'"]) } # matches 20
    report("MongoDB   ") { MD::Song.count(:conditions => {:body => /大きな愛で/}) } # matches 20
  end
  puts ""
end

Result

Bench1: using index
10 times:
                   Results |
----------------------------
PostgreSQL           0.028 |
MongoDB              0.012 |

100 times:
                   Results |
----------------------------
PostgreSQL           0.080 |
MongoDB              0.126 |

1000 times:
                   Results |
----------------------------
PostgreSQL           0.732 |
MongoDB              1.157 |

Bench2: using seq scan
1 times:
                   Results |
----------------------------
PostgreSQL           0.304 |
MongoDB              0.063 |

10 times:
                   Results |
----------------------------
PostgreSQL           1.621 |
MongoDB              0.600 |

100 times:
                   Results |
----------------------------
PostgreSQL          16.485 |
MongoDB              5.198 |

Bench3: using seq scan with regexp
1 times:
                   Results |
----------------------------
PostgreSQL           1.855 |
MongoDB              0.584 |

10 times:
                   Results |
----------------------------
PostgreSQL          11.012 |
MongoDB              4.928 |

100 times:
                   Results |
----------------------------
PostgreSQL          72.217 |
MongoDB             52.005 |

俺、この仕事が終わったら RDBもARも捨てて、これからは Ruby object を直接 MongoDB に突っ込むんだ。

参考