问题描述
我在数据库中有以下示例表,用于管理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 (将#修改为@)