Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge pull request #1532 from mahagr/getlistcount

Fix performance issues in JModelLegacy::_getListCount()
  • Loading branch information...
commit bb21e3aed1c49ec229d11193fc0a62675b918b77 2 parents 5da2f90 + ae88e05
@pasamio pasamio authored
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();
+ }
}
/**
Please sign in to comment.
Something went wrong with that request. Please try again.