Mikkel Kamstrup Erlandsen has proposed merging 
lp:~zeitgeist/zeitgeist/explain-logs into lp:zeitgeist.

Requested reviews:
  Zeitgeist Framework Team (zeitgeist)
Related bugs:
  #632363 Slow queries: SQL indexes not used
  https://bugs.launchpad.net/bugs/632363


Part 1) of our profiling quest can be merged now since it quite unintrusive and 
very useful in its own right
-- 
https://code.launchpad.net/~zeitgeist/zeitgeist/explain-logs/+merge/35334
Your team Zeitgeist Framework Team is requested to review the proposed merge of 
lp:~zeitgeist/zeitgeist/explain-logs into lp:zeitgeist.
=== modified file '_zeitgeist/engine/sql.py'
--- _zeitgeist/engine/sql.py	2010-05-28 07:38:51 +0000
+++ _zeitgeist/engine/sql.py	2010-09-13 21:01:49 +0000
@@ -23,6 +23,7 @@
 import sqlite3
 import logging
 import time
+import os
 
 from _zeitgeist.engine import constants
 
@@ -35,20 +36,28 @@
 	"subj_uri": "uri",
 }
 
+def explain_query(cursor, statement, arguments=()):
+	log.debug("**********************************************")
+	log.debug("QUERY:\n%s (%s)\nPLAN:" % (statement,arguments))
+	for r in cursor.execute("EXPLAIN QUERY PLAN "+statement, arguments).fetchall():
+		print r
+	log.debug("**********************************************")
+
 class UnicodeCursor(sqlite3.Cursor):
 	
+	debug_explain = os.getenv("ZEITGEIST_DEBUG_QUERY_PLANS")
+	
 	@staticmethod
 	def fix_unicode(obj):
 		if isinstance(obj, str):
 			obj = obj.decode("UTF-8")
 		return unicode(obj)		
 	
-	def execute(self, statement, parameters=None):
-		if parameters is not None:
-			parameters = [self.fix_unicode(p) for p in parameters]
-			return super(UnicodeCursor, self).execute(statement, parameters)
-		else:
-			return super(UnicodeCursor, self).execute(statement)
+	def execute(self, statement, parameters=()):
+		parameters = [self.fix_unicode(p) for p in parameters]
+		if UnicodeCursor.debug_explain:
+			explain_query(super(UnicodeCursor, self), statement, parameters)
+		return super(UnicodeCursor, self).execute(statement, parameters)
 
 def _get_schema_version (cursor, schema_name):
 	"""

_______________________________________________
Mailing list: https://launchpad.net/~zeitgeist
Post to     : zeitgeist@lists.launchpad.net
Unsubscribe : https://launchpad.net/~zeitgeist
More help   : https://help.launchpad.net/ListHelp

Reply via email to