YII queries not working [closed]

三世轮回 提交于 2019-12-31 05:39:06

问题


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")

then it worked.

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


回答1:


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.

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
}

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.

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.



来源:https://stackoverflow.com/questions/18675874/yii-queries-not-working

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!