Skip to content

Commit

Permalink
Fixed regression from getSelectCountSql optimization (OpenMage#3279)
Browse files Browse the repository at this point in the history
  • Loading branch information
colinmollenhour committed May 31, 2023
1 parent 1f54fc7 commit f8069c4
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions lib/Varien/Data/Collection/Db.php
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,10 @@ public function getSelectCountSql()
} else {
$countSelect->columns('COUNT(*)');

// Simple optimization - remove all joins if there are no where clauses using joined tables and all joins are left joins
// Simple optimization - remove all joins if:
// - there are no where clauses using joined tables
// - all joins are left joins
// - there are no join conditions using bind params (for simplicity)
$leftJoins = array_filter($countSelect->getPart(Zend_Db_Select::FROM), function ($table) {
return ($table['joinType'] == Zend_Db_Select::LEFT_JOIN || $table['joinType'] == Zend_Db_Select::FROM);
});
Expand All @@ -258,7 +261,16 @@ public function getSelectCountSql()
return !preg_match($pattern, $clause);
});
});
if (empty($whereUsingJoin)) {
if ($this->_bindParams) {
$bindParams = array_map(function ($token) {
return ltrim($token, ':');
}, array_keys($this->_bindParams));
$bindPattern = '/:('.implode('|', $bindParams).')/';
$joinUsingBind = array_filter($leftJoins, function ($table) use ($bindPattern) {
return !empty($table['joinCondition']) && preg_match($bindPattern, $table['joinCondition']);
});
}
if (empty($whereUsingJoin) && empty($joinUsingBind)) {
$from = array_slice($leftJoins, 0, 1);
$countSelect->setPart(Zend_Db_Select::FROM, $from);
}
Expand Down

0 comments on commit f8069c4

Please sign in to comment.