Y
Y
Yuri Petrashevich2016-07-29 09:27:13
MySQL
Yuri Petrashevich, 2016-07-29 09:27:13

sequelize. Why is so much memory being consumed?

There is a review model - Reviews. When viewing one review, several models are included:

var getLikedEntity = function(row_id, user) {
    return {
        include: [
            {model: InfoOrders},
            {model: Manager, as: 'Manager'},
            {model: Manager, as: 'Author'},
            {model: Manager, as: 'Members', include: [
                {model: Files, as: 'photo'}
            ]},
            {model: Statuses},
            {model: Brands, include: [
                {model: Files, as: 'logo'},
                {model: Sources, include: [
                    {model: Communications}
                ]}
            ]},
            {model: Sources},
            {model: Communications},
            {model: Classifiers, include: [
                {model: Classifiers, as: 'Parent', include: [
                    {model: Manager, as: 'Experts', include: [
                        {model: Files, as: 'photo'}
                    ]}
                ]},
                {
                    model: Reasons, paranoid: false,
                    where: {ReviewId: row_id},
                    required: false,
                    include:[
                        {
                            model: Classifiers,
                            as: 'ChangedClassifier',
                            attributes: ['id','name','rating'],
                            include: [
                                {model: Classifiers, as: 'Parent'}
                            ]
                        }
                    ]
                }
            ]},
            {model: Institutions},
            {model: ClassifiersTextTemplates, include: [
                {model:Classifiers, attributes: ['id', 'name']}
            ]},
            {model: Comments, include: [
                AnswerIdentyfireText,
                Communications,
                {model: CommentSms, as: 'Sms'},
                {model: Manager,
                    include: [
                        {model: Files, as: 'photo'}
                    ]
                },
                {model:Comments, as: 'Childs',
                    include: [
                        Communications,
                        Manager
                    ]},
                {model: CommentGift, as:'Gift'}
            ]},
            {
                model: ColorsUserReview,
                required: false,
                where: {
                    UserId: user.id
                },
                attributes: ['ColorId'],
                include: [{
                    model: Colors,
                    required: false,
                    attributes: ['color']
                }]
            },
            {model: Files}
        ]
    }
};

At the very end there is a file model include. This is exactly the problem - without it, the review loads in 0.5-0.8 seconds, with it - more than 4 seconds, and at the same time the application consumes + ~ 200 MB of memory. It happens that it starts to eat up all the memory, after which the server kills it.
Here is the feedback model:
module.exports = function(sequelize, DataTypes) {
    return sequelize.define('Reviews', {

        description: DataTypes.TEXT,
        rating: DataTypes.INTEGER,
        added_date: DataTypes.DATE,
        added_time: DataTypes.TIME

    }, {
        classMethods: {
            associate: function(models) {

                //associations
                models.Reviews.belongsTo(models.Brands);
                models.Reviews.belongsTo(models.Institutions);
                models.Reviews.belongsTo(models.Sources);
                models.Reviews.belongsTo(models.Communications);
                models.Reviews.belongsTo(models.Users, {as: 'Manager'});
                models.Reviews.belongsTo(models.Users, {as: 'Author'});
                models.Reviews.belongsToMany(models.Files, { through: 'review_files'});
                models.Reviews.belongsToMany(models.Users, { as: 'Members', through: 'review_members'});
                models.Reviews.belongsToMany(models.Classifiers, { through: 'review_classifiers'});
                models.Reviews.belongsToMany(models.ClassifiersTextTemplates, { through: 'review_classifiers_text_tempates'});
                models.Reviews.hasMany(models.Comments, { foreignKey: 'ReviewId', hooks:true });
                models.Reviews.hasMany(models.ClassifiersTextTemplates, { foreignKey: 'ReviewId', hooks:true });
                models.Reviews.hasMany(models.Reasons, { foreignKey: 'ReviewId', hooks:true });
                models.Reviews.belongsToMany(models.Statuses, {through: 'reviews_statuses'});
                models.Reviews.hasOne(models.ColorsUserReview, { foreignKey: 'ReviewId', hooks:true });
            }
        }
    });
};

and here is the file model:
module.exports = function(sequelize, DataTypes) {
    return sequelize.define('Files', {
        file: DataTypes.STRING,
        name: DataTypes.STRING
    }, {
        classMethods: {
            associate: function(models) { }
        }
    });
};

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question