2

I'd like to have a Django application record how much time each SQL query took.

The first problem is that SQL queries differ, even when they originate from the same code. That can be solved by normalizing them, so that

SELECT first_name, last_name FROM people WHERE NOW() - birth_date < interval '20' years;

would become something like

SELECT $ FROM people WHERE $ - birth_date < $;

After getting that done, we could just log the normalized query and the query timing to a file, syslog or statsd (for statsd, I'd probably also use a hash of the query as a key, and keep an index of hash->query relations elsewhere).

The bigger problem, however, is figuring out where that action can be performed. The best place for that I could find is this: https://github.com/django/django/blob/b5bacdea00c8ca980ff5885e15f7cd7b26b4dbb9/django/db/backends/util.py#L46 (note: we do use that ancient version of Django, but I'm fine with suggestions that are relevant only to newer versions).

Ideally, I'd like to make this a Django extension, rather than modifying Django source code. Sounds like I can make another backend, inheriting from the one we currently use, and make its CursorWrapper's class execute method record the timing and counter.

Is that the right approach, or should I be using some other primitives, like QuerySet or something?

demeshchuk
  • 742
  • 4
  • 10
  • 1
    Look at [this Q&A](http://stackoverflow.com/q/4375784/223424); full logging with timestamps + debug toolbar should help. – 9000 Mar 04 '17 at 00:37

1 Answers1

0

Django debug toolbar has a panel that shows "SQL queries including time to execute and links to EXPLAIN each query" http://django-debug-toolbar.readthedocs.io/en/stable/panels.html#sql

  • 1
    If I understand correctly, this suggests using `settings.DEBUG = True`, which is a strong no-no for production. Or am I missing something? – demeshchuk Mar 05 '17 at 19:09