--- database.mysqli.inc	Wed Sep  9 03:20:41 2009
+++ database.mysqli.inc	Wed Sep  9 13:03:29 2009
@@ -105,6 +105,9 @@
     $query = '/* '. $name .' : '. $bt[2]['function'] .' */ '. $query;
   }
 
+  list($usec, $sec) = explode(' ', microtime());
+  $timer = (float)$usec + (float)$sec;
+
   $result = mysqli_query($active_db, $query);
 
   if (variable_get('dev_query', 0)) {
@@ -118,6 +121,14 @@
   if ($debug) {
     print '<p>query: '. $query .'<br />error:'. mysqli_error($active_db) .'</p>';
   }
+  
+  list($usec, $sec) = explode(' ', microtime());
+  $stop = (float)$usec + (float)$sec;
+  $diff = $stop - $timer;
+  
+  if (isset($diff) && $diff > 3) {
+    file_put_contents('slow.txt', $query . "\n\n\n" . $diff . parse_backtrace(debug_backtrace()) . "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n", FILE_APPEND);
+  }
 
   if (!mysqli_errno($active_db)) {
     return $result;
@@ -130,6 +141,16 @@
   }
 }
 
+function parse_backtrace($raw) {
+  $output="";
+  foreach($raw as $entry) {
+    $output.="\nFile: ".$entry['file']." (Line: ".$entry['line'].")\n";
+    $output.="Function: ".$entry['function']."\n";
+    $output.="Args: ".implode(", ", $entry['args'])."\n";
+  }
+  return $output;
+}
+
 /**
  * Fetch one result row from the previous query as an object.
  *
