Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Out of memory using Model::find()->one() #303

Open
spiritdead opened this issue Mar 15, 2020 · 1 comment
Open

Out of memory using Model::find()->one() #303

spiritdead opened this issue Mar 15, 2020 · 1 comment
Labels
type:enhancement Enhancement

Comments

@spiritdead
Copy link

spiritdead commented Mar 15, 2020

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 method

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

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);
    }
Q A
Yii version
Yii MongoDB version
MongoDB server version
PHP version
Operating system
@samdark
Copy link
Member

samdark commented Mar 15, 2020

Related to #290

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type:enhancement Enhancement
Projects
None yet
Development

No branches or pull requests

2 participants