为什么MongoDB插入数据慢?
我正在尝试将解析后的数据保存到 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您是否尝试实际测量每个部分执行所需的时间?
据我所知,唯一可能变慢的地方是
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}