Ticket #5236: 5236.diff
File 5236.diff, 3.2 KB (added by , 17 years ago) |
---|
-
django/db/backends/util.py
1 1 import datetime 2 2 import md5 3 import traceback 3 4 from time import time 4 5 from django.utils.encoding import smart_unicode, force_unicode 5 6 … … 18 19 try: 19 20 return self.cursor.execute(sql, params) 20 21 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) 26 23 27 24 def executemany(self, sql, param_list): 28 25 start = time() 29 26 try: 30 27 return self.cursor.executemany(sql, param_list) 31 28 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) 37 30 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 38 41 def __getattr__(self, attr): 39 42 if attr in self.__dict__: 40 43 return self.__dict__[attr] -
docs/faq.txt
447 447 this:: 448 448 449 449 >>> 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' 453 465 454 466 ``connection.queries`` is only available if ``DEBUG`` is ``True``. It's a list 455 467 of dictionaries in order of query execution. Each dictionary has the following:: 456 468 457 469 ``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. 459 472 460 473 ``connection.queries`` includes all SQL statements -- INSERTs, UPDATES, 461 474 SELECTs, etc. Each time your app hits the database, the query will be recorded.