Login

django database snippet

Author:
ItsRLuo
Posted:
March 18, 2021
Language:
Python
Version:
3.0
Tags:
log database
Score:
1 (after 1 ratings)

..

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
Found a django middleware snippet while looking at reducing db calls.
# -*- coding: utf-8 -*-

from __future__ import print_function
try:
    from django.utils.deprecation import MiddlewareMixin as Parent
except ImportError:
    Parent = object

import textwrap

from django.conf import settings
from pygments import highlight, lexers, formatters
from pygments_pprint_sql import SqlFilter


class SqlPrintMiddleware(Parent):
    """Output the sql used in the view.
    """
    def process_response(self, request, response):
        """Output all the sql queries for the Django orm.
        """
        from django.db import connection
        should_run = getattr(settings, 'SQLPRINT_MIDDLEWARE', True)
        if not should_run:
            return response
        x_db_hits = getattr(settings, 'X_DB_HITS', True)
        runnable = settings.DEBUG
        max_queries = getattr(settings, 'SQLPRINT_MAX_QUERIES', 1200)
        min_queries = getattr(settings, 'SQLPRINT_MIN_QUERIES', 0)

        queries = connection.queries
        dbhits = len(queries)
        if runnable and x_db_hits:
            response['X-DB-hits'] = str(dbhits)

        if runnable:
            if dbhits > min_queries:
                self.print_queries(request, queries)

            if max_queries and dbhits > max_queries:
                raise RuntimeError(textwrap.dedent("""\
                    A single request caused {dbhits} db hits, which is more than
                    settings.SQLPRINT_MAX_QUERIES
                    """.format(dbhits=dbhits)))

        return response

    def print_queries(self, request, queries):
        """Output all the queries.
        """
        lexer = lexers.MySqlLexer()
        lexer.add_filter(SqlFilter())

        totsecs = 0.0
        for query in queries:
            print(query['time'], 'used on:')
            totsecs += float(query['time'])
            print(highlight(query['sql'], lexer, formatters.TerminalFormatter()))

        print('Number of queries:', len(queries))
        print('Total time:', totsecs)

More like this

  1. "Magic Link" Management Command by webology 3 weeks, 4 days ago
  2. Closest ORM models to a latitude/longitude point by simonw 3 weeks, 4 days ago
  3. Log the time taken to execute each DB query by kennyx46 3 weeks, 4 days ago
  4. django database snippet by ItsRLuo 1 month ago
  5. Serialize a model instance by chriswedgwood 1 month, 4 weeks ago

Comments

Please login first before commenting.