【发布时间】:2021-10-24 14:41:52
【问题描述】:
我现在正在开发的当前网站正在运行 Symfony 3.4。
我的一个实体是Contact:
我得到一个非常大的 MySQL 表,其中有 ~ 14.000 行 的联系人资料:
| Name | Phone number | Allocation | |
|---|---|---|---|
| Brand Peter | peter.brand@aol.com | 49594885403 | Bla blabla |
| .... | ... | ... | ... |
然后我有另一个实体 Event,它的自定义形式 EventForm 包括 6 个不同的插槽,引用 Contact 实体:
<?php
namespace AppBundle\Form;
/*****
** Here all my others 'use'
*****/
use AppBundle\Entity\Contact;
use AppBundle\Repository\ContactRepository;
class EventForm
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
// A lot of ->add(), not revelant for my issue
->add('client1', EntityType::class, [
'class' => Client::class,
'query_builder' => function(ClientRepository $repo) {
return $repo->getAll();
}
])
->add('client2', EntityType::class, [
'class' => Client::class,
'query_builder' => function(ClientRepository $repo) {
return $repo->getAll();
}
])
// ->add('client3'), same as above
// ->add('client4'), same as above
// ->add('client5'), same as above
// ->add('client6'), same as above
;
}
效果很好! 但每个查询的处理时间为 2 秒,然后表单的总加载时间超过 10 秒。有没有办法优化我的表单以便只执行一次$repo->getAll() 查询?我也试过findAll(),但性能是一样的。
【问题讨论】:
-
你想做什么?添加客户?还是改变它们?您肯定应该删除查询构建部分。您正在检索所有客户 6 次!只需在 $builder 上方编写一个查询构建器,并将返回的数据用作每个客户端的变量。另外为什么你有6个?它会更多或更少吗?我认为你应该以某种方式生成这 6 个
->add东西。 -
我也认为展示你的控制器很重要(提交后会发生什么)。以及您所有的 ->add() 代码。因为你的加载时间都是基于这些东西。 10秒的加载时间实在是太大了。测试哪个部分花费的时间最多,并在那里进行特定的优化。您现在要问的内容太大且不清楚。它不够具体,无法按照您想要的方式修复加载时间。如果您有 10 秒的加载时间,6 个查询如何花费 2 秒? 6 * 2 = 12。不是 10...
标签: php symfony orm doctrine symfony-3.4