为什么MongoDB插入数据慢?

发布于 2024-12-21 14:08:37 字数 1584 浏览 0 评论 0原文

我正在尝试将解析后的数据保存到 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

I'm trying to save the parsed data into MongoDB. I want to store every referrer URL. At present it is taking a lot of time to insert into 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一张白纸 2024-12-28 14:08:37

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

据我所知,唯一可能变慢的地方是 ReferrerDocument.last 调用。

该集合中有多少文档?有支持索引吗?
ReferrerDocument.last 转换为什么 mongodb 查询(您可以在development.log 中看到。我自己是 mongoid< /a> 用户)?
该查询的解释计划是什么样的?

为了使此查询最有效,您需要在 {website_url: 1,referrer_url: 1, _id: 1} 上建立索引

Did you try to actually measure how long each part takes to execute?

From what I see, the only place that could possibly be slow is ReferrerDocument.last invocation.

How many documents you have in this collection? Do you have supporting indexes?
What mongodb query does ReferrerDocument.last translate to (you can see in development.log. I myself am a mongoid user)?
What does explain plan for this query look like?

To make this query most efficient you'll need an index on {website_url: 1, referrer_url: 1, _id: 1}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文