From e328dc948effe36653268a245b5e40e957739cd6 Mon Sep 17 00:00:00 2001 From: Christophe Vandeplas Date: Fri, 12 Jul 2013 14:55:56 +0200 Subject: [PATCH] huge performance increase in generateCount --- app/Controller/AppController.php | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/app/Controller/AppController.php b/app/Controller/AppController.php index 33db25384..b2abd8c05 100755 --- a/app/Controller/AppController.php +++ b/app/Controller/AppController.php @@ -305,11 +305,14 @@ class AppController extends Controller { public function generateCount() { if (!self::_isSiteAdmin()) throw new NotFoundException(); - $this->loadModel('Event'); - $events = $this->Event->find('all', array('recursive' => 1)); + // do one SQL query with the counts + // loop over events, update in db + $this->loadModel('Attribute'); + $events = $this->Attribute->query('SELECT event_id, count(event_id) as attribute_count FROM attributes GROUP BY event_id'); foreach ($events as $event) { - $event['Event']['attribute_count'] = sizeof($event['Attribute']); - $this->Event->save($event); + $this->Event->read(null, $event['attributes']['event_id']); + $this->Event->set('attribute_count', $event[0]['attribute_count']); + $this->Event->save(); } $this->Session->setFlash(__('All done.')); $this->redirect(array('controller' => 'events', 'action' => 'index', 'admin' => false));