Last active
August 29, 2015 14:04
-
-
Save temoto/430364eb3e5df58d4115 to your computer and use it in GitHub Desktop.
Django SQL DB query statistics. Now you may find https://pypi.python.org/pypi/django-devserver SQLSummaryModule better.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
try: | |
# DB profiling. | |
# TODO: extract this to separate module. | |
TERMINAL_WIDTH = 124 # fine for 15" in 12px mono font. | |
SHOW_TOP_N_QUERIES = 5 | |
import itertools | |
import textwrap | |
queries = django.db.connection.queries | |
if queries: | |
queries = [ dict(q, time=int(float(q['time']) * 1000)) for q in queries ] | |
queries.sort(key=lambda q: q['sql']) | |
query_groups = [ (k, [q['time'] for q in g]) for k, g in itertools.groupby(queries, lambda q: q['sql']) ] | |
total_time = sum( q['time'] for q in queries ) | |
top_groups = sorted(query_groups, key=lambda (_q, t): (len(t), sum(t)), reverse=True)[:SHOW_TOP_N_QUERIES] | |
print u"--- DB: {count} queries in {num_groups} groups, total time: {time}ms. Top {num_top_groups} query groups:".format( | |
count=len(queries), num_groups=len(query_groups), time=total_time, num_top_groups=SHOW_TOP_N_QUERIES) | |
for q, t in top_groups: | |
q = q.decode('utf-8') | |
print u" >> repeats: {count}, took {time}ms".format(count=len(t), time=sum(t)) | |
print textwrap.fill(q, width=TERMINAL_WIDTH, initial_indent=u' ', subsequent_indent=u' ') | |
except Exception, e: | |
try: | |
print u"*** Error in DB profiler: {0!s}".format(e) | |
except Exception: | |
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- DB: 13 queries in 4 groups, total time: 8ms. Top 5 query groups: | |
>> repeats: 6, took 4ms | |
SELECT "blog_menuitem"."id", "blog_menuitem"."title", "blog_menuitem"."link_type", "blog_menuitem"."value", | |
"blog_menuitem"."parent_id", "blog_menuitem"."sort", "blog_menuitem"."visible" FROM "blog_menuitem" WHERE | |
"blog_menuitem"."id" = 9 | |
>> repeats: 4, took 2ms | |
SELECT "blog_menuitem"."id", "blog_menuitem"."title", "blog_menuitem"."link_type", "blog_menuitem"."value", | |
"blog_menuitem"."parent_id", "blog_menuitem"."sort", "blog_menuitem"."visible" FROM "blog_menuitem" WHERE | |
"blog_menuitem"."id" = 4 | |
>> repeats: 2, took 1ms | |
SELECT "blog_menuitem"."id", "blog_menuitem"."title", "blog_menuitem"."link_type", "blog_menuitem"."value", | |
"blog_menuitem"."parent_id", "blog_menuitem"."sort", "blog_menuitem"."visible" FROM "blog_menuitem" WHERE | |
"blog_menuitem"."id" = 10 | |
>> repeats: 1, took 1ms | |
SELECT "blog_menuitem"."id", "blog_menuitem"."title", "blog_menuitem"."link_type", "blog_menuitem"."value", | |
"blog_menuitem"."parent_id", "blog_menuitem"."sort", "blog_menuitem"."visible" FROM "blog_menuitem" WHERE | |
"blog_menuitem"."visible" = true | |
127.0.0.1 - - [02/Dec/2010 17:07:59] "GET /_block/main-menu HTTP/1.0" 200 - |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment