php - CakePHP Model::create and Model::save() performance in loop -
i @ cakephp 2.3.4 , php 5.5 mysql running on ubuntu server. have loop this:
foreach($notification_group['user'] $notification_user) { // $this->set($notification); $this->notification->create(); $this->notification->save($notification); // 0.4 seconds per iteration } // same $this->set($notification); $this->notification->create(); $this->notification->save($notification); // less 0.1 seconds
in loop, each iteration take 0.4 seconds complete. create , save method outside loop take (way) less 0.1 seconds finish. loop overhead should not problem.
why takes longer basic insert in loop? how improve it? thanks!
update 1: have tried comment out create , save methods in loop , turns out loop doesn't have overhead @ all. know why performance hit happening before try savemany() or other methods.
update 2: have tried savemany() save array of notification in single shot. here code snippet:
$notification_array = array(); // save notificaions foreach($notification_group['user'] $notification_user) { $notification = null; // $this->set($notification); array_push($notification_array, $notification); // $this->notification->create(); // $this->notification->save($notification); } if (!empty($notification_array)) { $this->notification->savemany($notification_array); }
the performance same using create , save in loop - 0.4 seconds per iteration/record save. if comment out savemany(), performance hit gone.
update 3: not @ notificationcontroller. load notification model @ beginning of function:
$this->loadmodel('notification');
and loop first 1 uses notification model. lazy-loading mechanism responsible performance hit? how avoid it?
Comments
Post a Comment