在 django ORM 中使用 Q 过滤条件或者简单地获取未过滤的对象并在 python 中进行比较,什么会提供更好的性能。
employee_qs = employee.objects.filter(state=States.ACTIVE, topic_assn__topic_id=instance.ss_topic_id).select_related('c_data').filter(
Q(c_data__is_null=True) | Q(c_budget__gt=F('c_data__budget_spent') + offset_amt))
Run Code Online (Sandbox Code Playgroud)
电压/秒
employee_qs = employee.objects.filter(state=States.ACTIVE, topic_assn__topic_id=instance.ss_topic_id).select_related('c_data')
for employee in employee_qs:
if not employee.c_data or float(employee.budget)-employee.c_data.budget_spent > offset_amt:
#do something...
Run Code Online (Sandbox Code Playgroud)
这两个选择中哪一个的性能会更好?