Permalink
Browse files

Fix performance issues in JModelLegacy::_getListCount()

  • Loading branch information...
1 parent 1d62cf3 commit da608f1fbcd0eb324d4fb729bcd923111c8b47d1 @mahagr committed Sep 15, 2012
Showing with 18 additions and 3 deletions.
  1. +18 −3 libraries/legacy/model/legacy.php
View
21 libraries/legacy/model/legacy.php
@@ -313,10 +313,25 @@ protected function _getList($query, $limitstart = 0, $limit = 0)
*/
protected function _getListCount($query)
{
- $this->_db->setQuery($query);
- $this->_db->execute();
+ if ($query instanceof JDatabaseQuery)
+ {
+ // Create COUNT(*) query to allow database engine to optimize the query.
+ $query = clone $query;
+ $query->clear('select')->clear('order')->select('COUNT(*)');
+ $this->_db->setQuery($query);
+
+ return (int) $this->_db->loadResult();
+ }
+ else
+ {
+ // Performance of this query is very bad as it forces database engine to go
+ // through all items in the database. If you don't use JDatabaseQuery object,
+ // you should override this function in your model.
+ $this->_db->setQuery($query);
+ $this->_db->execute();
- return $this->_db->getNumRows();
+ return $this->_db->getNumRows();
+ }
}
/**

0 comments on commit da608f1

Please sign in to comment.