Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
BatchSizeManagement.php
Go to the documentation of this file.
1 <?php
8 
13 {
17  private $rowSizeEstimator;
18 
22  private $logger;
23 
29  public function __construct(
30  \Magento\Framework\Indexer\IndexTableRowSizeEstimatorInterface $rowSizeEstimator,
31  \Psr\Log\LoggerInterface $logger
32  ) {
33  $this->rowSizeEstimator = $rowSizeEstimator;
34  $this->logger = $logger;
35  }
36 
40  public function ensureBatchSize(\Magento\Framework\DB\Adapter\AdapterInterface $connection, $batchSize)
41  {
42  $rowMemory = $this->rowSizeEstimator->estimateRowSize();
43 
44  $maxHeapTableSize = $connection->fetchOne('SELECT @@max_heap_table_size;');
45  $tmpTableSize = $connection->fetchOne('SELECT @@tmp_table_size;');
46  $bufferPoolSize = $connection->fetchOne('SELECT @@innodb_buffer_pool_size;');
47  $maxMemoryTableSize = min($maxHeapTableSize, $tmpTableSize);
48 
49  $size = (int) ($rowMemory * $batchSize);
50 
51  // Log warning if allocated memory for temp table greater than 20% of innodb_buffer_pool_size
52  if ($size > $bufferPoolSize * .2) {
53  $message = 'Memory size allocated for the temporary table is more than 20% of innodb_buffer_pool_size. ' .
54  'Please update innodb_buffer_pool_size or decrease batch size value '.
55  '(which decreases memory usages for the temporary table). ' .
56  'Current batch size: %1; Allocated memory size: %2 bytes; InnoDB buffer pool size: %3 bytes.';
57  $this->logger->warning(new \Magento\Framework\Phrase($message, [$batchSize, $size, $bufferPoolSize]));
58  }
59 
60  if ($maxMemoryTableSize < $size) {
61  $connection->query('SET SESSION tmp_table_size = ' . $size . ';');
62  $connection->query('SET SESSION max_heap_table_size = ' . $size . ';');
63  }
64  }
65 }
$message
$logger
__construct(\Magento\Framework\Indexer\IndexTableRowSizeEstimatorInterface $rowSizeEstimator, \Psr\Log\LoggerInterface $logger)
ensureBatchSize(\Magento\Framework\DB\Adapter\AdapterInterface $connection, $batchSize)
$connection
Definition: bulk.php:13