【问题标题】:Mongo Mapper & Rails - Queries suddenly taking > 100 msMongo Mapper & Rails - 查询突然超过 100 毫秒
【发布时间】:2013-07-28 02:41:41
【问题描述】:

我发誓,mongo 中的查询在一小时内从每行 0.3 毫秒增加到每行 > 100 毫秒。我在那段时间内向 mongo 添加了零数据,并且我没有对数据库、gem 文件或 rails 控制器进行任何更改 - 只是更改了视图中的一些 CSS。没有与数据库对话。

这是一些示例输出:

MONGODB (121.1ms) creative_development['admins'].find({:_id=>BSON::ObjectId('518b762e89651a0389000013')}).limit(-1)
MONGODB (121.6ms) creative_development['$cmd'].find({"count"=>"customers", "query"=>{:merchant_id=>"518b762e89651a0389000013"}, "fields"=>nil}).limit(-1)
MONGODB (249.7ms) creative_development['customers'].find({:merchant_id=>"518b762e89651a0389000013"}).limit(40).sort([["first_name", "asc"]])
MONGODB (244.8ms) creative_development['customers'].find({:merchant_id=>"518b762e89651a0389000013"}).limit(40).sort([["first_name", "asc"]])
  Rendered shared/_header.html.erb (0.2ms)
MONGODB (121.5ms) creative_development['companies'].find({:_id=>"0"}).limit(-1)
MONGODB (131.2ms) creative_development['jobs'].find({:customer_id=>BSON::ObjectId('51b7890a9727912430000022')})
MONGODB (122.1ms) creative_development['jobs'].find({:customer_id=>BSON::ObjectId('51b7890a9727912430000022')})
MONGODB (120.9ms) creative_development['companies'].find({:_id=>"0"}).limit(-1)

这是之前的样子:

MONGODB (0.4ms) creative_development['customers'].find({:_id=>BSON::ObjectId('51ddf5969727913cf8000424')}).limit(-1)
MONGODB (0.4ms) creative_development['admins'].find({:_id=>BSON::ObjectId('51ddbd6b972791243d0000f1')}).limit(-1)
MONGODB (0.4ms) creative_development['jobtypes'].find({:_id=>BSON::ObjectId('51ddcff69727912dfe000001')}).limit(-1)
MONGODB (0.4ms) creative_development['jobpriorities'].find({:_id=>BSON::ObjectId('51a7d01289651a3b100000e5')}).limit(-1)
MONGODB (0.4ms) creative_development['customers'].find({:_id=>BSON::ObjectId('51e4a6c797279171bc000004')}).limit(-1)
MONGODB (0.5ms) creative_development['admins'].find({:_id=>BSON::ObjectId('51ddbd6b972791243d0000f1')}).limit(-1)
MONGODB (0.4ms) creative_development['jobtypes'].find({:_id=>BSON::ObjectId('519bff2e89651a32af000001')}).limit(-1)
MONGODB (0.4ms) creative_development['jobpriorities'].find({:_id=>BSON::ObjectId('51b782db972791212c0000e6')}).limit(-1)
MONGODB (0.4ms) creative_development['customers'].find({:_id=>BSON::ObjectId('51e7448997279123e6000001')}).limit(-1)
MONGODB (0.3ms) creative_development['admins'].find({:_id=>BSON::ObjectId('51ddbd6b972791243d0000f1')}).limit(-1)
MONGODB (0.3ms) creative_development['jobtypes'].find({:_id=>BSON::ObjectId('519bff1389651a32d800002b')}).limit(-1)

查询:

@all_customers = Customer.where(:merchant_id => session[:admin_id].to_s)

respond_to do |format|
        format.html do # index.html.erb
          @all_customers = @all_customers.sort([sort_column,sort_direction]).paginate(:per_page => 40, :page => params[:page])
        end      

排序部分:

  def sort_column
    Customer.column_names.include?(params[:sort]) ? params[:sort] : "first_name"
  end

  def sort_direction
    %w[asc desc].include?(params[:direction]) ? params[:direction] : "asc"
  end   

这是 Heroku 上相同的页面、相同的登录、相同的 db 数据:

2013-07-28T03:59:31.829330+00:00 app[web.1]: Processing by CustomersController#index as HTML
2013-07-28T03:59:31.832789+00:00 app[web.1]: MONGODB (2.7ms) live-heroku['admins'].find({:_id=>BSON::ObjectId('51b0388697279164a4000001')}).limit(-1)
2013-07-28T03:59:31.839820+00:00 app[web.1]: MONGODB (4.5ms) live-heroku['$cmd'].find({"count"=>"customers", "query"=>{:merchant_id=>"518b762e89651a0389000013"}, "fields"=>nil}).limit(-1)
2013-07-28T03:59:31.896128+00:00 app[web.1]: MONGODB (55.7ms) live-heroku['customers'].find({:merchant_id=>"518b762e89651a0389000013"}).limit(40).sort([["first_name", "asc"]])
2013-07-28T03:59:31.951921+00:00 app[web.1]: MONGODB (13.8ms) live-heroku['customers'].find({:merchant_id=>"518b762e89651a0389000013"}).limit(40).sort([["first_name", "asc"]])
2013-07-28T03:59:32.126152+00:00 app[web.1]: MONGODB (7.7ms) live-heroku['admins'].find({:_id=>BSON::ObjectId('518b762e89651a0389000013')}).limit(-1)
2013-07-28T03:59:32.127529+00:00 app[web.1]:   Rendered shared/_header.html.erb (9.7ms)
2013-07-28T03:59:32.146588+00:00 app[web.1]: MONGODB (14.2ms) live-heroku['companies'].find({:_id=>"0"}).limit(-1)
2013-07-28T03:59:32.151084+00:00 app[web.1]: MONGODB (2.4ms) live-heroku['jobs'].find({:customer_id=>BSON::ObjectId('51b7890a9727912430000022')})
2013-07-28T03:59:32.191671+00:00 app[web.1]: MONGODB (2.4ms) live-heroku['jobs'].find({:customer_id=>BSON::ObjectId('51b7890a9727912430000022')})
2013-07-28T03:59:32.233914+00:00 app[web.1]: MONGODB (9.8ms) live-heroku['companies'].find({:_id=>"0"}).limit(-1)
2013-07-28T03:59:32.238298+00:00 app[web.1]: MONGODB (2.2ms) live-heroku['jobs'].find({:customer_id=>BSON::ObjectId('51b78a4e97279123fd000255')})

Heroku 正在从 Mongo Lab 实例中提取。

我的本​​地环境:

今年的 MacBook Air(Haswell CPU,i5)和大约 2012 年的 Mac Mini。

在这两种环境中 - Mongo 的本地性能为 0.2 ~ 0.5 毫秒。然后突然之间,在 Rails 应用程序中没有数据库更改,没有 gem 升级,没有控制器或模型更改,他们只是吓坏了。

我能想象到一件事 - 在过去的几天里,我对它们都进行了 brew 更新。但是 - 无论哪种情况,在 brew 更新后性能都没有立即下降。我没有在我的大脑中“加盖”它,但在 brew 更新和减速之间有很大的间隔(小时)。在 Air 的情况下,它是几天。 Mac Mini 我记不太清了。

有没有人知道为什么 Mongo 会突然像这样发疯?

【问题讨论】:

  • 您是否尝试过在控制台查询中使用.explain() 来查看它发生了什么?

标签: ruby-on-rails mongodb heroku homebrew


【解决方案1】:

@#(%&@ 的儿子,它是我的 mongo.yml 文件,它配置了一个默认数据库,它不是我的本地主机。

我不知道那是什么时候发生的变化——我非常专注于 GIT 历史,以至于我没有费心检查这个文件。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-11-18
    • 1970-01-01
    • 1970-01-01
    • 2011-10-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多