Herding your database queries: diagnosing, improving and guarding performance of DB interactions

Optimize database queries in Django apps with middleware, prefetching, serializers, caching, unit tests, observability, logging, and coverage context.

Key takeaways
  • Middleware can be used to track database queries and collect statistics such as the number of queries per request and the time spent in database calls.
  • Select-related and prefetch-related are two techniques for optimizing database queries by pre-fetching related data.
  • Select-related is used to retrieve related objects in the same query as the primary object.
  • Prefetch-related is used to retrieve related objects in a separate query, but only once for all instances of the primary object.
  • Serializers can be used to control the data that is returned from a database query.
  • Caching can be used to improve the performance of database queries by storing the results of frequently executed queries in memory.
  • Unit tests can be used to ensure that database queries are executed as expected and to detect performance regressions.
  • Observability is important for understanding the performance of database queries and identifying potential problems.
  • Logging can be used to track database queries and collect statistics such as the number of queries per request and the time spent in database calls.
  • Coverage context can be used to identify which parts of the code are responsible for executing database queries.