如何解决与棉花糖的关系序列化非常慢
我在数据库中有以下示例表,用于管理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,location='args')
self.reqparse_get.add_argument('first_name',location='args')
self.reqparse_get.add_argument('last_name',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秒,而手动添加的雇佣关系大约需要1.2秒。
在我的真实情况下,我的关系远不止一种,数据的序列化花费了惊人的32秒。
有没有一种方法可以加快棉花糖的序列化?我在做错什么吗?
感谢您的支持。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。