2012年5月5日土曜日

Macにmongodbをインストールしたときのメモ

■ Homebrewで楽々インストール

brew install mongodb

mkdir -p ~/dev/mongo/data

# 起動
mongod --dbpath=~/dev/mongo/data

# クライアント起動
mongo


■ クライアントでいくつかのクエリを実行

db.UserReport.insert({
  name: 'ruffy',
  pt: 1000,
  crystal: {
    big: 0,
    small: 300
  },
});
db.UserReport.insert({
  name: 'zoro',
  pt: 2000,
  crystal: {
    big: 45,
    small: 600
  },
});
db.UserReport.insert({
  name: 'nami',
  pt: 3000,
  crystal: {
    big: 40,
    small: 400
  },
});
db.UserReport.insert({
  name: 'sanji',
  pt: 4000,
  crystal: {
    big: 20,
    small: 100
  },
});
db.UserReport.insert({
  name: 'usopp',
  pt: 5000,
  crystal: {
    big: 30,
    small: 0
  },
});


// 全件取得
db.UserReport.find()

// where pt = 1000
db.UserReport.find({pt: 1000})

// 多次元(組み込みobject)の値で検索
db.UserReport.find({"crystal.small": 100})
db.UserReport.find({"crystal.small": {$lt: 300}}).sort({pt: -1})

// 全件削除
db.UserReport.remove()
やっぱ運用の評判は悪くても、mongoがいいねヽ(・∀・)ノ

■クエリはこちらを参考に。
クエリー - Docs-Japanese - 10gen Confluence


■Mongoのベンチマークとってみた。

・gem
sudo gem install mongo bson bson_ext

・ソース
#!/usr/bin/env ruby
require 'rubygems'
require 'mongo'
require 'benchmark'

# ---------------------------------------
# Insert / Select 性能
# ---------------------------------------
count = 100000
p 'count:' + count.to_s
 
# ---------------------------------------
# mongodb
m = Mongo::Connection.new('localhost', 27017)
db = m.db('testdb')

row = {
  :user_id => 0,
  :name => 'name0',
  :created_at => Time.now.to_i
}
Benchmark.bm { |rep|
  rep.report("mongodb insert ") {
    for num in 1..count
      db = m.db('testdb')
      name = 'name' + num.to_s
      row[:user_id] = num
      row[:name] = name
      db['user'].insert(row)
    end
  }
}
Benchmark.bm { |rep|
  rep.report("mongodb select") {
    for num in 1..count
      id = rand(count).to_i + 1
      db['user'].find({:user_id => id})
    end
  }
}

・結果
mongod --version
db version v2.0.4, pdfile version 4.5

# MongoDB
"count:100000"
      user     system      total        real
mongodb insert  13.430000   1.030000  14.460000 ( 14.457051)
      user     system      total        real
mongodb select  4.680000   0.070000   4.750000 (  4.768458)

# 過去の結果
# MySQLはInnoDB
"count:100000"
      user     system      total        real
mysql(o/r) insert  74.320000   5.670000  79.990000 (110.039184)
      user     system      total        real
mysql(o/r) select 50.610000   2.650000  53.260000 ( 65.465168)
      user     system      total        real
mysql(sql) insert   4.430000   1.530000   5.960000 ( 29.590552)
      user     system      total        real
mysql(sql) select  5.470000   1.780000   7.250000 ( 13.550391)
      user     system      total        real
cassandra insert  47.180000   3.140000  50.320000 ( 61.347904)
      user     system      total        real
cassandra select  69.150000   3.470000  72.620000 (111.568860)
      user     system      total        real
memcache insert  14.930000   2.280000  17.210000 ( 17.935214)
      user     system      total        real
memcache select  16.710000   2.080000  18.790000 ( 19.264883)
      user     system      total        real
handlersocket insert   1.430000   1.510000   2.940000 ( 26.721995)
      user     system      total        real
handlersocket select   1.320000   1.520000   2.840000 (  7.867557)

# MySQLはSpider
"count:100000"
      user     system      total        real
mysql(o/r) insert 101.200000   7.280000 108.480000 (341.227877)
      user     system      total        real
mysql(o/r) select 71.910000   3.450000  75.360000 (294.436478)
      user     system      total        real
mysql(sql) insert   9.000000   2.600000  11.600000 (235.837247)
      user     system      total        real
mysql(sql) select  8.750000   2.700000  11.450000 (245.967628)

# 前提として、1台のmacで行っているため、スケールアウトのメリットを享受できていません。
# そのため、Cassandra やhbaseでは期待する効果が出ていません!!


■ その他のベンチマーク詳細
Macのsandboxを使ってspiderを動かしてみたメモ
MacのMySQLでHandlerSocketのベンチマークをとった
※すべてMac1台で行っているので、適切にスケールの効果が見られないものもあります。Spiderとかね。

0 件のコメント:

コメントを投稿