Skip to content

Optimize DB queries for Events API end-point #763

@batpad

Description

@batpad

Relates to #708

We should check the database queries being generated for the Events end-point, and use .select_related and .prefetch_related to reduce any excess queries.

In general, to do query profiling:

  • Ensure Django Debug Toolbar is working, I just merged Django debug #682, we may need to test that it works under all local development scenarios.
  • Once Django Debug Toolbar is working, you can visit an API end-point like
  • Click on DjDT toolbar icon on the top-right of the page, and go to the Queries tab.

Inspecting the queries and SQL generated should give you a good idea if any of the related object lookups are being done in a loop instead of being fetched along-with the initial query.

To optimize, we'd add .select_related() and .prefetch_related calls to the ORM call in the get_queryset method for the view, and then one can re-inspect the queries in the Django Debug Toolbar to verify that the number of queries and overall query times have reduced.

get_queryset method of the View: https://github.com/IFRCGo/go-api/blob/develop/api/drf_views.py#L251

Django docs: https://docs.djangoproject.com/en/3.0/ref/models/querysets/#select-related

Metadata

Metadata

Labels

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions