difference between filter with multiple arguments and chain filter in django

前端 未结 9 791
[愿得一人]
[愿得一人] 2020-11-30 20:08

What is the difference between filter with multiple arguments and chain filter in django?

9条回答
  •  执笔经年
    2020-11-30 20:59

    The performance difference is huge. Try it and see.

    Model.objects.filter(condition_a).filter(condition_b).filter(condition_c)

    is surprisingly slow compared to

    Model.objects.filter(condition_a, condition_b, condition_c)

    As mentioned in Effective Django ORM,

    • QuerySets maintain state in memory
    • Chaining triggers cloning, duplicating that state
    • Unfortunately, QuerySets maintain a lot of state
    • If possible, don’t chain more than one filter

提交回复
热议问题