简单查询的性能问题

时间:2017-01-13 09:06:37

标签: node.js mongodb loopbackjs

我在Image表中有近10'000条记录,我想用查询检索它们。

图像模型定义:

{
  "name": "Image",
  "plural": "Images",
  "base": "PersistedModel",
  "idInjection": true,
  "options": {
    "validateUpsert": true
  },
  "properties": {
    "name": {
      "type": "string",
      "required": false
    },
    "uploaddate": {
      "type": "date",
      "required": true
    },
    "location": {
      "type": "string"
    },
    "description": {
      "type": "string"
    },
    "shootingdate": {
      "type": "date"
    },
    "imageStatusTitle": {
      "type": "string"
    },
    "category": {
      "type": "string"
    },
    "canton": {
      "type": "string"
    },
    "rights": {
      "type": "boolean"
    },
    "earmarked": {
      "type": "boolean"
    }
  },
  "validations": [],
  "relations": {
    "imageStatus": {
      "type": "embedsOne",
      "model": "ImageStatus",
      "foreignKey": "",
      "options": {
        "persistent": false
      }
    },
    "categories": {
      "type": "hasMany",
      "model": "ImageCategory",
      "foreignKey": ""
    },
    "votes": {
      "type": "hasMany",
      "model": "WebsiteUser",
      "foreignKey": "imageId",
      "through": "ImageVote"
    },
    "original": {
      "type": "embedsOne",
      "model": "File",
      "property": "original",
      "options": {
        "persistent": false
      }
    },
    "small": {
      "type": "embedsOne",
      "model": "File",
      "property": "small",
      "options": {
        "persistent": true
      }
    },
    "medium": {
      "type": "embedsOne",
      "model": "File",
      "property": "medium",
      "options": {
        "persistent": true
      }
    },
    "large": {
      "type": "embedsOne",
      "model": "File",
      "property": "large",
      "options": {
        "persistent": true
      }
    },
    "xlarge": {
      "type": "embedsOne",
      "model": "File",
      "property": "xlarge",
      "options": {
        "persistent": true
      }
    },
    "owner": {
      "type": "belongsTo",
      "model": "WebsiteUser",
      "foreignKey": "fotographerId"
    },
    "widgets": {
      "type": "hasMany",
      "model": "Widget",
      "through": "WidgetImage"
    }
  }
  ...

我将进行的查询如下:

    const query = {
        fields: ['id', 'name', 'fotographerId'],
        include: {
            relation: 'owner',
            fields: ['id', 'firstname', 'lastname', 'street', 'zip', 'city', 'email', 'phone', 'mobile', 'locale']
        }
    };

    if (0 < whereFilter.length) {
        query.where = {
            and: whereFilter
        };
    }

    app.models.Image.find(query, function(err, records) {
        ...
    }

问题是此查询需要5分钟才能执行。 然后我通过删除默认排序explained in this issue将其改进4分钟。

但是对于10'000文件这样的查询,无论如何4分钟对我来说似乎很慢。你看到了什么问题吗?或者你知道如何加快这个查询的速度吗?

1 个答案:

答案 0 :(得分:0)

以防有人遇到同样的问题。性能问题的原因是在每次获取/加载模型时执行某些代码的钩子上。现在需要2秒钟才能完成。