Symfony中Entity设置OneToMany后保存数据的性能问题

AppleBBS 2017-07-21 01:57:09
有2个Entity,分别如下:

class Catogry {
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*
* @Serializer\Expose
*/
protected $id;

/**
* @ORM\OneToMany(targetEntity="Product", mappedBy="category")
*
* @Serializer\Exclude
*/
protected $products;

public function setProducts(ArrayCollection $products)
{
$this->products= $products;

return $this;
}

/**
* Get products.
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getProducts()
{
return $this->products;
}

/**
* Add product.
*
* @param Entity\Product $product
*
*/
public function addProduct(\Entity\Produc t$product)
{
if (!$this->products->contains($product)) {
$this->products[] = $product;
}

return $this;
}

/**
* Remove product.
*
* @param \Entity\Product $product
*/
public function removeProduct(\Entity\Product $product)
{
if ($this->products->contains($product)) {
$this->products->removeElement($product);
}
}
}

class Product {
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*
* @Serializer\Expose
*/
protected $id;

/**
* @var string
*
* @ORM\Column(name="title", type="string")
*
* @Serializer\Expose
*
* @Constraints\NotBlank()
*/
private $title;

/**
* @ORM\ManyToOne(targetEntity="Catogry", inversedBy="products")
* @ORM\JoinColumn(name="category_id", referencedColumnName="id", onDelete="SET NULL")
* @Serializer\Exclude
*/
private $category;

/**
* Get id.
*
* @return int
*/
public function getId()
{
return $this->id;
}

/**
* Get title.
*
* @return string
*/
public function getTitle()
{
return $this->title;
}

/**
* Set title.
*
* @param string $title
*
* @return mixed
*/
public function setTitle($title)
{
$this->title = $title;

return $this;
}

public function setCategory($category)
{
$this->category= $category;

return $this;
}

public function getCategory()
{
return $this->category;
}
}

现在用下面语句添加product数据时,会发生性能问题:

$product = new Product();
$product->setTitle('test');
$category->addProduct($product);
$product->setCategory($category);
$manager = $this->getDoctrine()->getManager();
$manager->persist($category);
$manager->persist($product);
$manager->flush();

查看生成的sql语句有类似下面的语句:

SELECT
t0.id AS id1,
t0.category_id AS category_id2,
t0.title AS title3,
FROM
product t0
WHERE
t0.category_id = 1

消耗时间比较长,因为相关的数据有1w多条。

怎么处理这个问题呢?
...全文
1173 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
qq_39840950 2018-09-12
  • 打赏
  • 举报
回复
$manager->flush($category); 跟进去之后你会发现,他是在验证实体,这样子就可以去掉查询
lanshs 2017-12-23
  • 打赏
  • 举报
回复
列出所有生成的sql吧,这一段sql只是查询,只看这个看不出原因。
AppleBBS 2017-10-27
  • 打赏
  • 举报
回复
无答案?只好结贴了
AppleBBS 2017-08-18
  • 打赏
  • 举报
回复
引用 5楼_锦衣卫 的回复:
表有建立索引了吗?一般来说太多数据都是需要建立索引来快速定位!
建了索引了。怎么避免插入和更新数据库时不调用实体定义的查询关联数据呢,这是个问题
AppleBBS 2017-08-18
  • 打赏
  • 举报
回复
引用 4楼一起混吧 的回复:
[quote=引用 2 楼 AppleBBS 的回复:] [quote=引用 1 楼 jordan102 的回复:] sql 执行过长? 在category_id 上建立索引试试看
反正这个symfony框架,有关联的实体之间,在进行数据库相关查询操作时,框架总是多做一些无用的查询语句,特别是在循环列表数据时,每条数据有可能多出一条额外的关联查询[/quote]框架都是环环相扣的,既然是有关联的,你就不能说它是无用的查询。[/quote]如果是不用框架,手工写sql就可以不用循环里嵌套查询了
_锦衣卫 2017-08-08
  • 打赏
  • 举报
回复
表有建立索引了吗?一般来说太多数据都是需要建立索引来快速定位!
一起混吧 2017-07-21
  • 打赏
  • 举报
回复
引用 2 楼 AppleBBS 的回复:
[quote=引用 1 楼 jordan102 的回复:] sql 执行过长? 在category_id 上建立索引试试看
反正这个symfony框架,有关联的实体之间,在进行数据库相关查询操作时,框架总是多做一些无用的查询语句,特别是在循环列表数据时,每条数据有可能多出一条额外的关联查询[/quote]框架都是环环相扣的,既然是有关联的,你就不能说它是无用的查询。
AppleBBS 2017-07-21
  • 打赏
  • 举报
回复
引用 1 楼 jordan102 的回复:
sql 执行过长? 在category_id 上建立索引试试看
有索引的,product表还有不少其它字段,没有一一列出。
AppleBBS 2017-07-21
  • 打赏
  • 举报
回复
引用 1 楼 jordan102 的回复:
sql 执行过长? 在category_id 上建立索引试试看
反正这个symfony框架,有关联的实体之间,在进行数据库相关查询操作时,框架总是多做一些无用的查询语句,特别是在循环列表数据时,每条数据有可能多出一条额外的关联查询
一起混吧 2017-07-21
  • 打赏
  • 举报
回复
sql 执行过长? 在category_id 上建立索引试试看

4,251

社区成员

发帖
与我相关
我的任务
社区描述
国内外优秀PHP框架讨论学习
社区管理员
  • Framework
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧