Got a question?  Ask a developer in our Developer's Corner Forum

Back To The Developer's Corner Main Page

Display Joomla! Database Query Runtimes With Debug Info

Enabling debugging in Joomla! is great for seeing which SQL queries run, but I wanted to know which ones were taking the longest.  This is how I modified the core files to display runtimes:

.../libraries/joomla/database/database/mysql.php

function query()
{
	if (!is_resource($this->_resource)) {
		return false;
	}

	// Take a local copy so that we don't modify the original query and cause issues later
	$sql = $this->_sql;
	if ($this->_limit > 0 || $this->_offset > 0) {
		$sql .= ' LIMIT '.$this->_offset.', '.$this->_limit;
	}
	// MOVED ABOVE if/LOG SECTION
	$this->_errorNum = 0;
	$this->_errorMsg = '';
	$starttime = microtime(true); // ADDED
	$this->_cursor = mysql_query( $sql, $this->_resource );
	$endtime = microtime(true); // ADDED
	if ($this->_debug) {
		$this->_ticker++;
		// ADDED ALL THE timediff LOGIC
		$timediff = 1000000*($endtime - $starttime);
		if($timediff > 1000)
		{
			$timediff = "!!!!! ".$timediff;
		}
		$this->_log[] = "(".$timediff."): ".$sql;
		//$this->_log[] = $sql;
	}

	if (!$this->_cursor)
	{
		$this->_errorNum = mysql_errno( $this->_resource );
		$this->_errorMsg = mysql_error( $this->_resource )." SQL=$sql";

		if ($this->_debug) {
			JError::raiseError(500, 'JDatabaseMySQL::query: '.
				$this->_errorNum.' - '.$this->_errorMsg );
		}
		return false;
	}
	return $this->_cursor;
}

After making these changes, I now get the amount of time it takes the queries to run as well as the SQL.  Then I can search my page for "!!!!!" to find any queries that take longer than 1ms.

 

Hope this helps others.

 

 

Part of the SourceCoast Network:

SourceCoast / Cooking Allergy Free / CMS Market

© 2010 CMS Market. All rights reserved.

Joomla Facebook integration by JFBConnect

GTranslate Joomla Module