Skip to content

Commit 3260cd6

Browse files
committed
ACP2E-2457: Large MySQL temporary table are created when executing "aggregate_sales_report_bestsellers_data" cron
- mass-inserts are now split by store dimension - adjusted corresponding unit test
1 parent 619d608 commit 3260cd6

File tree

2 files changed

+69
-58
lines changed

2 files changed

+69
-58
lines changed

app/code/Magento/Reports/Model/ResourceModel/Helper.php

Lines changed: 48 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class Helper extends \Magento\Framework\DB\Helper implements \Magento\Reports\Mo
1919
/**
2020
* @var StoreManagerInterface
2121
*/
22-
protected StoreManagerInterface $storeManager;
22+
private StoreManagerInterface $storeManager;
2323

2424
/**
2525
* @param ResourceConnection $resource
@@ -55,60 +55,60 @@ public function mergeVisitorProductIndex($mainTable, $data, $matchFields)
5555
*/
5656
public function updateReportRatingPos($connection, $type, $column, $mainTable, $aggregationTable)
5757
{
58-
$periodSubSelect = $connection->select();
59-
$ratingSubSelect = $connection->select();
60-
$ratingSelect = $connection->select();
58+
foreach ($this->storeManager->getStores(true) as $store) {
59+
$periodSubSelect = $connection->select();
60+
$ratingSubSelect = $connection->select();
61+
$ratingSelect = $connection->select();
6162

62-
switch ($type) {
63-
case 'year':
64-
$periodCol = $connection->getDateFormatSql('t.period', '%Y-01-01');
65-
break;
66-
case 'month':
67-
$periodCol = $connection->getDateFormatSql('t.period', '%Y-%m-01');
68-
break;
69-
default:
70-
$periodCol = 't.period';
71-
break;
72-
}
63+
switch ($type) {
64+
case 'year':
65+
$periodCol = $connection->getDateFormatSql('t.period', '%Y-01-01');
66+
break;
67+
case 'month':
68+
$periodCol = $connection->getDateFormatSql('t.period', '%Y-%m-01');
69+
break;
70+
default:
71+
$periodCol = 't.period';
72+
break;
73+
}
7374

74-
$columns = [
75-
'period' => 't.period',
76-
'store_id' => 't.store_id',
77-
'product_id' => 't.product_id',
78-
'product_name' => 't.product_name',
79-
'product_price' => 't.product_price',
80-
];
75+
$columns = [
76+
'period' => 't.period',
77+
'store_id' => 't.store_id',
78+
'product_id' => 't.product_id',
79+
'product_name' => 't.product_name',
80+
'product_price' => 't.product_price',
81+
];
8182

82-
if ($type == 'day') {
83-
$columns['id'] = 't.id'; // to speed-up insert on duplicate key update
84-
}
83+
if ($type == 'day') {
84+
$columns['id'] = 't.id'; // to speed-up insert on duplicate key update
85+
}
8586

86-
$cols = array_keys($columns);
87-
$cols['total_qty'] = new \Zend_Db_Expr('SUM(t.' . $column . ')');
88-
$periodSubSelect->from(
89-
['t' => $mainTable],
90-
$cols
91-
)->group(
92-
['t.store_id', $periodCol, 't.product_id']
93-
)->order(
94-
['t.store_id', $periodCol, 'total_qty DESC']
95-
);
87+
$cols = array_keys($columns);
88+
$cols['total_qty'] = new \Zend_Db_Expr('SUM(t.' . $column . ')');
89+
$periodSubSelect->from(
90+
['t' => $mainTable],
91+
$cols
92+
)->group(
93+
['t.store_id', $periodCol, 't.product_id']
94+
)->order(
95+
['t.store_id', $periodCol, 'total_qty DESC']
96+
);
9697

97-
$cols = $columns;
98-
$cols[$column] = 't.total_qty';
99-
$cols['rating_pos'] = new \Zend_Db_Expr(
100-
"(@pos := IF(t.`store_id` <> @prevStoreId OR {$periodCol} <> @prevPeriod, 1, @pos+1))"
101-
);
102-
$cols['prevStoreId'] = new \Zend_Db_Expr('(@prevStoreId := t.`store_id`)');
103-
$cols['prevPeriod'] = new \Zend_Db_Expr("(@prevPeriod := {$periodCol})");
104-
$ratingSubSelect->from($periodSubSelect, $cols);
98+
$cols = $columns;
99+
$cols[$column] = 't.total_qty';
100+
$cols['rating_pos'] = new \Zend_Db_Expr(
101+
"(@pos := IF(t.`store_id` <> @prevStoreId OR {$periodCol} <> @prevPeriod, 1, @pos+1))"
102+
);
103+
$cols['prevStoreId'] = new \Zend_Db_Expr('(@prevStoreId := t.`store_id`)');
104+
$cols['prevPeriod'] = new \Zend_Db_Expr("(@prevPeriod := {$periodCol})");
105+
$ratingSubSelect->from($periodSubSelect, $cols);
105106

106-
$cols = $columns;
107-
$cols['period'] = $periodCol;
108-
$cols[$column] = 't.' . $column;
109-
$cols['rating_pos'] = 't.rating_pos';
107+
$cols = $columns;
108+
$cols['period'] = $periodCol;
109+
$cols[$column] = 't.' . $column;
110+
$cols['rating_pos'] = 't.rating_pos';
110111

111-
foreach ($this->storeManager->getStores(true) as $store) {
112112
$ratingSubSelect->where('t.store_id = ' . $store->getId());
113113
$ratingSelect->from($ratingSubSelect, $cols);
114114
$sql = $ratingSelect->insertFromSelect($aggregationTable, array_keys($cols));

app/code/Magento/Reports/Test/Unit/Model/ResourceModel/HelperTest.php

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,13 @@
1111
use Magento\Framework\DB\Adapter\AdapterInterface;
1212
use Magento\Framework\DB\Select;
1313
use Magento\Reports\Model\ResourceModel\Helper;
14+
use Magento\Store\Api\Data\StoreInterface;
15+
use Magento\Store\Model\StoreManagerInterface;
1416
use PHPUnit\Framework\MockObject\MockObject;
1517
use PHPUnit\Framework\TestCase;
1618

1719
class HelperTest extends TestCase
1820
{
19-
/**
20-
* @var Helper
21-
*/
22-
protected $helper;
23-
2421
/**
2522
* @var ResourceConnection|MockObject
2623
*/
@@ -31,6 +28,11 @@ class HelperTest extends TestCase
3128
*/
3229
protected $connectionMock;
3330

31+
/**
32+
* @var StoreManagerInterface
33+
*/
34+
private StoreManagerInterface $storeManager;
35+
3436
/**
3537
* {@inheritDoc}
3638
*/
@@ -48,9 +50,7 @@ protected function setUp(): void
4850
->method('getConnection')
4951
->willReturn($this->connectionMock);
5052

51-
$this->helper = new Helper(
52-
$this->resourceMock
53-
);
53+
$this->storeManager = $this->createMock(StoreManagerInterface::class);
5454
}
5555

5656
/**
@@ -67,7 +67,11 @@ public function testMergeVisitorProductIndex()
6767
->method('insertOnDuplicate')
6868
->with($mainTable, $data, array_keys($data));
6969

70-
$this->helper->mergeVisitorProductIndex($mainTable, $data, $matchFields);
70+
$helper = new Helper(
71+
$this->resourceMock,
72+
$this->storeManager
73+
);
74+
$helper->mergeVisitorProductIndex($mainTable, $data, $matchFields);
7175
}
7276

7377
/**
@@ -82,6 +86,9 @@ public function testUpdateReportRatingPos($type, $result)
8286
$column = 'column';
8387
$aggregationTable = 'aggregationTable';
8488

89+
$store = $this->createMock(StoreInterface::class);
90+
$store->expects($this->once())->method('getId')->willReturn(1);
91+
$this->storeManager->expects($this->once())->method('getStores')->willReturn([$store]);
8592
$selectMock = $this->getMockBuilder(Select::class)
8693
->disableOriginalConstructor()
8794
->getMock();
@@ -108,7 +115,11 @@ public function testUpdateReportRatingPos($type, $result)
108115
->method('select')
109116
->willReturn($selectMock);
110117

111-
$this->helper->updateReportRatingPos($this->connectionMock, $type, $column, $mainTable, $aggregationTable);
118+
$helper = new Helper(
119+
$this->resourceMock,
120+
$this->storeManager
121+
);
122+
$helper->updateReportRatingPos($this->connectionMock, $type, $column, $mainTable, $aggregationTable);
112123
}
113124

114125
/**

0 commit comments

Comments
 (0)