Saturday, October 3, 2015

Re: Slow query even for small number of rows

Need more information.

What do you consider slow?
How many rows are there total in the table?
How many rows are you retrieving?

It looks like your serializers pull in a lot of cross-table data, so I'm guessing there are numerous queries for each request. Have you examined the SQL queries being generated to see all of the joins and related queries? Maybe you can play around with pre-fetching some of the related rows?

Have you installed the debug toolbar? That should give some good clues as to where your processing is being held up.

-James

On Oct 3, 2015 7:54 PM, "Yik Surn Chong" <yiksurn.chong@gmail.com> wrote:
Hi guys,

I wonder why my query to view the list of scholarships takes quite long to load even though the number of rows is small. Would be great if someone could help me out here.

Thanks.

Here's my view.
class DualSerializerViewSet(viewsets.ReadOnlyModelViewSet):
    queryset = Scholarship.objects.all()
    filter_class = ScholarshipFilter
    """ViewSet providing different serializer for list and details view"""
    def get_serializer_class(self):
        if self.action == 'list':
            return ListScholarshipSerializer
        if self.action == 'retrieve':
            return DetailScholarshipSerializer

Here's my serializers.
class ListScholarshipSerializer(serializers.ModelSerializer):
    """Serializer when showing scholarship list"""
    offer_levels = ScholarshipOfferLevelSerializer(source='scholarshipofferlevel_set', many=True)
    offer_courses = ScholarshipOfferCourseSerializer(source='scholarshipoffercourse_set', many=True)
    # institutions = ScholarshipOfferInstitutionSerializer(source='scholarshipofferinstitution_set', many=True)
    offer_countries = ScholarshipOfferCountrySerializer(source='scholarshipoffercountry_set', many=True)
    class Meta:
        model = Scholarship
        fields = ('scholarship_id', 'title', 'info_url', 'opening_date', 'closing_date',
            'offer_levels', 'offer_courses', 'offer_countries')
# serializer to use when showing scholarship details
class DetailScholarshipSerializer(serializers.ModelSerializer):
    """Serializer when showing scholarship details"""
    tuition_benefit = TuitionBenefitSerializer()
    offer_levels = ScholarshipOfferLevelSerializer(source='scholarshipofferlevel_set', many=True)
    offer_courses = ScholarshipOfferCourseSerializer(source='scholarshipoffercourse_set', many=True)
    other_benefits = ScholarshipOfferBenefitSerializer(source='scholarshipofferbenefit_set', many=True)
    institutions = ScholarshipOfferInstitutionSerializer(source='scholarshipofferinstitution_set', many=True)
    offer_countries = ScholarshipOfferCountrySerializer(source='scholarshipoffercountry_set', many=True)
    status_eligibility = ScholarshipStatusEligibilitySerializer(source='statuseligibility_set', many=True)
    academic_eligibility = ScholarshipAcademicEligibilitySerializer(source='academiceligibility_set', many=True)
    apply_guide = ApplicationGuideSerializer()
    class Meta:
        model = Scholarship

And this is the scholarship model.
class Scholarship(models.Model):
    NATIONALITY = (
        ('Malaysian','Malaysian'),
        ('Any','Any'),
    )
    scholarship_id = models.AutoField(primary_key=True)
    title = models.CharField(max_length=100, unique=True)
    opening_date = models.DateField(null=True)
    closing_date = models.DateField(null=True)
    info_url = models.URLField(max_length=2048, null=True)
    description = models.TextField(null=True) 
    contact_email = models.EmailField(null=True)
    contact_number = models.CharField(max_length=150, null=True)
    bond = models.NullBooleanField()
    bond_length = models.CharField(max_length=100, null=True)
    eligible_age = IntegerRangeField(null = True)
    eligible_income = models.DecimalField(max_digits=10, decimal_places=2, blank=True, default=0, null=True)
    other_eligibility = models.TextField(null=True)
    tuition_benefit = models.ForeignKey(TuitionBenefit, related_name='scholarship')
    apply_guide = models.OneToOneField(ApplicationGuide, null=True)
    academic_eligibility = models.ManyToManyField(Academic, through='AcademicEligibility')
    status_eligibility = models.ManyToManyField(Status, through='StatusEligibility')
    offer_levels = models.ManyToManyField(StudyLevel, through = 'ScholarshipOfferLevel')
    offer_courses = models.ManyToManyField(Course, through = 'ScholarshipOfferCourse')
    other_benefits = models.ManyToManyField(OtherBenefit, through= 'ScholarshipOfferBenefit')
    institutions = models.ManyToManyField(Institution, through= 'ScholarshipOfferInstitution')
    offer_countries = models.ManyToManyField(Country, through= 'ScholarshipOfferCountry')
    class Meta:
        db_table = 'scholarship_general_info'
 

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/cfa1bb9b-9deb-4bcc-8036-03235ed98380%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CA%2Be%2BciX6sPLhHTajNSM3rjqHh5rtdL6mdH7oeu9qZPfJ1gyoSg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment