- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我知道我可以像这样指定存储 prefetch_related 的结果。
my_objs = MyClass.objects.prefetch_related(
Prefetch('relation', to_attr='relation_as_list')
)
for my_obj in my_objs:
l = my_obj.relation_as_list
是不是和下面的一样?文档说 prefetch_related store 会导致 Queryset,但我不知道我从哪里获得了性能提升?
my_objs = MyClass.objects.prefetch_related('relation')
for my_obj in my_objs:
l = list(my_obj.relation.all()) # looks like DB hit but it isnt?
最佳答案
这两个是一样的
MyClass.objects.prefetch_related('relation')
MyClass.objects.prefetch_related(
Prefetch('relation', to_attr='relation_as_list')
)
You can use the Prefetch object to further control the prefetch operation (docs)
只有当您需要优化prefetch_related
的结果时,您才需要一个Prefetch
对象。例如,您可能不需要每个关系
,而是特定的关系,因此您可以优化预取查询。
Using to_attr is recommended when filtering down the prefetch result as it is less ambiguous than storing a filtered result in the related manager’s cache (docs)
to_attr
不会提供额外的性能提升,它允许使 Prefetch
关系不那么模糊,并且可以预取与不同 QuerySet 的相同关系。
MyClass.objects.prefetch_related(
Prefetch('same_relation', queryset=queryset_one, to_attr='relation_set_one')
Prefetch('same_relation', queryset=queryset_two, to_attr='relation_set_two')
)
如果您使用不同的 Prefetch
对象预取相同的关系,则每个对象都会有一个额外的查询。生成的预取查询集将存储在一个列表中,但结果并不是您假设的 relation_as_list
列表。它们是查询集。在您的示例中,可以使用 all()
访问这两个关系,例如 my_obj.relation_as_list.all()
和 my_obj.relation.all()
关于性能提升
总而言之,prefetch_related
在一个(额外的)数据库命中中获取相关对象,这是性能提升的来源。 Prefetch
对象可让您进一步优化此数据库调用。
for item in yourmodel.object.all():
for a_relation in item.relation.all():
do_something(a_relation)
# WITHOUT PREFETCH RELATED YOU'D HIT DB EVERY TIME!!
# IMAGINE IF YOU HAD TONS OF ITEMS
# THIS WILL HAVE 2 DB HITS
for item in yourmodel.object.prefetch_related('relation').all().:
for a_relation in item.relation.all():
do_something(a_relation)
关于Django store prefetch_related 结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39030198/
我有一个问题,我现在想解决一天。 随着模型 class Quote(models.Model): text = models.TextField() source = models.F
我有一个 Django 网站,模型为 Event ,让我们说: class Event(models.Model): home = models.ForeignKey('Team', rela
我现在正在构建一个基本的时间记录应用程序,我有一个使用 django-taggit 的 todo 模型。我的 Todo 模型如下所示: class Todo(models.Model): pr
我需要列出我所有的设备。为此,我使用与减少查询量相关的预取。但是其中一个花费了太多时间..我想知道它是否不能更好。 我将从模型构造开始:我想要一个设备列表。 class Device(models.M
我正在尝试优化我的查询,但 prefetch_related 坚持加入表并选择所有字段,即使我只需要关系表中的 id 列表。 您可以忽略第四个查询。它与问题无关。 相关代码: class Contac
在 Django docs 中给出这些模型: class Topping(models.Model): name = models.CharField(max_length=30) class
我有 Menu具有与自身相关的名为 parent 的 ForeignKey 的模型。 如果 parent 为 None 则表示此菜单是父菜单,如果它显示另一个 Menu 对象,则表示它是其父级的子菜单
我正在尝试使用 PersonScore 对这 3 个表进行内部联接,但找不到 persontype。我究竟做错了什么? 楷模: class PersonScore(models.Model):
我有这样的模型 Model Trip: hotel: m2m_field to Hotel flight: m2m_field to Flight Model Hotel: city: f
我有两个 Django 模型: class Product(models.Model): name = models.CharField(max_length=80, null=True)
如果我的模型看起来像: class Publisher(models.Model): pass class Book(models.Model): publisher = models
我已经在 django 1.4 中从主干尝试过 prefetch_related(),但无法预取反向查找。 我的简化模型(每本书有很多价格): class Book(models.Model):
我几乎可以肯定我在这里做错了什么,但我想不通。 在我的应用程序中,项目可以存储在工作区中,如下所示: # models.py class Item(models.Model): name
我正在开发一个 Django 网络应用程序,我正在使用 prefetch_related 和 select_related 方法来最小化单个数据库的访问量,我在我的 User 模型中有一个特定的方法可
我正在使用 DRF 序列化一些相关模型。在我下面的玩具示例中,假设每个作者都可以拥有一百万本书。显然对所有“好”书进行数据库查询,然后对所有“坏”书进行另一个数据库查询是低效的。 这篇文章 [ htt
我想通过 prefetch_related 查询来获取我的模型和所有相关模型,遵循一对多关系,但具有过滤条件。我的目标是使用 prefetch_related 在一个 django 查询中获取我的所有
我正在尝试返回,对于每个 UserProfile,它具有一对多 Subscription,它具有两个 Artist 的外键> 和 UserProfile,每个艺术家有许多 ReleaseGroup,c
我有一个关系简单的模型 class Tasks(models.Model): initiator = models.ForeignKey(User, on_delete = models.CAS
我已经用 GET 实现了一个基于类的 View 获取工资单详情的方法 id .我如何附加 net_allowance在工资单中 object所以JSON看起来像这样: { "employee_
我以前使用过 select_related 和 prefetch_related,效果很好。 我正在处理一个当前项目,由于某种原因,我无法弄清楚为什么我的预取相关查询无法正常工作,因此我收到了很多冗余
我是一名优秀的程序员,十分优秀!