为什么MongoDB会慢插入数据?

时间:2011-12-17 10:22:31

标签: ruby-on-rails ruby mongodb mongomapper

我正在尝试将解析后的数据保存到MongoDB中。我想存储每个引荐来源网址。目前,它需要花费大量时间插入MongoDB。

  class ReferrerDocument
    include MongoMapper::Document
    key :website_url, String
    key :referrer_url, String
    key :full_referrer_url, String
    key :count_of_appearance, Integer, :default => 1
    key :current_time, Time
    timestamps!

    def self.save_referrer_info(website_url, referrer_url, full_referrer_url, current_time)
      referrer_document = ReferrerDocument.last(:website_url => website_url,
                                                :referrer_url => referrer_url,
                                                :order => :id.asc)
      if(referrer_document.nil?)
        ReferrerDocument.create(:website_url => website_url,
                                :referrer_url => referrer_url,
                                :full_referrer_url => full_referrer_url,
                                :last_seen_at => 0,
                                :current_time => current_time)
      else
        count = referrer_document.count_of_appearance += 1
        last_seen_at = referrer_document.current_time.utc
        ReferrerDocument.create(:website_url => website_url,
                                :referrer_url => referrer_url,
                                :full_referrer_url => full_referrer_url,
                                :last_seen_at => last_seen_at,
                                :current_time => current_time,
                                :count_of_appearance => count)
      end
    end
end

1 个答案:

答案 0 :(得分:0)

您是否尝试实际测量每个部分执行的时间?

从我看到的,唯一可能很慢的地方是ReferrerDocument.last调用。

此系列中有多少份文件?你有支持索引吗? ReferrerDocument.last转换为什么mongodb查询(您可以在development.log中看到。我自己是mongoid用户)? 解释这个查询的计划是什么样的?

为了使此查询最有效,您需要{website_url: 1, referrer_url: 1, _id: 1}

上的索引