title: Slow associations in SequelizeJS
body: <p>I am trying to diagnose the cause of some slow downs in my Express app which is using SequlizeJS as the ORM. I have a model that has a 2x hasMany &amp; a hasOne relation with 2 other models:</p>
  
  <p><strong>Update</strong>: I've made the associations within the define call using the classMethods#associate function.</p>
  
  <pre><code>// Model1
  classMethods: {
      associate: function(models) {
          Model1.hasMany(models.Model2);
          Model1.hasMany(models.Model3);
          Model1.hasOne(models.Model2, {as: 'next', foreignKey: 'model2_next'});
      }
  }
  
  // Model2
  classMethods: {
      associate: function(models) {
          Model2.belongsTo(models.Model1, {foreignKey: 'model2_next'});
      }
  }
  </code></pre>
  
  <p>if I query them in the following manner:</p>
  
  <pre><code>db.Model1.find({
      where: { /* Simple where statement */ },
      include: [
          db.Model2,
          db.Model3,
          { model: db.Model2, as: 'next' },
      ]
  }).complete(function(err, data) {
      res.json(data);
  });
  </code></pre>
  
  <p>It can take between 8-12seconds to respond. However, if I query Model2 separately and merge them manually using the async &amp; lodash libraries:</p>
  
  <pre><code>async.parallel({
      model2: function(callback) {
          db.Model2.findAll({
              where: { /* Simple where statement */ }
          }).complete(callback)
      },
      model1: function(callback) {
          db.Model1.find({
              where: { /* Simple where statement */ },
              include: [
                  db.Model3,
                  { model: db.Model2, as: 'next' },
              ]
          }).complete(callback);
      }
  }, function(err, data) {
      var response = data.model1.values;
      response.Model2 = data.model2.map(function(Model2){ return Model2.values });
  
      res.json(response);
  })
  </code></pre>
  
  <p>it takes between 60-100ms.</p>
  
  <p>I've tried switching from MySQL to PostgreSQL and whilst PostgreSQL was fractionally faster, it was a matter of 2-3%!</p>
  
  <p>What is causing Sequelize to take so much longer than the split query and is there a way I can speed this up?</p>