From f8069c4333d24a8602a47bec2147e7fab561d7b4 Mon Sep 17 00:00:00 2001 From: Colin Mollenhour Date: Wed, 31 May 2023 02:53:42 -0400 Subject: [PATCH] Fixed regression from getSelectCountSql optimization (#3279) --- lib/Varien/Data/Collection/Db.php | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/lib/Varien/Data/Collection/Db.php b/lib/Varien/Data/Collection/Db.php index b20a4791d38..341325568dc 100644 --- a/lib/Varien/Data/Collection/Db.php +++ b/lib/Varien/Data/Collection/Db.php @@ -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); }); @@ -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); }