Open spiritdead opened 4 years ago
Create a collection with 500k records try to use $model = Model::find()->one();
get the model
Fatal error Out of memory
Reading Query.php i found that when we enter in this method
Query.php
protected function fetchRowsInternal($cursor, $all) { $result = []; if ($all) { foreach ($cursor as $row) { $result[] = $row; } } else { if ($row = current($cursor->toArray())) { $result = $row; } else { $result = false; } } return $result; }
in this moment $cursor is not limited with (limit = 1) and the cursor try to fetch the full collection generating an out of memory
Proposal Solution is add limit = 1 when we try to get one
limit = 1
public function one($db = null) { if (!empty($this->emulateExecution)) { return false; } $cursor = $this->buildCursor($db); return $this->fetchRows($cursor, false); }
after the change should looks like this
public function one($db = null) { if (!empty($this->emulateExecution)) { return false; } if (!isset($this->limit)) { $this->limit = 1; } $cursor = $this->buildCursor($db); return $this->fetchRows($cursor, false); }
Related to https://github.com/yiisoft/yii2-mongodb/issues/290
What steps will reproduce the problem?
Create a collection with 500k records try to use $model = Model::find()->one();
What's expected?
get the model
What do you get instead?
Fatal error Out of memory
Additional info
Reading
Query.php
i found that when we enter in this methodin this moment $cursor is not limited with (limit = 1) and the cursor try to fetch the full collection generating an out of memory
Proposal Solution is add
limit = 1
when we try to get oneafter the change should looks like this