Ticket #5236: 5236.diff

File 5236.diff, 3.2 KB (added by Philippe Raoult, 17 years ago)

patch slightly cleaned up, but mostly documented in the FAQ

  • django/db/backends/util.py

     
    11import datetime
    22import md5
     3import traceback
    34from time import time
    45from django.utils.encoding import smart_unicode, force_unicode
    56
     
    1819        try:
    1920            return self.cursor.execute(sql, params)
    2021        finally:
    21             stop = time()
    22             self.db.queries.append({
    23                 'sql': smart_unicode(sql) % convert_args(params),
    24                 'time': "%.3f" % (stop - start),
    25             })
     22            self.trace_entry(smart_unicode(sql) % convert_args(params), start)
    2623
    2724    def executemany(self, sql, param_list):
    2825        start = time()
    2926        try:
    3027            return self.cursor.executemany(sql, param_list)
    3128        finally:
    32             stop = time()
    33             self.db.queries.append({
    34                 'sql': 'MANY: ' + sql + ' ' + smart_unicode(tuple(param_list)),
    35                 'time': "%.3f" % (stop - start),
    36             })
     29            self.trace_entry('MANY: ' + sql + ' ' + smart_unicode(tuple(param_list)), start)
    3730
     31    def trace_entry (self, sql, start):
     32        stop = time()
     33        entry = {
     34            'sql': sql,
     35            'time': "%.3f" % (stop - start),
     36            # remove ourselves from the stack
     37            'stack': traceback.format_stack()[:-2],
     38        }
     39        self.db.queries.append(entry)
     40
    3841    def __getattr__(self, attr):
    3942        if attr in self.__dict__:
    4043            return self.__dict__[attr]
  • docs/faq.txt

     
    447447this::
    448448
    449449    >>> from django.db import connection
    450     >>> connection.queries
    451     [{'sql': 'SELECT polls_polls.id,polls_polls.question,polls_polls.pub_date FROM polls_polls',
    452     'time': '0.002'}]
     450    >>> len(connection.queries)
     451    12
     452    >>> connection.queries[0]['sql']
     453    u'SELECT "polls_poll"."id","polls_poll"."question","polls_poll"."pub_date" FROM
     454    "polls_poll"'
     455    >>> connection.queries[0]['time']
     456    '0.000'
     457    >>> type(connection.queries[0]['stack'])
     458    <type 'list'>
     459    >>> connection.queries[0]['stack'][-1]
     460    '  File "H:\\cygwin\\home\\Philippe\\django\\trunk\\django\\db\\models\\query.py",
     461    line 189, in iterator\ncursor.execute("SELECT " + (self._distinct and "DISTINCT "
     462    or "") + ",".join(select) + sql, params)\n'
     463    >>> connection.queries[0]['stack'][0]
     464    '  File "./manage.py", line 11, in <module>\n    execute_manager(settings)\n'
    453465
    454466``connection.queries`` is only available if ``DEBUG`` is ``True``. It's a list
    455467of dictionaries in order of query execution. Each dictionary has the following::
    456468
    457469    ``sql`` -- The raw SQL statement
    458     ``time`` -- How long the statement took to execute, in seconds.
     470    ``time`` -- How long the statement took to execute, in seconds
     471    ``stack`` -- the stack, as a list of strings.
    459472
    460473``connection.queries`` includes all SQL statements -- INSERTs, UPDATES,
    461474SELECTs, etc. Each time your app hits the database, the query will be recorded.
Back to Top