Enhanced the PDO Logstream Driver for better performance on large databases.

On MYSQL and POSTGRES, the PDO Logstream does not uses the LIMIT
statement to minimize database usage.
This commit is contained in:
Andre Lorbach 2008-08-27 16:41:53 +02:00
parent 54dcacb25e
commit d768ed6068
2 changed files with 31 additions and 6 deletions

View File

@ -724,6 +724,7 @@ class LogStreamDB extends LogStream {
// Append LIMIT clause // Append LIMIT clause
$szSql .= " LIMIT " . $this->_currentRecordStart . ", " . $this->_logStreamConfigObj->RecordsPerQuery; $szSql .= " LIMIT " . $this->_currentRecordStart . ", " . $this->_logStreamConfigObj->RecordsPerQuery;
echo $szSql . "<br>";
// Perform Database Query // Perform Database Query
$myquery = mysql_query($szSql, $this->_dbhandle); $myquery = mysql_query($szSql, $this->_dbhandle);

View File

@ -257,8 +257,9 @@ class LogStreamPDO extends LogStream {
// Now read new ones // Now read new ones
$ret = $this->ReadNextRecordsFromDB($uID); $ret = $this->ReadNextRecordsFromDB($uID);
//echo "1mowl " . $this->_currentRecordStart . "=" . $this->_currentRecordNum; //echo "!" . $ret . " " . $this->_currentRecordStart . "=" . $this->_currentRecordNum;
// Check if we found more records
if ( !isset($this->bufferedRecords[$this->_currentRecordNum] ) ) if ( !isset($this->bufferedRecords[$this->_currentRecordNum] ) )
$ret = ERROR_NOMORERECORDS; $ret = ERROR_NOMORERECORDS;
} }
@ -679,11 +680,21 @@ class LogStreamPDO extends LogStream {
global $querycount; global $querycount;
// create query if necessary! // create query if necessary!
if ( $this->_myDBQuery == null ) // if ( $this->_myDBQuery == null )
{ {
// Get SQL Statement // Get SQL Statement
$szSql = $this->CreateSQLStatement($uID); $szSql = $this->CreateSQLStatement($uID);
// --- Append LIMIT if supported by the driver! Why the hell do we still have no unified solution for this crap in the sql language?!
if ( $this->_logStreamConfigObj->DBType == DB_MYSQL )
$szSql .= " LIMIT " . $this->_logStreamConfigObj->RecordsPerQuery;
// $szSql .= " LIMIT " . $this->_currentRecordStart . ", " . $this->_logStreamConfigObj->RecordsPerQuery;
else if ( $this->_logStreamConfigObj->DBType == DB_PGSQL )
$szSql .= " LIMIT " . $this->_logStreamConfigObj->RecordsPerQuery;
// $szSql .= " LIMIT " . $this->_logStreamConfigObj->RecordsPerQuery . " OFFSET " . $this->_currentRecordStart;
// echo $szSql . "<br>";
// ---
// Perform Database Query // Perform Database Query
$this->_myDBQuery = $this->_dbhandle->query($szSql); $this->_myDBQuery = $this->_dbhandle->query($szSql);
if ( !$this->_myDBQuery ) if ( !$this->_myDBQuery )
@ -691,6 +702,16 @@ class LogStreamPDO extends LogStream {
$this->PrintDebugError( "Invalid SQL: ".$szSql . "<br><br>Errorcode: " . $this->_dbhandle->errorCode() ); $this->PrintDebugError( "Invalid SQL: ".$szSql . "<br><br>Errorcode: " . $this->_dbhandle->errorCode() );
return ERROR_DB_QUERYFAILED; return ERROR_DB_QUERYFAILED;
} }
else
{
// Skip one entry in this case
if ( $this->_currentRecordStart > 0 )
{
// Throw away
$myRow = $this->_myDBQuery->fetch(PDO::FETCH_ASSOC);
}
}
// Increment for the Footer Stats // Increment for the Footer Stats
$querycount++; $querycount++;
@ -709,7 +730,7 @@ class LogStreamPDO extends LogStream {
if ( $this->_myDBQuery != null ) if ( $this->_myDBQuery != null )
{ {
// Free Query ressources // Free Query ressources
// $this->_myDBQuery->closeCursor(); $this->_myDBQuery->closeCursor();
$this->_myDBQuery = null; $this->_myDBQuery = null;
} }
@ -722,8 +743,11 @@ class LogStreamPDO extends LogStream {
*/ */
private function ReadNextRecordsFromDB($uID) private function ReadNextRecordsFromDB($uID)
{ {
// Clear SQL Query first!
$this->DestroyMainSQLQuery();
// Create query if necessary // Create query if necessary
if ( $this->_myDBQuery == null ) // if ( $this->_myDBQuery == null )
{ {
// return error if there was one! // return error if there was one!
if ( ($res = $this->CreateMainSQLQuery($uID)) != SUCCESS ) if ( ($res = $this->CreateMainSQLQuery($uID)) != SUCCESS )