Skip to content

Commit 0eca2ca

Browse files
committed
MAGETWO-70406: Significant degradation of indexer with big number of customer groups
1 parent 6a33737 commit 0eca2ca

File tree

2 files changed

+32
-18
lines changed

2 files changed

+32
-18
lines changed

lib/internal/Magento/Framework/DB/Adapter/AdapterInterface.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ interface AdapterInterface
3535
const INSERT_ON_DUPLICATE = 1;
3636

3737
const INSERT_IGNORE = 2;
38+
39+
/** Strategy for updating data in table. See https://dev.mysql.com/doc/refman/5.7/en/replace.html */
40+
const REPLACE = 4;
3841

3942
const ISO_DATE_FORMAT = 'yyyy-MM-dd';
4043

lib/internal/Magento/Framework/DB/Adapter/Pdo/Mysql.php

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3368,8 +3368,9 @@ public function enableTableKeys($tableName, $schemaName = null)
33683368
*/
33693369
public function insertFromSelect(Select $select, $table, array $fields = [], $mode = false)
33703370
{
3371-
$query = 'INSERT';
3372-
if ($mode == self::INSERT_IGNORE) {
3371+
$query = $mode === self::REPLACE ? 'REPLACE' : 'INSERT';
3372+
3373+
if ($mode === self::INSERT_IGNORE) {
33733374
$query .= ' IGNORE';
33743375
}
33753376
$query = sprintf('%s INTO %s', $query, $this->quoteIdentifier($table));
@@ -3380,26 +3381,36 @@ public function insertFromSelect(Select $select, $table, array $fields = [], $mo
33803381

33813382
$query = sprintf('%s %s', $query, $select->assemble());
33823383

3383-
if ($mode == self::INSERT_ON_DUPLICATE) {
3384-
if (!$fields) {
3385-
$describe = $this->describeTable($table);
3386-
foreach ($describe as $column) {
3387-
if ($column['PRIMARY'] === false) {
3388-
$fields[] = $column['COLUMN_NAME'];
3389-
}
3390-
}
3391-
}
3392-
$update = [];
3393-
foreach ($fields as $field) {
3394-
$update[] = sprintf('%1$s = VALUES(%1$s)', $this->quoteIdentifier($field));
3395-
}
3384+
if ($mode === self::INSERT_ON_DUPLICATE) {
3385+
$query .= $this->renderOnDuplicate($table, $fields);
3386+
}
33963387

3397-
if ($update) {
3398-
$query = sprintf('%s ON DUPLICATE KEY UPDATE %s', $query, join(', ', $update));
3388+
return $query;
3389+
}
3390+
3391+
/**
3392+
* Render On Duplicate query part
3393+
*
3394+
* @param string $table
3395+
* @param array $fields
3396+
* @return string
3397+
*/
3398+
private function renderOnDuplicate($table, array $fields)
3399+
{
3400+
if (!$fields) {
3401+
$describe = $this->describeTable($table);
3402+
foreach ($describe as $column) {
3403+
if ($column['PRIMARY'] === false) {
3404+
$fields[] = $column['COLUMN_NAME'];
3405+
}
33993406
}
34003407
}
3408+
$update = [];
3409+
foreach ($fields as $field) {
3410+
$update[] = sprintf('%1$s = VALUES(%1$s)', $this->quoteIdentifier($field));
3411+
}
34013412

3402-
return $query;
3413+
return count($update) ? ' ON DUPLICATE KEY UPDATE ' . join(', ', $update) : '';
34033414
}
34043415

34053416
/**

0 commit comments

Comments
 (0)