1

我们正在尝试优化一个消耗大量内存资源的项目。我们所有的查询都是使用这种sintaxes完成的:

$qb->select(array('e'))
   ->from('MyBundle:Event', 'e');

这在选择表的每个字段的查询中进行转换,如下所示:

SELECT t0.id AS id1,
   t0.field1 AS field12,
   t0.field2 AS field23,
   t0.field3 AS field34,
   t0.field4 AS field45,
FROM event t0

使用部分对象语法仅对某些预定义字段进行水合对性能来说是一个好主意吗?我真的不知道它是否会影响性能并且我会有很多缺点,因为其他字段将为空。你用 Doctrine 在你的选择查询中做什么?

问候。

4

1 回答 1

2

My two cents

I suppose that hydration (Object Hydration and lazy loading, of course) is good until you don't know how many and what fields to pull from DB tables and put into objects. If you know that you have to retrieve all fields, is better to get them once and work with them, instead of do every time a query that is time-consuming.

However, as a good practice, when I have retrieved and used my objects I unset them explicitly (not if they are last instructions of my function that will return and implicitly unset them)

Update

$my_obj_repo = $this->getDoctrine()->getManager()->getRepository('MyBundleName:my_obj');
$my_obj = $my_obj_repo->fooHydrateFunction(12); //here I don't pull out from db all data
//do stuff with this object, like extracting data or manipulating data
if($my_obj->getBarField() == null) //this is the only field I've load with fooHydrateFunction, so no lazy loading
{
  $my_obj->setBarField($request->query->get('bar');
  $entity_manager->persist($my_obj);
  $entity_manager->flush();
}
//here my object isn't necessary anymore
unset($my_obj); //this is a PHP instruction
//continue with my business logic 
于 2013-05-30T07:20:34.447 回答