我有一个巨大的产品表(100k+ 行),在我的控制器中我有以下功能:
public function indexAction(Request $request)
{
$findProducts = $this->getDoctrine()
->getRepository("StockBundle:Product")->findAll();
$paginator = $this->get('knp_paginator');
$producten = $paginator->paginate(
$findProducts,
$request->query->getInt('page', 1)/*page number*/,
20/*limit per page*/
);
return $this->render('StockBundle:Default:index.html.twig',
array('producten' => $producten));
}
问题是页面加载大约需要 11-12 秒并消耗 233MB 的 RAM。
我可以做些什么来提高速度和减少内存?
这是我的实体:
/**
* Product
*
* @ORM\Table()
* @ORM\Entity(repositoryClass="Namespace\StockBundle\Entity\ProductRepository")
*/
class Product
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="naam_nl", type="string", length=255)
*/
private $naamNl;
/**
* @var string
*
* @ORM\Column(name="naam_fr", type="string", length=255)
*/
private $naamFr;
/**
* @var string
*
* @ORM\Column(name="naam_en", type="string", length=255)
*/
private $naamEn;
/**
* @var string
*
* @ORM\Column(name="productnummer", type="string", length=255)
*/
private $productnummer;
/**
* @var float
*
* @ORM\Column(name="prijs", type="float")
*/
private $prijs;
/**
* @var string
*
* @ORM\Column(name="merk", type="string", length=255)
*/
private $merk;
/**
* @ORM\OneToOne(targetEntity="Namespace\StockBundle\Entity\ProductInventory", cascade={"persist"})
* @ORM\JoinColumn(name="productinventory_id", referencedColumnName="id")
*
*/
private $productinventory;
表结构由教义创建,如下所示:
CREATE TABLE `product` (
`id` int(11) NOT NULL,
`naam_nl` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`productnummer` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`prijs` double NOT NULL,
`merk` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`productinventory_id` int(11) DEFAULT NULL,
`naam_fr` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`naam_en` varchar(255) COLLATE utf8_unicode_ci NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;