--- Model/ResourceModel/Indexer/Price.php	2024-11-09 10:39:19
+++ Model/ResourceModel/Indexer/Price.php	2024-11-07 20:32:50
@@ -6,18 +6,18 @@
 namespace Magento\Bundle\Model\ResourceModel\Indexer;

 use Magento\Catalog\Api\Data\ProductInterface;
-use Magento\Catalog\Model\ResourceModel\Product\Indexer\Price\BasePriceModifier;
-use Magento\Framework\DB\Select;
-use Magento\Framework\Indexer\DimensionalIndexerInterface;
-use Magento\Framework\EntityManager\MetadataPool;
 use Magento\Catalog\Model\Indexer\Product\Price\TableMaintainer;
-use Magento\Catalog\Model\ResourceModel\Product\Indexer\Price\IndexTableStructureFactory;
+use Magento\Catalog\Model\Product\Attribute\Source\Status;
+use Magento\Catalog\Model\ResourceModel\Product\Indexer\Price\BasePriceModifier;
 use Magento\Catalog\Model\ResourceModel\Product\Indexer\Price\IndexTableStructure;
+use Magento\Catalog\Model\ResourceModel\Product\Indexer\Price\IndexTableStructureFactory;
 use Magento\Catalog\Model\ResourceModel\Product\Indexer\Price\Query\JoinAttributeProcessor;
+use Magento\CatalogInventory\Model\Stock;
 use Magento\Customer\Model\Indexer\CustomerGroupDimensionProvider;
+use Magento\Framework\DB\Select;
+use Magento\Framework\EntityManager\MetadataPool;
+use Magento\Framework\Indexer\DimensionalIndexerInterface;
 use Magento\Store\Model\Indexer\WebsiteDimensionProvider;
-use Magento\Catalog\Model\Product\Attribute\Source\Status;
-use Magento\CatalogInventory\Model\Stock;

 /**
  * Bundle products Price indexer resource model
\ No newline at end of file
@@ -302,10 +302,10 @@
             ['cg' => $this->getTable('customer_group')],
             array_key_exists(CustomerGroupDimensionProvider::DIMENSION_NAME, $dimensions)
                 ? sprintf(
-                    '%s = %s',
-                    $this->dimensionToFieldMapper[CustomerGroupDimensionProvider::DIMENSION_NAME],
-                    $dimensions[CustomerGroupDimensionProvider::DIMENSION_NAME]->getValue()
-                ) : '',
+                '%s = %s',
+                $this->dimensionToFieldMapper[CustomerGroupDimensionProvider::DIMENSION_NAME],
+                $dimensions[CustomerGroupDimensionProvider::DIMENSION_NAME]->getValue()
+            ) : '',
             ['customer_group_id']
         )->joinInner(
             ['pw' => $this->getTable('catalog_product_website')],
\ No newline at end of file
@@ -518,25 +518,25 @@
         $bundleSelectionTable = $this->getBundleSelectionTable();

         $select = $this->getConnection()->select()
-        ->join(
-            ['i' => $this->getBundlePriceTable()],
-            "i.entity_id = $bundleSelectionTable.entity_id
+            ->join(
+                ['i' => $this->getBundlePriceTable()],
+                "i.entity_id = $bundleSelectionTable.entity_id
              AND i.customer_group_id = $bundleSelectionTable.customer_group_id
              AND i.website_id = $bundleSelectionTable.website_id",
-            []
-        )->join(
-            ['parent_product' => $this->getTable('catalog_product_entity')],
-            'parent_product.entity_id = i.entity_id',
-            []
-        )->join(
-            ['bo' => $this->getTable('catalog_product_bundle_option')],
-            "bo.parent_id = parent_product.$linkField AND bo.option_id = $bundleSelectionTable.option_id",
-            ['option_id']
-        )->join(
-            ['bs' => $this->getTable('catalog_product_bundle_selection')],
-            "bs.option_id = bo.option_id AND bs.selection_id = $bundleSelectionTable.selection_id",
-            ['selection_id']
-        );
+                []
+            )->join(
+                ['parent_product' => $this->getTable('catalog_product_entity')],
+                'parent_product.entity_id = i.entity_id',
+                []
+            )->join(
+                ['bo' => $this->getTable('catalog_product_bundle_option')],
+                "bo.parent_id = parent_product.$linkField AND bo.option_id = $bundleSelectionTable.option_id",
+                ['option_id']
+            )->join(
+                ['bs' => $this->getTable('catalog_product_bundle_selection')],
+                "bs.option_id = bo.option_id AND bs.selection_id = $bundleSelectionTable.selection_id",
+                ['selection_id']
+            );

         return $select;
     }
\ No newline at end of file
@@ -671,10 +671,9 @@
      * @return void
      * @throws \Exception
      */
-    private function calculateDynamicBundleSelectionPrice($dimensions)
+    private function calculateDynamicBundleSelectionPrice(array $dimensions): void
     {
         $connection = $this->getConnection();
-
         $price = 'idx.min_price * bs.selection_qty';
         $specialExpr = $connection->getCheckSql(
             'i.special_price > 0 AND i.special_price < 100',
\ No newline at end of file
@@ -716,8 +715,32 @@
             []
         );
         $select->where('si.stock_status = ?', Stock::STOCK_IN_STOCK);
+        $query = str_replace('AS `idx`', 'AS `idx` USE INDEX (PRIMARY)', (string) $select);
+        $insertColumns = [
+            'entity_id',
+            'customer_group_id',
+            'website_id',
+            'option_id',
+            'selection_id',
+            'group_type',
+            'is_required',
+            'price',
+            'tier_price'
+        ];
+        $insertColumns = array_map(function ($item) use ($connection) {
+            return $connection->quoteIdentifier($item);
+        }, $insertColumns);
+        $updateValues = [];
+        foreach ($insertColumns as $column) {
+            $updateValues[] = sprintf("%s = VALUES(%s)", $column, $column);
+        }

-        $this->tableMaintainer->insertFromSelect($select, $this->getBundleSelectionTable(), []);
+        $connection->query(sprintf(
+            "INSERT INTO `" . $this->getBundleSelectionTable() . "` (%s) %s ON DUPLICATE KEY UPDATE %s",
+            implode(",", $insertColumns),
+            $query,
+            implode(",", $updateValues)
+        ));
     }

     /**
\ No newline at end of file
@@ -764,7 +787,7 @@
             'tp.website_id = 0 OR tp.website_id = pw.website_id',
             ['website_id']
         )->joinLeft(
-            // customer group website limitations
+        // customer group website limitations
             ['cgw' => $this->getTable('customer_group_excluded_website')],
             'cg.customer_group_id = cgw.customer_group_id AND pw.website_id = cgw.website_id',
             []
\ No newline at end of file
@@ -866,7 +889,7 @@
         $select = $connection->select()->join(
             ['io' => $subSelect],
             'i.entity_id = io.entity_id AND i.customer_group_id = io.customer_group_id' .
-                ' AND i.website_id = io.website_id',
+            ' AND i.website_id = io.website_id',
             []
         )->columns(
             [
\ No newline at end of file
@@ -919,4 +942,4 @@
     {
         return $this->resource->getTableName($tableName, $this->connectionName);
     }
-}
+}
\ No newline at end of file