【发布时间】:2020-09-28 16:50:44
【问题描述】:
我正在使用 PostgreSQL 处理具有超过 4000 万行的模型(表)并且查询非常慢(> 2 分钟)。
模型:
class ModelA(...):
source = models.ForeignKey(ModelB, related_name='source', ...)
target = models.ForeignKey(ModelB, related_name='target', ...)
class ModelB(...):
c = models.ForeignKey(ModelC, ...)
...
Django 过滤器:
ModelA.objects.filter(source__isnull=True, target__c=my_c)
查询:
SELECT "model_a"."id", ...
FROM "model_a"
INNER JOIN "model_b" T3 ON ("model_a"."target_id" = T3."id")
WHERE ("model_a"."removed" = False AND "model_a"."source_id" IS NULL AND T3."c_id" = 389)
解释查询:
QUERY PLAN
-----------------------------------------------------------------------------------------------------------------------------------
Gather (cost=1001.26..2498957.37 rows=12277 width=71)
Workers Planned: 3
-> Nested Loop (cost=1.26..2496729.67 rows=3960 width=71)
-> Parallel Index Scan using model_b_model_c_removed_filename on model_b t3 (cost=0.69..2481771.88 rows=5907 width=4)
Index Cond: (c_id = 389)
-> Index Scan using model_a_source_target_idx on model_a (cost=0.56..2.52 rows=1 width=71)
Index Cond: (target_id = t3.id)
Filter: (NOT removed)
(8 rows)
如何优化它?
我有优化问题,我想知道不同的解决方案等。
【问题讨论】:
-
运行
EXPLAIN YOU QUERY并将查询执行平面放在这里 -
@SlavaRozhnev 添加了
EXPLAIN。谢谢
标签: django postgresql performance