简体   繁体   English

一对多关系船上的学说表现

[英]Doctrine Performance On OneToMany RelationShip

I am wondering whether OneToMany Realationship will hamper on application's performance. 我想知道OneToMany Realationship是否会妨碍应用程序的性能。 Say, city and user entity are in a OneToMany relationship, that means, a city can contain a lots of users. 假设城市和用户实体处于OneToMany关系中,这意味着一个城市可以包含很多用户。 Now after setting the relationship in entity classes, whenever I retrieve a city, I can get its users via: 现在在实体类中设置关系之后,每当我检索城市时,都可以通过以下方式获取其用户:

$users = $city->getUsers();

Now, I am wondering about the internal architechture. 现在,我想知道内部架构。 Whenever I retrieve city, will it query for all users also? 每当我检索城市时,它还会查询所有用户吗? If so, lets say a city can have 10000 users. 如果是这样,可以说一个城市可以有10000个用户。 Now won't it be a performance issue that I am retrieving a city only, but its getting along with all 10000 users as well? 现在,仅检索一个城市会不会是一个性能问题,但是它也能与所有10000个用户相处? Or, it uses some other machanism and I am totally ok with it to implement? 或者,它使用其他机制,我完全可以实现它?

Wish to get an explanation from you experts and suggestions about best practices regarding this situations as well. 希望从您的专家那里得到解释,并就有关这种情况的最佳实践提出建议。 Thanks in advance. 提前致谢。

Well, I am not an expert, but I can share you some good practices that you could consider at application development: 好吧,我不是专家,但是我可以向您分享一些在应用程序开发时可以考虑的良好实践:

fetch="EXTRA_LAZY" fetch =“ EXTRA_LAZY”

By default, doctrine 2.0 will load the entire collection and store it memory. 默认情况下,教义2.0将加载整个集合并将其存储在内存中。 In a scenario like yours, the users collection could be a performance problems by the table dimesion. 在像您这样的场景中,由于表维度不足,用户集合可能会导致性能问题。 So, why not mark the relationship as EXTRA_LAZY? 那么,为什么不将关系标记为EXTRA_LAZY? An entity with this fetch mode only will be load when is accessed without triggering a full load of the collection. 具有这种获取模式的实体仅在访问时加载而不触发集合的完整加载。

/**
 * @ManyToMany(targetEntity="User", mappedBy="cities", fetch="EXTRA_LAZY")
 */

Allowing this fetch mode, you are able to make use of function like slide() and count() robustly. 允许这种访存模式,您可以稳健地利用诸如slide()count()类的功能。 For example: 例如:

$users = $em->getRepository('models\User')->findAll();
echo  $users->count();

The code triggers a sql statement like select count(*) from users.. instead looping through the $user collection. 该代码触发了一条select count(*) from users..的sql语句,如select count(*) from users..而是循环遍历$ user集合。

Hydrating objects 保湿物品

Not always is necessary to load a collection of entities. 加载实体集合并非总是必要的。 If you are creating a blog system, we need to provide only a list of post titles. 如果您要创建博客系统,我们仅需要提供帖子标题列表。 This infrasrtucture can be inproved by Hydrating objects for read-only purposes. 可以通过水化对象(用于只读)来改善此基础结构。

Useful links 有用的链接

Some links that routes you to official doctrine docs guide. 一些链接将您带到官方学说文档指南。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM