2

我正在为特定项目使用sails 0.10.5 版。我们使用 postgresql 作为底层数据库,大小为几 GB。最近我开始注意到一个问题。有一个postgres表,其模型定义如下:

module.exports = {

  attributes: {

    user: {
      model: "user",
      required: true
    },

    organization: {
      model: "organization",
      required: true
    },

    questionaire: {
      model: "questionaire",
      required: true
    },

    mailinglist: {
      model: "mailinglist",
      defaultsTo: null
    },

    name: {
      type: "string",
      required: true
    },

    broadcast: {
      type: "boolean",
      defaultsTo: false
    },

    link: {
      type: "string",
      defaultsTo: null
    },

    count: {
      type: "integer",
      defaultsTo: 0
    }


  }
};

当我使用带有 URL 的控制器的蓝图路由时:

GET /api/survey?broadcast=false&organization=2

返回结果需要 30 多秒。我在 postgres 中索引了两个列,并且还有一个使用这两个列的复合索引。此外,当我在 postgres 中运行查询时,它会以毫秒为单位返回结果。所以,我很困惑为什么通过蓝图路线需要这么长时间。

因此,我通过在控制器中覆盖它来修改路由:

  find: function (req, res){
    var packet = req.params.all();
    var myQuery = "select * from survey where 1=1 ";
    Object.keys(packet).forEach(function (key){
      myQuery += " and "+ key + " = " + packet[key] + " "
    })
    Survey.query(myQuery, function (err, result){
      if(err){
        return res.json(500, err);
      }
      else{
        return res.json(200, result.rows)        
      }
    })
  },

然后我可以通过这种方式获得极快的性能。所以我的问题是,我是否应该在需要性能时避免使用 Waterline 的方法,或者我在模型定义或其他任何地方做错了什么?

4

0 回答 0