Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/assembly/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sqlalchemy 使用棉花糖进行的关系序列化非常缓慢_Sqlalchemy_Flask Sqlalchemy_Flask Restful_Marshmallow_Flask Marshmallow - Fatal编程技术网

Sqlalchemy 使用棉花糖进行的关系序列化非常缓慢

Sqlalchemy 使用棉花糖进行的关系序列化非常缓慢,sqlalchemy,flask-sqlalchemy,flask-restful,marshmallow,flask-marshmallow,Sqlalchemy,Flask Sqlalchemy,Flask Restful,Marshmallow,Flask Marshmallow,我的数据库中有下面的示例表,我管理mit Flask_SQLAlchemy class Employee(db.Model): __tablename__ = 'employee' id = db.Column(db.Integer, primary_key=True) salutation = db.Column(db.String(250), nullable=False) first_name = db.Column(db.String(250), null

我的数据库中有下面的示例表,我管理mit Flask_SQLAlchemy

class Employee(db.Model):
    __tablename__ = 'employee'
    id = db.Column(db.Integer, primary_key=True)
    salutation = db.Column(db.String(250), nullable=False)
    first_name = db.Column(db.String(250), nullable=False)
    last_name = db.Column(db.String(250), nullable=False)
    
    employments = db.relationship('Employment', foreign_keys='Employment.employee_id', backref="employee", lazy=True)
我的Flask架构如下所示:

class EmployeeSchema(mm.SQLAlchemyAutoSchema):
    class Meta:
        model = DB_employee.Employee
        include_fk = True
        include_relationships = True
以下是我对员工的获取路线:

class EmployeesListAPI(Resource):
    def __init__(self):
        self.reqparse_get = reqparse.RequestParser()
        self.reqparse_get.add_argument('employee_id', type=int, action='append', required=False, location='args')
        self.reqparse_get.add_argument('salutation', type=str, action='append', required=False, location='args')
        self.reqparse_get.add_argument('first_name', type=str, required=False, location='args')
        self.reqparse_get.add_argument('last_name', type=str, required=False, location='args')
        
        super(EmployeesListAPI, self).__init__()

    def get(self):
        get_args = self.reqparse_get.parse_args()
        employees = get_list_employee(get_args)
        
        employees_output = EmployeeSchema(many=True, only=get_args['return_fields']).dump(employees)
            
        return employees_output, 200
如果我现在运行它,使用SQL Alhchemy的查询大约需要0.01秒,这对我来说是可以的,但是使用Flask_Marshmallow的序列化大约需要1.3秒

如果我按以下方式更改模式:
include_relationships=False
并手动运行序列化:

for employee, output in zip(employees, employees_output):
            if get_args['show_employments']:
                output['employments'] = [e.id for e in employee.employments]
使用棉花糖进行序列化大约需要0.02秒,手动添加的employments关系大约需要1.2秒

在我的实际案例中,我的关系远不止一个,数据序列化需要惊人的32秒

有没有办法加速棉花糖的系列化?我做错什么了吗

感谢您的支持。

lazy=“joined”
对序列化没有影响。DB数据的查询速度稍快,但这不是我目前的问题。