Fetching list of SQL queries executed so far for all requests

 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# file: settings.py

MIDDLEWARE_CLASSES = (
    'django.middleware.common.CommonMiddleware',
    'yourapp.middleware.SqlProfilingMiddleware',
    ...
)



# file: yourapp/midddlware.py

from django.db import connection
import time

class SqlProfilingMiddleware(object):
    Queries = []

    def process_request(self, request):
        return None
    def process_view(self, request, view_func, view_args, view_kwargs):
        return None
    def process_template_response(self, request, response):
        self._add_sql_queries(request)
        return response
    def process_response(self, request, response):
        self._add_sql_queries(request)
        return response
    def process_exception(self, request, exception):
        return None

    def _add_sql_queries(self, request):
        for q in connection.queries:
            q["time"] = time.time() + float(q["time"])
            SqlProfilingMiddleware.Queries.insert(0, q)
            # add request info as a separator
        SqlProfilingMiddleware.Queries.insert(0, {"time": time.time(), "path" : request.path})



# file: yourapp/views.py

from yourapp.middleware import SqlProfilingMiddleware

def profiling(request):
    return render_to_response("profiling.html", {"queries": SqlProfilingMiddleware.Queries})



# file: urls.py

urlpatterns = patterns('',
  (r'^profiling/$', 'yourapp.views.profiling'),
)



# file: yourapp/templates/profiling.html


        <div id="profiling">
            <h2>SQL queries</h2>
            <table cellpadding="0" cellspacing="0">
                <thead>
                    <tr>
                       <th>Time</th>
                       <th>SQL</th>
                    </tr>
                </thead>
                <tbody>
                {% for q in queries %}
                    {% if q.path %}
                    <tr class="path">
                        <td >{{ q.time }}</td>
                        <td>{{ q.path }}</td>
                    </tr>
                    {% else %}
                    <tr class="sql">
                        <td >{{ q.time }}</td>
                        <td>{{ q.sql }}</td>
                    </tr>
                    {% endif %}
                {% endfor %}
                </tbody>
            </table>
        </div>

More like this

  1. Debug middleware for displaying sql queries and template loading info when ?debug=true by SEJeff 3 years ago
  2. More informative error mailings by kcarnold 6 years, 1 month ago
  3. Profiling Middlware by udfalkso 7 years ago
  4. Debug SQL Query in Template by dario.agliottone 1 year, 11 months ago
  5. Query printer coroutine by fnl 5 years ago

Comments

aisbaa (on December 18, 2011):

This snippet could be great app.

#

(Forgotten your password?)