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: counting records 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: counting records 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 __END__ Bench1: counting records 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: counting records 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 |