MikeN
MikeN

Reputation: 46357

Django slow queries: Connect django filter statements to slow queries in database logs

If you are trying to diagnose slow queries in your mysql backend and are using a Django frontend, how do you tie together the slow queries reported by the backend with specific querysets in the Django frontend code?

Upvotes: 0

Views: 1367

Answers (2)

Chris Pratt
Chris Pratt

Reputation: 239430

If you install django-devserver, it will show you the queries that are being run and the time they take in your shell when using runserver.

Another alternative is django-debug-toolbar, which will do the same in a side panel-overlay on your site.

Either way, you'll need to test it out in your development environment. However, neither really solves the issue of pinpointing you directly to the offending queries; they work on a per-request basis. As a result, you'll have to do a little thinking about which of your views are using the database most heavily and/or deal with exceptionally large amounts of data, but by cherry-picking likely-candidate views and inspecting the times for the queries to run on those pages, you should be able to get a handle on which particular queries are the worst.

Upvotes: 1

Paulo Scardine
Paulo Scardine

Reputation: 77359

I think you has no alternative besides logging every django query for the suspicious querysets.

See this answer on how to access the actual query for a given queryset.

Upvotes: 1

Related Questions