How to increase the speed of django filter condition?

Viewed 197

I have 2 million data in the database. I tried to filter the foreign data in database. Finally filter data around 30 records only. but it's taking time around 2 to 3 minutes. I need to reduce the time and filter data fastly. How to achieve this scenario.

Models.py

class Sample1(models.Model):
   name = models.CharField( max_length=64 )

class Sample2( models.Model):
   name2 = models.CharField( max_length=64 )

class Sample3(models.Model):
   name3 = models.CharField( max_length=64 )

class sample4(models.Model):
   sample1 = models.ForeignKey( Sample1,on_delete=models.CASCADE )
   sample2 = models.ForeignKey( Sample2,on_delete=models.CASCADE )
   sample3 = models.ForeignKey( Sample3,on_delete=models.CASCADE )
   name4 = models.CharField( max_length=64 )

views.py

def user(sample1,sample2,):
    rank = sample4.objects.filter( sample1=sample1,
                                        sample2=sample2,
                                        sample3=sample3).order_by( 'name' )
    return rank
    

user(sample1,sample2,sample3)

This my case. In sample4 table i have 1 million data and tried to filter some records using this condition. I got the records. but It's taking show much time. I need to reduce the time and get the filter data fastly. Any one can give sample example code used this scenario

0 Answers
Related