教义关系是否会影响应用程序性能? [英] Are Doctrine relations affecting application performance?

查看:62
本文介绍了教义关系是否会影响应用程序性能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在与一个新团队一起进行Symfony项目,由于性能问题,他们决定尽最大可能停止使用教义关系。

I am working on a Symfony project with a new team, and they decide to stop using Doctrine relations the most they can because of performances issues.

例如,我必须存储我的关系的id而不是使用ManyToOne关系。

For instance I have to stock the id of my "relation" instead of using a ManyToOne relation.

但是我想知道这是否是一个真正的问题?

But I am wondering if it is a real problem?

问题是,它改变了编码以检索信息的方式,等等。

The thing is, it changes the way of coding to retrieve information and so on.

推荐答案

性能问题很可能来自查询未优化的事实。

The performance issue most likely comes from the fact that queries are not optimised.

如果让Doctrine(处理查询的Symfony组件)自己执行查询(通过使用 findBy() findAll() findOneBy()等),它将首先获取您的要求,然后执行更多查询

If you let Doctrine (Symfony component that handle the queries) do the queries itself (by using findBy(), findAll(), findOneBy(), etc), it will first fetch what you asked, then do more query as it will require data from other tables.

让我们以最常见的例子为库。

Lets take the most common example, a library.

< kbd> 实体


  • Book

  • 作者

  • 货架

关系


  • 一本有一本作者,但一个 Author 可以有许多 Books Book< = ManyToOne => ;作者

  • 一本图书存储在一个书架 Book< = OneToOne => Sheilf

  • One Book have one Author, but one Author can have many Books (Book <= ManyToOne => Author)
  • One Book is stored in one Shelf (Book <= OneToOne => Sheilf)

现在,如果您查询,则Doctrine还将获取书架,因为它是 OneToOn e 关系。

但是它不会获取 Author 。在您的对象中,您只能访问 book.author.id ,因为此信息位于 Book 本身中。

Now if you query a Book, Doctrine will also fetch Shelf as it's a OneToOne relation.
But it won't fetch Author. In you object, you will only have access to book.author.id as this information is in the Book itself.

因此,如果在Twig视图中执行 {{book.author.name}} 之类的操作,由于初始查询中未获取信息,Doctrine将添加一个额外的查询以获取有关该书作者的数据。

Thus, if in your Twig view, you do something like {{ book.author.name }}, as the information wasn't fetched in the initial query, Doctrine will add an extra query to fetch data about the author of the book.

因此,为防止这种情况,您必须自定义查询,以便一次性获得所需的数据,例如:

Thus, to prevent this, you have to customize your query so it get the required data in one go, like this:

public function getBookFullData(Book $book) {
    $qb=$this->createQueryBuilder('book');

    $qb->addSelect('shelf')
       ->addSelect('author')
       ->join('book.shelf', 'shelf')
       ->join('book.author', 'author');

    return $qb->getQuery()->getResult();
}

通过此自定义查询,您可以将一本书的所有数据合而为一因此,Doctrine不必进行额外的查询。

With this custom query, you can get all the data of one book in one go, thus, Doctrine won't have to do an extra query.

因此,尽管示例很简单,但我相信您可以理解大型项目,让免费掌握Doctrine只会增加额外的查询次数。

我的一个项目在优化之前已达到每页加载1500个查询...

So, while the example is rather simple, I'm sure you can understand that in big projects, letting free rein to Doctrine will just increase the number of extra query.
One of my project, before optimisation, reached 1500 queries per page loading...

另一方面,忽略数据库中的关系不是很好。
实际上,使用外键和索引的数据库要比不使用外键和索引的数据库要快。

On the other hand, it's not good to ignore relations in a database. In fact, a database is faster with foreign keys and indexes than without.

如果您希望自己的应用程序尽可能快,则必须使用关系来优化数据库查询速度,并优化Doctrine查询以避免不必要的额外查询。

If you want your app to be as fast as possible, you have to use relations to optimise your database query speed, and optimise Doctrine queries to avoid a foul number of extra queries.

最后,我要说顺序很重要。
使用 ORDER BY 在孩子之前获取父对象也将大大减少Doctrine自己进行的查询数量。

Last, I will say that order matter. Using ORDER BY to fetch parent before child will also greatly reduce the number of query Doctrine might do on it's own.

[旁注]

您还可以更改实体注释的提取方法,以优化预制的教义

You can also change the fetch method on your entity annotation to "optimise" Doctrine pre-made queries.


  • fetch = EXTRA_LAZY

  • fetch = LAZY

  • fetch = EAGER

  • fetch="EXTRA_LAZY
  • fetch="LAZY
  • fetch="EAGER

但这并不明智,通常不能真正提供我们真正需要的东西。

因此,自定义查询是最佳选择。

But it's not smart, and often don't really provide what we really need.
Thus, custom queries is the best choice.

这篇关于教义关系是否会影响应用程序性能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆