Million ManyToMany条目

编程入门 行业动态 更新时间:2024-10-27 23:20:59
Million ManyToMany条目 - 内存问题(Million ManyToMany Entries - Memory Problems)

我开发了一种“统计 - 网络”。

例如,我有一些博客条目,每个访问者都有一个额外的统计条目。

示例博客实体:

/** * @ORM\ManyToMany(targetEntity="Statistic", inversedBy="blogid") * @ORM\JoinTable(name="blog_statistics") */ private $statistics;

示例统计实体:

/** * @ORM\ManyToMany(targetEntity="Blog", mappedBy="statistics") */ private $blog;

在统计实体中,我有更多的字段,如“时间,用户,IP”。 在博客实体中,我有“文本,标题,时间”等字段。

一开始我有1个入口。 一切都很好/好。

一周后,我有2个博客条目的5.000个条目(DB)。 (每篇博客2.500)我得到了php内存问题。

我认为doctrine试图将所有2.500个条目加载到RAM / Cache中。 但我只需要最后一个用于“最后访问”的信息。 如果我需要,我可以获得其余的条目。 (统计概览)

什么是最好的“限制”条目? 当前通话:“Repository-> fetchAll”

i develope a type of "Statistics-Web".

For example i have some blog entries and for each visitor a extra statistic entry.

Example blog entity:

/** * @ORM\ManyToMany(targetEntity="Statistic", inversedBy="blogid") * @ORM\JoinTable(name="blog_statistics") */ private $statistics;

Example statistic entity:

/** * @ORM\ManyToMany(targetEntity="Blog", mappedBy="statistics") */ private $blog;

In the entity for statistic i have more fields like "time, user, ip". In the Blog entity i have fields like "text, title, time".

At the beginning i had 1 Entry. Everything is working / good.

A week later, i had 5.000 Entries (DB) for 2 Blog Entries. (2.500 per blog entry) I get php memory problems.

I think doctrine tries to load all 2.500 Entries into the RAM/Cache. But i only need the last one for the "last visited" information. I could get the rest of the entries if i needed them. (statistics overview)

Whats the best to "limit" entries? Current call: "Repository->fetchAll"

最满意答案

解决方案非常明显:

$lastStatisticsRecord = $repository->createQueryBuilder('s') ->orderBy('s.time', 'DESC') ->setMaxResults(1) ->getQuery() ->execute();

此查询将仅从表中选择最后一个统计实体。 如果您需要获取包含最后一个统计条目的博客条目,只需创建一个JOIN语句:

$lastStatisticsRecord = $repository->createQueryBuilder('s') ->select(array('s', 'b')) ->leftJoin('s.blogid', 'b') ->orderBy('s.time', 'DESC') ->setMaxResults(1) ->getQuery() ->execute();

Problem solved...

Only use "fetch" option in many-to-many relation:

@ORM\ManyToMany(targetEntity="Statistic", inversedBy="blogid", fetch="EXTRA_LAZY")

Then you could use this function to get latest statistic entry:

public function getLatestStatistic() { $cur = array(); if(count($this->getStatistics()) > 0) { $cur = $this->getStatistics()->slice(count($this->getStatistics()) - 1, 1); } return count($cur) > 0 ? $cur[0] : null; }

更多推荐

本文发布于:2023-08-06 11:29:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1450067.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:条目   Million   ManyToMany

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!