我的模型结构的简化示例是
class Corporation(models.Model):
...
class Division(models.Model):
corporation = models.ForeignKey(Corporation)
class Department(models.Model):
division = models.ForeignKey(Division)
type = models.IntegerField()
现在我想显示一个表,该表显示公司,其中一列将包含某种类型的部门数,例如type=10
。当前,这是通过Corporation
模型上的帮助程序来实现的,例如,
class Corporation(models.Model):
...
def get_departments_type_10(self):
return (
Department.objects
.filter(division__corporation=self, type=10)
.count()
)
这里的问题是,由于N + 1问题,这绝对会破坏性能。
我尝试使用select_related
,prefetch_related
,annotate
和subquery
解决此问题,但是我无法获得所需的结果。
理想情况下,查询集中的每个Corporation
都应使用整数type_10_count
进行注释,该整数应反映该类型部门的数量。
我确定我可以在.extra()
中使用原始sql进行操作,但是文档宣布它将不推荐使用(我在Django 1.11上)
编辑:原始sql解决方案示例
corps = Corporation.objects.raw("""
SELECT
*,
(
SELECT COUNT(*)
FROM foo_division div ON div.corporation_id = c.id
JOIN foo_department dept ON dept.division_id = div.id
WHERE dept.type = 10
) as type_10_count
FROM foo_corporation c
""")
答案 0 :(得分:5)
我认为使用Subquery
可以通过此代码获得与您提供的SQL类似的SQL
# Get amount of departments with GROUP BY division__corporation [1]
# .order_by() will remove any ordering so we won't get additional GROUP BY columns [2]
departments = Department.objects.filter(type=10).values(
'division__corporation'
).annotate(count=Count('id')).order_by()
# Attach departments as Subquery to Corporation by Corporation.id.
# Departments are already grouped by division__corporation
# so .values('count') will always return single row with single column - count [3]
departments_subquery = departments.filter(division__corporation=OuterRef('id'))
corporations = Corporation.objects.annotate(
departments_of_type_10=Subquery(
departments_subquery.values('count')
)
)
生成的SQL是
SELECT "corporation"."id", ... (other fields) ...,
(
SELECT COUNT("division"."id") AS "count"
FROM "department"
INNER JOIN "division" ON ("department"."division_id" = "division"."id")
WHERE (
"department"."type" = 10 AND
"division"."corporation_id" = ("corporation"."id")
) GROUP BY "division"."corporation_id"
) AS "departments_of_type_10"
FROM "corporation"
这里有些担心的是,大表子查询的速度可能很慢。但是,数据库查询优化器足够聪明,可以将子查询提升为OUTER JOIN,至少我听说PostgreSQL做到了。
答案 1 :(得分:3)
您应该可以使用Case()
表达式来执行此操作,以查询具有您要查找的类型的部门的数量:
from django.db.models import Case, IntegerField, Sum, When, Value
Corporation.objects.annotate(
type_10_count=Sum(
Case(
When(division__department__type=10, then=Value(1)),
default=Value(0),
output_field=IntegerField()
)
)
)