Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

Fix performance issues in JModelLegacy::_getListCount() #1532

Merged
merged 3 commits into from Sep 16, 2012
@@ -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();
+ }
}
/**