Skip to content

Query::one uses fetch instead of limit - big memory usage #4348

@SonicGD

Description

@SonicGD
Model::find()->one();

generate

SELECT * FROM ModelTable;

and mysql return all records from table. Then yii use fetch to return first row of result. But all result already in memory.

In our project we have table with 77 million records. Such query use 20.83 gb of memory. But if we add limit(1) it use only 3.5mb.

I think one() should set limit(1) something like this:

public function one($db = null)
    {
        return $this->limit(1)->createCommand($db)->queryOne();
    }

I can submit PR.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions