本文介绍了使用Yii ActiveRecord在大型表中进行遍历-“内存不足"错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个有关Yii Framework的网站,我想搜索一个表以查找匹配的单词.

I have a website on Yii Framework and I want to search a table for matching words.

我一直得到"out of memory"(这是一张大桌子).

I keep getting "out of memory" (it is a large table).

我尝试使用此代码,但是它会继续加载页面

I try this code but it keeps loading the page

$dataProvider = new CActiveDataProvider('Data');
$iterator = new CDataProviderIterator($dataProvider);
foreach($iterator as $data) {
    echo $data->name."\n";
}

因此,我尝试使用此代码,但始终将结果限制为10:

So I try this code but it keeps limiting the result to 10:

$dataProvider = new CActiveDataProvider('Data');
$iterator = new CDataProviderIterator($dataProvider);
foreach($dataProvider as $data) {
    echo $data->name."\n";
}

如果执行此操作,则会收到内存不足"消息:

and if I do this I get the "out of memory" message:

$dataProvider = new CActiveDataProvider('Data' array(
    'criteria'=>array(
        'order'=>'id DESC',
    ),
    'pagination' => false
));

foreach($dataProvider as $data) {
    echo $data->name."\n";
}

推荐答案

对大型数据集使用CActiveDataProvider是不明智的.特别是如果您只想对它们执行后台任务.建议使用直接SQL并从那里开始.

It is unwise to use CActiveDataProvider for big datasets. Especially if you only want to perform background tasks on them.It would be advised to use direct SQL and go from there.

基于对CreatoR答案的评论,您正在尝试在一个大表中查找大量的事件.例如:

Based on the comments on CreatoR's answer, you are trying to find a number of occurences in a big table. As an example:

$connection=Yii::app()->db;
$sql = "SELECT id FROM data WHERE field1 LIKE '%someValue%' OR field2 LIKE '%someValue%' OR field3 LIKE '%someValue%'";
$command=$connection->createCommand($sql);
$numberOfRestuls=$command->execute();
//if you also want to display the results :
$ids=$command->queryAll();
$criteria=new CDbCriteria;
$criteria->addInCondition('id',$ids,'OR');
$dataProvider = new CActiveDataProvider('Data', $criteria);
//etc

这篇关于使用Yii ActiveRecord在大型表中进行遍历-“内存不足"错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 01:30