AR in Yii is very efficient but it’s slower than using DAO approach. One of Russian community members profiled both his DAO implementation and AR implementation and found that bottlenech here is not SQL generated but record population at CActiveRecord::populateRecord(). Noticeable from about 30 records.
public function populateRecord($attributes,$callAfterFind=true)
{
if($attributes!==false)
{
$record=$this->instantiate($attributes);
$record->setScenario('update');
$md=$record->getMetaData();
foreach($attributes as $name=>$value)
{
if(property_exists($record,$name)) // this check is SLOW
$record->$name=$value;
else if(isset($md->columns[$name]))
$record->_attributes[$name]=$value;
}
$record->_pk=$record->getPrimaryKey();
$record->attachBehaviors($record->behaviors());
if($callAfterFind)
$record->afterFind();
return $record;
}
else
return null;
}
1000 records, populateRecord just without property_exists(): ~525ms.
1000 records, populateRecord without filling $record->_attributes, metadata, behaviours, etc. Only filling properties without property_exists(): ~250ms.