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

Conversation

Projects
None yet
2 participants
Contributor

mahagr commented Sep 15, 2012

Current code in JModelLegacy::_getListCount() forces database engine to fetch all items instead of just counting them. With hundreds of thousands or millions of rows the query gets very inefficient even if no results are actually fetched by the function.

Cloning and modifying JDatabaseQuery object into COUNT(*) allows database engine to do optimizations that will considerably speed up the code even with very simple queries like:

SELECT * FROM jos_users

if there are hundreds of thousands of users.

@pasamio pasamio commented on an outdated diff Sep 15, 2012

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);
+
@pasamio

pasamio Sep 15, 2012

Contributor

Can you remove the white space on this line?

Contributor

mahagr commented Sep 16, 2012

Sorry, project settings were wrong. Now it should be better.

pasamio added a commit that referenced this pull request Sep 16, 2012

Merge pull request #1532 from mahagr/getlistcount
Fix performance issues in JModelLegacy::_getListCount()

@pasamio pasamio merged commit bb21e3a into joomla:staging Sep 16, 2012

Contributor

pasamio commented Sep 16, 2012

Sweet! Nice addition!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment