【问题标题】:Collection.find() very slow with other BDD Meteor ReactCollection.find() 对于其他 BDD Meteor React 非常慢
【发布时间】:2016-01-27 16:39:30
【问题描述】:

我正在使用 Meteor 和 React 开发一个应用程序。 我正在尝试从外部 MongoDB 数据库中获取 inventory_array 的内容,但速度非常慢。我等了 7 秒,我有一个对象,我等了 5 秒,还有另外两个对象,等等……

Spaces = new Mongo.Collection("Space");
Properties = new Mongo.Collection("Property");
Inventories = new Mongo.Collection("Inventory");
if (Meteor.isClient) {
    Meteor.subscribe("Property");
    Meteor.subscribe("Space");
    Meteor.subscribe("Inventory");  
    Tracker.autorun(function() { 

        inventories_array = Inventories.find({quantityBooked: 2},{fields: {priceTaxExcl: 1, property: 1, space: 1}}).fetch();
    console.log(inventories_array);

}

if (Meteor.isServer) {
    Meteor.publish("Property", function () {
    return Properties.find();
  });

  Meteor.publish("Space", function () {
    return Spaces.find();
  });
  Meteor.publish("Inventory", function () {
    return Inventories.find();
  }); 
}

库存对象:

{
 ...
 "property" : ObjectId("..."),
 "space" : ObjectId("..."),
 "quantityBooked":2,
 "priceTaxExcl":...,
 ...
}

我使用MONGO_URL=mongodb://127.0.0.1:27017/mydb meteor run启动应用程序

任何想法为什么它这么慢?

【问题讨论】:

  • Inventory/Inventories 以及 Space 和 Property 中的数据集有多大?
  • 对于库存:3 个 ObjectId,1 个日期,4 个 int 用于财产和空间,它们很大,但我在库存中只有它们的 ID。我已经用另一个较小的集合(3 个字符串和 2 个日期)进行了测试,我遇到了同样的问题
  • 我的意思是您的收藏中有多少条记录?
  • 很多。 2148 个库存,51 个属性,96 个空间,但我在 find() 上使用 {limit: 5} 进行了测试,它仍然很慢
  • 如果您在客户端设置限制,它不会提高性能,因为服务器仍会将整个集合传递回客户端。所有 2148 个库存对象都被发送给您的客户(可能非常频繁)。像我们这样说话很难调试,所以我建议安装 Kadira:kadira.io 它是免费的,您可以在 10 分钟内开始获取有关您的应用程序的统计信息,它应该会显示您的性能瓶颈在哪里。您也可以尝试在 chrome 调试控制台中运行 Inventories.find() 查询,看看需要多长时间

标签: performance mongodb meteor reactjs find


【解决方案1】:

如果您查看检查器中的网络选项卡,您将看到每个订阅从服务器流向客户端的所有数据,您将能够判断它有多大以及需要多长时间。

我建议您首先更改您的 Inventory 发布,如下所示:

Meteor.publish("Inventory", function () {
  if ( this.userId ){ // only return data if we have a logged-in user
    return Inventories.find({quantityBooked: 2},{fields: {priceTaxExcl: 1, property: 1, space: 1}});
  } else {
    this.ready();
  }
});

这样,您的服务器只发送所需文档中的所需字段(假设您只需要 {quantityBooked: 2} 的文档。

【讨论】:

    猜你喜欢
    • 2016-12-17
    • 1970-01-01
    • 1970-01-01
    • 2013-08-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-21
    相关资源
    最近更新 更多