flice com
flice com

Reputation: 125

Performance: Database, Django, reverse lookup or direct?

I have 2 models. first contain 5000 objects. second contain 10M objects.

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"))

I tried both options to get Followers objects out of influencer's usernames:

                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()

It seems that both results in same around 20 seconds .... are thay SQL the same ? I think the "REVERSE_SCAN" JOIN 10M with the middle table (manytomany middle table) while the second JOIN the 5K table with the middle table. am I right ?

SUGGESTIONS ?

Upvotes: 3

Views: 163

Answers (0)

Related Questions