3

我正在使用knppaginator对非常大的实体集合进行分页,在对性能不满意之后,我意识到无论传递什么页码,它们都会获取所有实体,我检查了查询分析器,但我从未看到任何偏移量查询。无论如何,这就是我发现的有效方法,但我想将获得计数和实体的部分移回存储库类,我想在其中使用createQueryBuilder

public function indexAction(Request $request)
{

    $page = $request->get('p', 1);

    $this->breadcrumbs(
        array(
            array('title'=>'Admin','path'=>'home'),
            array('title'=>'Customers','path'=>'customers'),
            array('title'=>'All'),
        )
    );

    $em = $this->getDoctrine()->getManager();


    $count = $em
        ->createQuery('SELECT COUNT(cb) FROM MWCoreBundle:ContactsBasics cb LEFT JOIN cb.login l LEFT JOIN cb.setting s')
        ->getSingleScalarResult()
    ;

    $entities = $em
        ->createQuery('SELECT cb,l,s FROM MWCoreBundle:ContactsBasics cb LEFT JOIN cb.login l LEFT JOIN cb.setting s ORDER BY cb.dateAdded DESC')
        ->setHint('knp_paginator.count', $count);


    $pagination = $this->get('knp_paginator')->paginate($entities, $page, 50, array('distinct' => false));


    return $this->render('MWCoreBundle:ContactsBasics:index.html.twig', array(
        'entities' => $entities,
        'pagination' => $pagination,
    ));
}
4

1 回答 1

0

不,knp 分页器,运行三个查询:

  1. 计数项目

  2. 通过偏移量获取不同的 id

  3. 通过 id 获取项目

计数项目:

SELECT 
  count(DISTINCT u0_.id) AS sclr_0 
FROM 
  users u0

通过偏移量获取不同的 id

SELECT 
    DISTINCT u0_.id AS id_0 
FROM 
  users u0_ 
LIMIT 
  10 OFFSET 0

通过 id 获取项目:

SELECT 
    *
FROM 
  users u0_ 
WHERE 
  u0_.id IN (?)
于 2015-04-19T04:37:35.413 回答