Reputation: 120
I'm using Yii2 and my database server is MySQL. I need to scan every row of a whole db table searching for occurrences of some text.
This is what I want but because of the large number of records, I'm not sure if I do it like this the server won't run out of memory or the MySQL server wont go away:
$rows = Posts::find()->select('content')->all();
foreach($rows as $post) {
// do some regex on $post['content']. no need to save it back to the table.
}
It's a live server with a large database. I must do it on the fly, can't take down the server for back up and restore!
Would this work? Is there any better way to do this?
Upvotes: 0
Views: 3829
Reputation: 527
The following two subsections of Accessing Data in Yii2 Guide have talked about your issue:
Retrieving Data in Arrays
use yii\helpers\ArrayHelper;
$contents = ArrayHelper::getColumn(
Post::find()->asArray()->all(),
'content'
);
foreach ($contents as $content) {
}
Retrieving Data in Batches
// fetch 10 rows at a time
foreach(Posts::find()->select('content')->each(10) as $post) {
// ...
}
Both methods could reduce memory cost.
Upvotes: 7