YII 查询不起作用 [英] YII queries not working

查看:41
本文介绍了YII 查询不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个大约有 700,000 行的表.

I have a table with around 700,000 rows.

Profile::model()->findAll("country='US'")

我用它来查找所有以美国"作为其国家的行.但是执行停止了,我没有得到任何结果.但后来我添加了如下限制

I used this to find all rows that have 'US' as its country. But the execution stopped and I didn't get any result. But then I added limit like below

Profile::model()->findAll("country='US' limit 10000")

然后就成功了.

为什么执行停止?请帮助我.. 我是 YII 的新手.

why the execution stopped? Please help me.. I'm new to YII.

推荐答案

如果您使用 findAll,则将返回所有记录,而不会应用任何限制.但是,如果您不另行说明,CActiveDataProvider 会自动对结果进行分页.如评论中所述,使用 findAll 时可能会耗尽内存.使用限制或分页(自动应用限制)会将返回的行数降低到您的应用程序可以处理的范围.

If you use findAll, then all records will be returned without any limit applied. However, CActiveDataProvider automatically paginates results if you don't tell it otherwise. As mentioned in the comments, you are likely running out of memory when using findAll. Using a limit or pagination (which automatically applies a limit) lowers the returned number of rows to what your application can handle.

我建议使用 CDataProviderIterator 来获取大量活动记录.试试下面的示例代码.

I recommend using CDataProviderIterator to fetch large numbers of active records. Try the following sample code.

$criteria = new CDbCriteria;
$criteria->compare('country','US');

$Profiles = CDataProviderIterator(
   new CActiveDataProvider(
     'Profile',
      array('criteria'=>$criteria)
   ),
   10000
 );

foreach ($Profiles as $Profile) {
  //do whatever
}

使用迭代器,Yii 将自动一次只获取 10000 条记录(一个你说可以在内存不足的情况下工作的数字),然后在抓取下一页之前将它们从内存中删除.使用 foreach 循环可以方便地访问所有记录,而无需您做任何进一步的工作.

Using the iterator, Yii will automatically fetch just 10000 records (a number you say worked without running out of memory) at a time, and then remove them from memory before grabbing the next page. Conveniently all the records can be accessed with a foreach loop without any further work from you.

根据需要更改 10000 值以获得所需的结果.此外,请确保您正在监控错误日志以识别内存不足和其他错误.

Change the 10000 value as necessary to achieve desired results. Also, make sure you are monitoring your error logs to identify out of memory and other errors.

这篇关于YII 查询不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆