django - Django 使用 prefetch_related 或 select_related 的有效方法
问题描述
我经常使用prefetch_related
&select_related
一次性处理嵌套查询。我想让你知道我们可以做更多的事情来让它更快一点。
Foo.objects.prefetch_related('bar')
在我的应用程序中的某些情况下,我只需要特定列(如果Bar
模型)。
在下面的方法中使用自定义查询集是否可以prefetch_related
提高查询速度?
Foo.objects.prefetch_related(Prefetch('bar',queryset=Bar.objects.all().only('id')))
解决方案
推荐阅读
- python - 将枕头图像从 PDF 保存到 Google 云服务器
- dialogflow-es - 用户退出 Google 助理后,基本卡在哪里?
- postgresql - docker-compose时如何创建postgres数据库并运行迁移
- port - 大使中的 service_port 是做什么用的
- python-2.7 - BeautifulSoup Python API 没有得到我想要的部分
- sql - 有没有办法设置日期模式并强制指定日期?
- java - LocalTime.parse 在 JDK 12 上的行为不同
- javascript - 尝试将对象放入我的数组中时,此过滤器功能不会删除旧对象
- angular - 存储 Observable 并稍后订阅它 - Angular 7
- macos - shell `time` 命令并行执行的正确顺序