我有2个模型。首先包含 5000 个对象。第二个包含 10M 个对象。
class Follower(models.Model):
username = models.CharField(
_("username"), max_length=250, db_index=True)
class Influencer(models.Model):
username = models.CharField(
_("username"), max_length=250, unique=True, null=False, blank=False, db_index=True)
followers = models.ManyToManyField(
"instagram_data.Follower", verbose_name=_("Followers"))
我尝试了两种方法来从影响者的用户名中获取关注者对象:
influencer_choosen_by_user = ['nike', 'adidas']
if self.REVERSE_SCAN:
qs = Follower.objects.filter(
influencer__username__in=influencer_choosen_by_user)
else:
qs = Follower.objects.none()
qs_influencer = Influencer.objects.filter(username__in=influencer_choosen_by_user)
for influe in qs_influencer.iterator():
qs = qs | influe.followers.all()
似乎这两个结果都在 20 秒左右.... SQL 是一样的吗?我认为“REVERSE_SCAN” JOIN 10M 与中间表(manytomany middle table),而第二个 JOIN 5K 表与中间表。我对吗 ?
建议?