Large amount data with less memory

I have large amount of data,near to 12000 records.I set pagination 100 and everything is but it takes times.I read docs in yii2 but did nt get how to use batch on full query.I want it takes less time so fetch in to do this?

use yii\db\Query;

$query = (new Query())



foreach ($query->batch(100) as $users) {

    // $users is an array of 100 or fewer rows from the user table


// or if you want to iterate the row one by one

foreach ($query->each() as $user) {

    // $user represents one row of data from the user table



it will work on activedataprovider or not and if i have 1000 records,i m showing 100 by 100 using pagination ,can i load only 100 by 100. if it is possible then how search filters will filter the data.

ActiveDataProvider will automatically construct the appropriate sql using ‘OFFSET’ and ‘LIMIT’ with the help of Pagination object. So it will fetch into the memory only the records that should be displayed in the current page. I mean, just 100 out of 1000. You don’t need to worry about how large the total count of records might be.

On the other hand, ArrayDataProvider needs all the items beforehand. You have to give all the records as ‘allModels’ to it, no matter what little portion of it will be displayed in the current page. I mean, you have to give 1000 items just to display 100 items out of them. This is the main reason why you should avoid using ArrayDataProvider if you can.

Search filters are applied to the ‘WHERE’ part of the sql. It is not modified by Pagination.