Skip to content

Commit 6147ee4

Browse files
authored
Merge pull request #18 from fredden/large-data-set
Replace use of fetchAll() for database queries
2 parents d16dbeb + 428e642 commit 6147ee4

File tree

3 files changed

+66
-42
lines changed

3 files changed

+66
-42
lines changed

Console/Command/CleanUpAttributesAndValuesWithoutParentCommand.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,12 +85,12 @@ public function execute(InputInterface $input, OutputInterface $output): int
8585
foreach ($types as $type) {
8686
$eavTable = $this->resourceConnection->getTableName('eav_attribute');
8787
$entityValueTable = $this->resourceConnection->getTableName($code . '_entity_' . $type);
88-
$query = "SELECT * FROM $entityValueTable WHERE `attribute_id` NOT IN(SELECT attribute_id"
88+
$query = "SELECT COUNT(*) FROM $entityValueTable WHERE `attribute_id` NOT IN(SELECT attribute_id"
8989
. " FROM `$eavTable` WHERE entity_type_id = " . $entityType->getEntityTypeId() . " AND backend_type = '$type')";
90-
$results = $db->fetchAll($query);
91-
$output->writeln("Clean up " . count($results) . " rows in $entityValueTable");
90+
$count = (int) $db->fetchOne($query);
91+
$output->writeln("Clean up $count rows in $entityValueTable");
9292

93-
if (!$isDryRun && count($results) > 0) {
93+
if (!$isDryRun && $count > 0) {
9494
$db->query("DELETE FROM $entityValueTable WHERE `attribute_id` NOT IN(SELECT attribute_id"
9595
. " FROM `$eavTable` WHERE entity_type_id = " . $entityType->getEntityTypeId() . " AND backend_type = '$type')");
9696
}

Console/Command/RestoreUseDefaultConfigValueCommand.php

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Magento\Framework\App\Config\Initial\Reader;
66
use Magento\Framework\App\ResourceConnection;
7+
use Magento\Framework\Model\ResourceModel\IteratorFactory;
78
use Symfony\Component\Console\Command\Command;
89
use Symfony\Component\Console\Input\InputInterface;
910
use Symfony\Component\Console\Output\OutputInterface;
@@ -14,6 +15,9 @@ class RestoreUseDefaultConfigValueCommand extends Command
1415
/** @var Reader */
1516
private $configReader;
1617

18+
/** @var IteratorFactory */
19+
private $iteratorFactory;
20+
1721
/**
1822
* @var ResourceConnection
1923
*/
@@ -24,12 +28,14 @@ class RestoreUseDefaultConfigValueCommand extends Command
2428

2529
public function __construct(
2630
Reader $configReader,
31+
IteratorFactory $iteratorFactory,
2732
ResourceConnection $resourceConnection,
2833
string $name = null
2934
) {
3035
parent::__construct($name);
3136

3237
$this->configReader = $configReader;
38+
$this->iteratorFactory = $iteratorFactory;
3339
$this->resourceConnection = $resourceConnection;
3440
}
3541

@@ -65,21 +71,25 @@ public function execute(InputInterface $input, OutputInterface $output): int
6571

6672
$removedConfigValues = 0;
6773

68-
$db = $this->resourceConnection->getConnection();
74+
$dbRead = $this->resourceConnection->getConnection('core_read');
75+
$dbWrite = $this->resourceConnection->getConnection('core_write');
6976
$tableName = $this->resourceConnection->getTableName('core_config_data');
70-
$configData = $db->fetchAll('SELECT DISTINCT path, value FROM ' . $tableName
71-
. ' WHERE scope_id = 0');
7277

73-
foreach ($configData as $config) {
74-
$count = $db->fetchOne('SELECT COUNT(*) FROM ' . $tableName
78+
$query = $dbRead->query("SELECT DISTINCT path, value FROM $tableName WHERE scope_id = 0");
79+
80+
$iterator = $this->iteratorFactory->create();
81+
$iterator->walk($query, [function (array $result) use ($dbRead, $dbWrite, $isDryRun, $output, &$removedConfigValues, $tableName): void {
82+
$config = $result['row'];
83+
84+
$count = (int) $dbRead->fetchOne('SELECT COUNT(*) FROM ' . $tableName
7585
. ' WHERE path = ? AND BINARY value = ?', [$config['path'], $config['value']]);
7686

7787
if ($count > 1) {
7888
$output->writeln('Config path ' . $config['path'] . ' with value ' . $config['value'] . ' has ' . $count
7989
. ' values; deleting non-default values');
8090

8191
if (!$isDryRun) {
82-
$db->query(
92+
$dbWrite->query(
8393
'DELETE FROM ' . $tableName . ' WHERE path = ? AND BINARY value = ? AND scope_id != ?',
8494
[$config['path'], $config['value'], 0]
8595
);
@@ -93,12 +103,12 @@ public function execute(InputInterface $input, OutputInterface $output): int
93103

94104
if (!$isDryRun) {
95105
if ($config['value'] === null) {
96-
$db->query(
106+
$dbWrite->query(
97107
"DELETE FROM $tableName WHERE path = ? AND value IS NULL AND scope_id = 0",
98108
[$config['path']]
99109
);
100110
} else {
101-
$db->query(
111+
$dbWrite->query(
102112
"DELETE FROM $tableName WHERE path = ? AND BINARY value = ? AND scope_id = 0",
103113
[$config['path'], $config['value']]
104114
);
@@ -107,7 +117,7 @@ public function execute(InputInterface $input, OutputInterface $output): int
107117

108118
$removedConfigValues++;
109119
}
110-
}
120+
}]);
111121

112122
$output->writeln('Removed ' . $removedConfigValues . ' values from core_config_data table.');
113123

Console/Command/RestoreUseDefaultValueCommand.php

Lines changed: 43 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Magento\Framework\App\ProductMetadataInterface;
66
use Magento\Framework\App\ResourceConnection;
7+
use Magento\Framework\Model\ResourceModel\IteratorFactory;
78
use Symfony\Component\Console\Command\Command;
89
use Symfony\Component\Console\Input\InputInterface;
910
use Symfony\Component\Console\Input\InputOption;
@@ -12,6 +13,9 @@
1213

1314
class RestoreUseDefaultValueCommand extends Command
1415
{
16+
/** @var IteratorFactory */
17+
protected $iteratorFactory;
18+
1519
/**
1620
* @var ProductMetaDataInterface
1721
*/
@@ -23,11 +27,14 @@ class RestoreUseDefaultValueCommand extends Command
2327
private $resourceConnection;
2428

2529
public function __construct(
30+
IteratorFactory $iteratorFactory,
2631
ProductMetaDataInterface $productMetaData,
2732
ResourceConnection $resourceConnection,
2833
string $name = null
2934
) {
3035
parent::__construct($name);
36+
37+
$this->iteratorFactory = $iteratorFactory;
3138
$this->productMetaData = $productMetaData;
3239
$this->resourceConnection = $resourceConnection;
3340
}
@@ -58,7 +65,7 @@ public function execute(InputInterface $input, OutputInterface $output): int
5865
if (!in_array($entity, ['product', 'category'])) {
5966
$output->writeln('Please specify the entity with --entity. Possible options are product or category');
6067

61-
return 1;
68+
return 1; // error.
6269
}
6370

6471
if (!$isDryRun && !$isForce) {
@@ -76,57 +83,64 @@ public function execute(InputInterface $input, OutputInterface $output): int
7683
}
7784
}
7885

79-
$db = $this->resourceConnection->getConnection();
86+
$dbRead = $this->resourceConnection->getConnection('core_read');
87+
$dbWrite = $this->resourceConnection->getConnection('core_write');
8088
$counts = [];
8189
$tables = ['varchar', 'int', 'decimal', 'text', 'datetime'];
8290
$column = $this->productMetaData->getEdition() === 'Enterprise' ? 'row_id' : 'entity_id';
8391

8492
foreach ($tables as $table) {
8593
// Select all non-global values
8694
$fullTableName = $this->resourceConnection->getTableName('catalog_' . $entity . '_entity_' . $table);
87-
$rows = $db->fetchAll('SELECT * FROM ' . $fullTableName . ' WHERE store_id != 0');
8895

89-
foreach ($rows as $row) {
96+
// NULL values are handled separately
97+
$query = $dbRead->query("SELECT * FROM $fullTableName WHERE store_id != 0 AND value IS NOT NULL");
98+
99+
$iterator = $this->iteratorFactory->create();
100+
$iterator->walk($query, [function (array $result) use ($column, &$counts, $dbRead, $dbWrite, $fullTableName, $isDryRun, $output): void {
101+
$row = $result['row'];
102+
90103
// Select the global value if it's the same as the non-global value
91-
$results = $db->fetchAll(
104+
$query = $dbRead->query(
92105
'SELECT * FROM ' . $fullTableName
93106
. ' WHERE attribute_id = ? AND store_id = ? AND ' . $column . ' = ? AND BINARY value = ?',
94107
[$row['attribute_id'], 0, $row[$column], $row['value']]
95108
);
96109

97-
if (count($results) > 0) {
98-
foreach ($results as $result) {
99-
if (!$isDryRun) {
100-
// Remove the non-global value
101-
$db->query(
102-
'DELETE FROM ' . $fullTableName . ' WHERE value_id = ?',
103-
$row['value_id']
104-
);
105-
}
106-
107-
$output->writeln(
108-
'Deleting value ' . $row['value_id'] . ' "' . $row['value'] . '" in favor of '
109-
. $result['value_id']
110-
. ' for attribute ' . $row['attribute_id'] . ' in table ' . $fullTableName
110+
$iterator = $this->iteratorFactory->create();
111+
$iterator->walk($query, [function (array $result) use (&$counts, $dbWrite, $fullTableName, $isDryRun, $output, $row): void {
112+
$result = $result['row'];
113+
114+
if (!$isDryRun) {
115+
// Remove the non-global value
116+
$dbWrite->query(
117+
'DELETE FROM ' . $fullTableName . ' WHERE value_id = ?',
118+
$row['value_id']
111119
);
120+
}
112121

113-
if (!isset($counts[$row['attribute_id']])) {
114-
$counts[$row['attribute_id']] = 0;
115-
}
122+
$output->writeln(
123+
'Deleting value ' . $row['value_id'] . ' "' . $row['value'] . '" in favor of '
124+
. $result['value_id']
125+
. ' for attribute ' . $row['attribute_id'] . ' in table ' . $fullTableName
126+
);
116127

117-
$counts[$row['attribute_id']]++;
128+
if (!isset($counts[$row['attribute_id']])) {
129+
$counts[$row['attribute_id']] = 0;
118130
}
119-
}
120-
}
121131

122-
$nullValues = $db->fetchOne(
132+
$counts[$row['attribute_id']]++;
133+
}]);
134+
}]);
135+
136+
$nullCount = (int) $dbRead->fetchOne(
123137
'SELECT COUNT(*) FROM ' . $fullTableName . ' WHERE store_id != 0 AND value IS NULL'
124138
);
125139

126-
if (!$isDryRun && $nullValues > 0) {
127-
$output->writeln("Deleting " . $nullValues . " NULL value(s) from " . $fullTableName);
140+
if (!$isDryRun && $nullCount > 0) {
141+
$output->writeln("Deleting $nullCount NULL value(s) from $fullTableName");
128142
// Remove all non-global null values
129-
$db->query(
143+
$dbWrite->query(
130144
'DELETE FROM ' . $fullTableName . ' WHERE store_id != 0 AND value IS NULL'
131145
);
132146
}

0 commit comments

Comments
 (0)