2

I am using sails version 0.10.5 for a particular project. We are using postgresql as the underlying database which is a few GBs in size. Recently I started noticing a problem. There is a postgres table whose model definition is as follows:

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
    }


  }
};

And when I use the controller's blueprint route with URL like:

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

It was taking over 30 seconds to return the result. I have both the columns indexed in postgres and also there is a composite index using both these columns also. Also, when I run the query in postgres, it returns the result in milliseconds. So, I was confused as to why is it taking so long via blueprint route.

So, I modified the route by overriding it in the controller:

  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)        
      }
    })
  },

and then I could get extremely fast performance this way. So my question is, should I avoid using Waterline's methods whenever performance is a requirement or is there anything I did wrong in my model definition or anywhere else ?

Mandeep Singh
  • 7,674
  • 19
  • 62
  • 104
  • 5
    Did you disable populate option for blueprint ? If you don't blueprint populate every association maybe it's because of this you have bad performances ? check /config/blueprints.js to disable populate option – jaumard Aug 09 '15 at 16:20
  • 1
    Perfect! I tried in sails console. there is a remarkable difference between with populate and without populate – Mandeep Singh Aug 09 '15 at 20:06
  • I hope that solution helps. If you would like an additional resource, here is a chat room for sails.js, node.js, and waterline questions. https://gitter.im/balderdashy/sails – Travis Webb Aug 12 '15 at 02:57
  • Although the solution helped but it gives me a big concern regarding whether to use associations at all ? If my indexing in postgres is optimized already, why the performance lag ? I can fetch data by joining all those tables in a native SQL query quite efficiently – Mandeep Singh Aug 15 '15 at 20:54

0 Answers0