0
votes

Hi all this is my view class

class ModuleViewSet(mixins.CreateModelMixin,
                    mixins.RetrieveModelMixin,
                    mixins.UpdateModelMixin,
                    mixins.ListModelMixin,
                    viewsets.GenericViewSet):
    queryset = models.Module.objects.all()
    permission_classes = [ModuleViewPermission]
    serializer_class = serializers.ModuleSerializer

I have turned the django.db logger to INFO level to see the actual database queries being executed. Following are my findings:

  1. Get list of modules:
2020-07-26 19:22:18,062 DEBUG utils:110 e70f16f5c7494c8783ca9f5a6bea22c4 (0.013) SELECT "courses_module"."id", "courses_module"."course_id", "courses_module"."name", "courses_module"."number" FROM "courses_module"; args=() django.db.backends
2020-07-26 19:22:18,090 INFO basehttp:154 e70f16f5c7494c8783ca9f5a6bea22c4 "GET /api/courses/modules/ HTTP/1.1" 200 99 django.server
  1. Get single object:
2020-07-26 19:23:26,143 DEBUG utils:110 f1332e4a5e2247709dc784dc2f8b6d19 (0.005) SELECT "courses_module"."id", "courses_module"."course_id", "courses_module"."name", "courses_module"."number" FROM "courses_module"; args=() django.db.backends
2020-07-26 19:23:27,331 DEBUG utils:110 f1332e4a5e2247709dc784dc2f8b6d19 (0.003) SELECT "courses_module"."id", "courses_module"."course_id", "courses_module"."name", "courses_module"."number" FROM "courses_module" WHERE "courses_module"."id" = 1; args=(1,) django.db.backends
2020-07-26 19:23:27,342 DEBUG utils:110 f1332e4a5e2247709dc784dc2f8b6d19 (0.006) SELECT "courses_course"."id", "courses_course"."name", "courses_course"."description", "courses_course"."price_group_id" FROM "courses_course" WHERE "courses_course"."id" = 1; args=(1,) django.db.backends
2020-07-26 19:23:27,359 DEBUG utils:110 f1332e4a5e2247709dc784dc2f8b6d19 (0.015) SELECT "user_user"."id", "user_user"."password", "user_user"."last_login", "user_user"."is_superuser", "user_user"."username", "user_user"."is_staff", "user_user"."is_active", "user_user"."date_joined", "user_user"."email", "user_user"."first_name", "user_user"."last_name" FROM "user_user" INNER JOIN "courses_course_authors" ON ("user_user"."id" = "courses_course_authors"."user_id") WHERE "courses_course_authors"."course_id" = 1; args=(1,) django.db.backends
2020-07-26 19:23:27,375 INFO basehttp:154 f1332e4a5e2247709dc784dc2f8b6d19 "GET /api/courses/modules/1/ HTTP/1.1" 200 45 django.server

For 1 it may be correct to select all but according to documentation queryset is cached for the upcoming requests. But in get_queryset() method we can see that we are re-evaluating queryset.

Apart from this contradiction. The results for get single object (pt 2) are more concerning. As we are first selecting all the objects and then again are selecting the module object by Id. Last 2 queries are for checking user permissions on the object (which are acceptable).

Please let me know if I have something wrong in my views, or if it is a known performance issue of django rest framework. Have also raised this question here

This can be a major issue in case of millions of records in table.

Thanks

1

1 Answers

0
votes

I have got the reason behind this miscellaneous behaviour. Querysets are lazy and they are computed whenever we expect the result. While I was debugging and hovering over the queryset, the queries were being executed as the debugger was expecting the result. I may be wrong. But I was unable to reproduce the above output when I ran it in non debug mode. Following are the logs: Get List:


2020-07-26 23:00:06,697 DEBUG utils:110 1c8a250d8f784aff9c4e5056e446ddf9 (0.004) SELECT "courses_module"."id", "courses_module"."course_id", "courses_module"."name", "courses_module"."number" FROM "courses_module"; args=() django.db.backends
2020-07-26 23:00:06,706 INFO basehttp:154 1c8a250d8f784aff9c4e5056e446ddf9 "GET /api/courses/modules/ HTTP/1.1" 200 99 django.server

Get object:

2020-07-26 22:59:47,120 DEBUG utils:110 018f23ade3774a44a7cd423c80f6fe59 (0.005) SELECT "courses_module"."id", "courses_module"."course_id", "courses_module"."name", "courses_module"."number" FROM "courses_module" WHERE "courses_module"."id" = 1; args=(1,) django.db.backends
2020-07-26 22:59:47,125 DEBUG utils:110 018f23ade3774a44a7cd423c80f6fe59 (0.004) SELECT "courses_course"."id", "courses_course"."name", "courses_course"."description", "courses_course"."price_group_id" FROM "courses_course" WHERE "courses_course"."id" = 1; args=(1,) django.db.backends
2020-07-26 22:59:47,137 DEBUG utils:110 018f23ade3774a44a7cd423c80f6fe59 (0.011) SELECT "user_user"."id", "user_user"."password", "user_user"."last_login", "user_user"."is_superuser", "user_user"."username", "user_user"."is_staff", "user_user"."is_active", "user_user"."date_joined", "user_user"."email", "user_user"."first_name", "user_user"."last_name" FROM "user_user" INNER JOIN "courses_course_authors" ON ("user_user"."id" = "courses_course_authors"."user_id") WHERE "courses_course_authors"."course_id" = 1; args=(1,) django.db.backends
2020-07-26 22:59:47,168 INFO basehttp:154 018f23ade3774a44a7cd423c80f6fe59 "GET /api/courses/modules/1/ HTTP/1.1" 200 45 django.server

Still not confirm about caching of queryset for 1(list). Now we are good with 2.

Thanks Aniket