Sure Doctrine is awesome, but sometimes (every time?), it is also a memory killer. Hopefully there are some handy tricks that can be used on several occasions. Here are some Doctrine 1.2 performance tips & tricks I’ve collected over the time working with Symfony 1.4. Hope they’re useful to you guys!
If you know more performance tips, please leave them in the comments.
Tip #1
Create a task environment to run tasks and disable the profiler on databases.yml:
1 2 3 4 | task: doctrine: param: profiler: false |
Tip #2
Enable Doctrine’s auto free query objects
1 | Doctrine_Manager::connection()->setAttribute(Doctrine_Core::ATTR_AUTO_FREE_QUERY_OBJECTS, true); |
Tip #3
Free Doctrine objects when they are no longer needed
1 2 | $doctrine_object->free(true); $doctrine_object = null; |
Tip #4
Force Doctrine connection clear after performing needed Doctrine operations
1 2 3 4 5 6 7 | Doctrine_Manager::connection()->connect(); // code code ... $doctrine_object = Doctrine_Query::create()->from('Modal a')->limit(1)->fetchOne(); // code code... Doctrine_Manager::connection()->clear(); |
Tip #5
Hydrate array when Model objects are not needed.
1 | $doctrine_object = Doctrine_Query::create()->from('Modal a')->limit(1)->execute(array(), Doctrine_Core::HYDRATE_ARRAY); |
Tip #6
Enable APC caching on Doctrine queries.
1 2 | Doctrine_Manager::connection()->setAttribute(Doctrine::ATTR_QUERY_CACHE, new Doctrine_Cache_Apc(array('prefix' => 'fancyapcprefix_'))); Doctrine_Manager::connection()->setAttribute(Doctrine_Core::ATTR_RESULT_CACHE, new Doctrine_Cache_Apc(array('prefix' => 'fancyapcprefix_'))); |
Tip #7
Perform bulk inserts instead of one-by-one inserts.
1 2 3 4 5 6 7 8 9 10 11 | $record1 = new Model() $record1->setName('Example #1'); $record2 = new Model() $record2->setName('Example #2'); $col = new Doctrine_Collection('Model'); $col->add($record1); $col->add($record2); $col->add($record3); $col->add($recordN); $col->save(); |
You can read more on Doctrine 1.2 performance here, here, here and here.