Skip to content

Commit 517f5a5

Browse files
committed
MCP-85: Optimize order creation scenario [admin area]
1 parent 26acabe commit 517f5a5

File tree

2 files changed

+59
-0
lines changed

2 files changed

+59
-0
lines changed

app/code/Magento/Catalog/Ui/DataProvider/Product/ProductCollection.php

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,58 @@ protected function _productLimitationJoinPrice()
2525
$this->_productLimitationFilters->setUsePriceIndex(false);
2626
return $this->_productLimitationPrice(true);
2727
}
28+
29+
/**
30+
* Return approximately amount if too much entities.
31+
*
32+
* @return int|mixed
33+
*/
34+
public function getSize()
35+
{
36+
$sql = $this->getSelectCountSql();
37+
$possibleCount = $this->analyzeCount($sql);
38+
39+
if ($possibleCount > 20000) {
40+
return $possibleCount;
41+
}
42+
43+
return parent::getSize();
44+
}
45+
46+
/**
47+
* Analyze amount of entities in DB.
48+
*
49+
* @param $sql
50+
* @return int|mixed
51+
* @throws \Zend_Db_Statement_Exception
52+
*/
53+
private function analyzeCount($sql)
54+
{
55+
$results = $this->getConnection()->query('EXPLAIN ' . $sql)->fetchAll();
56+
$alias = $this->getMainTableAlias();
57+
58+
foreach ($results as $result) {
59+
if ($result['table'] == $alias) {
60+
return $result['rows'];
61+
}
62+
}
63+
64+
return 0;
65+
}
66+
67+
/**
68+
* Identify main table alias or its name if alias is not defined.
69+
*
70+
* @return string
71+
* @throws \LogicException
72+
*/
73+
private function getMainTableAlias()
74+
{
75+
foreach ($this->getSelect()->getPart(\Magento\Framework\DB\Select::FROM) as $tableAlias => $tableMetadata) {
76+
if ($tableMetadata['joinType'] == 'from') {
77+
return $tableAlias;
78+
}
79+
}
80+
throw new \LogicException("Main table cannot be identified.");
81+
}
2882
}

app/code/Magento/Sales/etc/adminhtml/di.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,9 @@
4848
</argument>
4949
</arguments>
5050
</type>
51+
<type name="Magento\Sales\Block\Adminhtml\Order\Create\Search\Grid\DataProvider\ProductCollection">
52+
<arguments>
53+
<argument name="collectionFactory" xsi:type="object">\Magento\Catalog\Ui\DataProvider\Product\ProductCollectionFactory</argument>
54+
</arguments>
55+
</type>
5156
</config>

0 commit comments

Comments
 (0)