我有一个数据库包含
Profile
和
Relationship
模型。我没有在模型中显式链接它们(因为它们是第三方ID,可能还不存在于两个表中),但是
source
和
target
字段映射到一个或多个
轮廓
对象通过
id
字段:
from django.db import models
class Profile(models.Model):
id = models.BigIntegerField(primary_key=True)
handle = models.CharField(max_length=100)
class Relationship(models.Model):
id = models.AutoField(primary_key=True)
source = models.BigIntegerField(db_index=True)
target = models.BigIntegerField(db_index=True)
我的查询需要从
Relationship.source
尚未作为
Profile.id
. 该列表将用于从第三方收集必要的数据。下面的查询可以工作,但随着表的增长(10m以上),子查询变得非常大且速度很慢。
关于如何优化这一点,有什么建议吗?后端是PostgreSQL,但如果可能的话,我想使用原生Django ORM。
编辑:有一个额外的复杂性水平,这将有助于缓慢的查询。并非所有ID都能保证返回成功,这意味着它们继续“不存在”,并使程序处于无限循环中。所以我添加了一个
filter
和
order_by
输入最高
身份证件
从上一批100个。这将导致一些问题,因此,很抱歉最初错过了它。
from django.db.models import Subquery
user = Profile.objects.get(handle="philsheard")
qs_existing_profiles = Profiles.objects.all()
rels = TwitterRelationship.objects.filter(
target=user.id,
).exclude(
source__in=Subquery(qs_existing_profiles.values("id"))
).values_list(
"source", flat=True
).order_by(
"source"
).filter(
source__gt=max_id_from_previous_batch # An integer representing a previous `Relationship.source` id
)
提前感谢您的建议!