What does limit() do?

I’m using mongodb ext for yii2 however, i noticed it still uses yii->db->xxxx for a lot of items.

I kept running out of memory in my php. All it would say is it exhusted all of the memory with no error code. So I upped it and upped it and upped it until i could get it to work.

So the page is using 526m which is absolutely ridiculous.

I’m using

listview

mongo/ActiveRecord

mongo/Query

4 Fields (id, name, desc, date) all of them have the same data as well minus the id (id, "TEST", "TEST", "") so a very very minimal amount of data to be returned.

440,000 collections (i.e. rows for you SQL people)




	public function search($params) {

    	$query = new Query;

    	$query = Objects::find();

    	$dataProvider = new ActiveDataProvider([

        	'query' => $query,

        	'pagination' => array('pageSize' => isset($session['pageSize']) ? $session['pageSize'] : Yii::$app->params['defaultPageSize'])

    	]);

    	if (!($this->load($params) && $this->validate())) {

        	return $dataProvider;

    	}

    	$query->andFilterWhere([

        	'_id' => $this->_id

    	]);

    	$query->orWhere(['like', 'name', $this->name]);

    	return $dataProvider;

	}



I tried limiting what the query returns but i can’t seem to get it to work. i.e. limit(24)

So my question is what does limit() do?

Is it suposeto limit the number of returned results?

i.e. xxx->limit(24); should return only 24 results

If so, i can’t seem to get it to work for Mongodb queries. The limit in any of the queries i.e. activeQuery, Query, activeRecord->find() don’t work. It always returns all (440k) of my records.

Anyone know a way to reduce the memory usages / what i’m doing wrong/ ideas to try please share!!!

So you tried with limit 24 as





        public function search($params) {

        $query = new Query;

        $query = Objects::find()->limit(24);       // <--- LIMIT 24

        .....

        }



?

Yes that’s what I did. Apperently if you have pagination set in the dataprovider it will override your limit making the pager page size the limit.


public function search($params) {

    	$query = Objects::find()->orderBy('name ASC')->limit(24);

    	$dataProvider = new ActiveDataProvider([

        	'query' => $query,

        	'pagination' => false

    	]);


    	return $dataProvider;

	}

but then the pager doesn’t work because it’s set to false and has to in order for the limit to work.

i.e. the following doesn’t work


$query = Objects::find()->orderBy('name ASC')->limit(1);

    	$dataProvider = new ActiveDataProvider([

        	'query' => $query,

        	'pagination' => array('pageSize' =>24)

    	]);

if that was the case the limit would now be 24 not 1… but really it just doesn’t work and produces the 2 queries below.




find.count({"ns":"geekobjects.objects","limit":-1,"batchSize":0,"skip":-1,"flags":0,"query":{},"fields":[],"started_iterating":false})


find({"ns":"geekobjects.objects","limit":24,"batchSize":0,"skip":0,"flags":0,"query":{"$query":{},"$orderby":{"name":1}},"fields":[],"started_iterating":false})



So one overrides the other and making all of my records show in 24 items per page even though it was a limit of 1.

The queries that are being performed are…

Pager

ActiveDataProvider

Don’t know why adding one search field is creating 2 queries just by rendering the form and not searching anything.

Ok, i understand.

You could get 24 record and then paginate as you want using ArrayDataProvider

http://www.yiiframework.com/doc-2.0/yii-data-arraydataprovider.html

as this, getting 24 record and paginate at 10 at time.




$query = Objects::find()->orderBy('name ASC')->limit(24);

$provider = new ArrayDataProvider([

    'allModels' => $query->all(),

    'pagination' => [

        'pageSize' => 10,

    ],

]);



This should work.

Yes!!!

+1 for you… That worked and thank you!

Also, this got rid of the duplication of queries.

Update:

I ended up going with this and just forgot about the limit until it gets added as a feature.




$query = Objects::find()->orderBy('name ASC');

    	$dataProvider = new ActiveDataProvider([

        	'pagination' => ['pageSize' =>24],

        	'query' => $query,

    	]);



The above used 5.4m of memory (yii2 is 3.4 empty) with 300k records / documents.

The only problem with this is that it uses 2 queries. one for the pager and one for the retrieving of the records.


$query = Objects::find()->orderBy('name ASC')->limit(24);

$provider = new ArrayDataProvider([

    'allModels' => $query->all(),

    'pagination' => [

        'pageSize' => 10,

    ],

]);



the above on the same ducuments used 534m of memory which doesn’t work for anyone. The plus side is it only uses one query for the pager and retrieving of the records. Is there a way to have the low memory usage (first block of code) and only use one query like the second code block?