Skip to content

Commit a1f14a1

Browse files
committed
Avoid duplicates with > 1000 changelog entries.
If an import modifies 2000 products, and then changes their category allocation, etc. there may be many duplicates, but not within a range of 1000 changelog entries. This loads all the ids in one batch (which should be relatively cheap memory and time wise), and then runs over them with the indexer in smaller chunks. This way indexers continue to seem the small chunk sizes, in case they would fail with too many ids.
1 parent 205a408 commit a1f14a1

File tree

1 file changed

+13
-3
lines changed
  • lib/internal/Magento/Framework/Mview

1 file changed

+13
-3
lines changed

lib/internal/Magento/Framework/Mview/View.php

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ class View extends \Magento\Framework\DataObject implements ViewInterface
2121
*/
2222
const DEFAULT_BATCH_SIZE = 1000;
2323

24+
/**
25+
* Max versions to load from database at a time
26+
*/
27+
const MAX_VERSION_QUERY_BATCH = 100000;
28+
2429
/**
2530
* @var string
2631
*/
@@ -272,14 +277,19 @@ public function update()
272277
try {
273278
$this->getState()->setStatus(View\StateInterface::STATUS_WORKING)->save();
274279

280+
$versionBatchSize = self::MAX_VERSION_QUERY_BATCH;
275281
$batchSize = isset($this->changelogBatchSize[$this->getChangelog()->getViewId()])
276282
? $this->changelogBatchSize[$this->getChangelog()->getViewId()]
277283
: self::DEFAULT_BATCH_SIZE;
278284

279-
for ($versionFrom = $lastVersionId; $versionFrom < $currentVersionId; $versionFrom += $batchSize) {
280-
$ids = $this->getChangelog()->getList($versionFrom, $versionFrom + $batchSize);
285+
for ($versionFrom = $lastVersionId; $versionFrom < $currentVersionId; $versionFrom += $versionBatchSize) {
286+
// Don't go past the current version for atomicy.
287+
$versionTo = min($currentVersionId, $versionFrom + $versionBatchSize);
288+
$ids = $this->getChangelog()->getList($versionFrom, $versionTo);
281289

282-
if (!empty($ids)) {
290+
// We run the actual indexer in batches. Chunked AFTER loading to avoid duplicates in separate chunks.
291+
$chunks = array_chunk($ids, $batchSize);
292+
foreach ($chunks as $ids) {
283293
$action->execute($ids);
284294
}
285295
}

0 commit comments

Comments
 (0)