sequelize icon indicating copy to clipboard operation
sequelize copied to clipboard

Include models query - inefficient

Open goodthing opened this issue 11 years ago • 1 comments

I noticed when I include multiple models in my .find it creates a query that returns all combinations it seems. It's returning 100 thousand rows to get one object and is currently very slow. The model is

      classMethods: {
            associate: function (models) {
                Users.belongsTo(models.R, {foreignKey: 'userId'});
                Users.hasMany(models.M, {foreignKey: 'userId'});
                Users.hasMany(models.MA, {foreignKey: 'userId'});
                Users.belongsToMany(models.DE, {through: 'DD', as: 'DE'});
                Users.hasMany(models.FSE, {foreignKey: 'userId', as: 'FSE'});
                Users.hasMany(models.AST, {foreignKey: 'userId', as: 'AST'});
                Users.hasMany(models.STT, {foreignKey: 'userId', as: 'STT'});
                Users.hasMany(models.FGE, {foreignKey: 'userId', as: 'FGE'});
                Users.hasMany(models.NDE, {foreignKey: 'userId', as: 'NDE'});
            },

and a query to Users include all those models like this

    {model: db.MA,
            as: 'MA',
            where: {active: true}
            , required: false
            , attributes: ['id', 'name', 'primaryImageFilename']
            , through: {attributes: []}
        },
        {model: db.AST,
            as: 'AST',
            where: {}
            , required: false
            , attributes: ['id', 'name', 'startDate', 'finishDate', 'isCurrent']
        }

.. and so on up to all of the associations mentioned above. How do I make this query perform better? Using mysql flavor. I am going to put indexes in but 100 thousand row for a single user is excessive in my view

goodthing avatar Feb 26 '15 23:02 goodthing

@streetkid row duplication is a huge performance hit. We have a {seperate: true} option for includes on the roadmap that would allow you to tell a specific include to be run in a seperate query entirely (allowing you to manage the tradeoffs of performance).

mickhansen avatar Feb 27 '15 08:02 mickhansen