我的doctrine2查询中的内部联接将执行时间从400毫秒增加到5秒

时间:2015-05-15 12:52:06

标签: php sql doctrine-orm

我的doctrine2查询中的内部联接将执行时间从400毫秒增加到5秒,但如果我提取SQL并直接在SQLSERVER studio中运行,则需要几毫秒才能执行。

下面是我的查询,瓶颈只是内连接:

debuggable true

以下是我的SQL摘录,它内部加入运行也很棒:

$qb = $em->createQueryBuilder();
$q = $qb->select( 'wp.id, wp.last_activity_datetime' )
->from( '\Entities\wall_post', 'wp' )
->LeftJoin( 'wp.wall_postsLikes', 'likes' )
->LeftJoin( 'wp.wall_postsShare', 'shares' )
->LeftJoin( 'wp.wall_postsComment', 'comments' )
->leftJoin( 'wp.wall_postsFrom_user','fromUser');
if( $wall_type == self::WALL_TYPE_SOCIALISE )
{
    $q->leftJoin( 'wp.wall_postsSocialise_album','album');
    $q->innerJoin( 'album.socialise_albumsSocialise_photo','photo');
}
$q->setParameter( 'my_links_r', $my_links_r )
->setParameter( 1, $userId )

->setFirstResult( $offset )
->setMaxResults( $limit )
->groupBy( 'wp.id, wp.last_activity_datetime' )
->orderBy( 'wp.last_activity_datetime', 'DESC' );

if( $wall_type )
{
    $q->where( 'wp.wall_postsFrom_user = ?1 AND wp.wall_type = ?4' );
    $q->orWhere( 'wp.wall_postsFrom_user IN (:my_links_r) AND wp.wall_type = ?4' );
    $q->orWhere( 'likes.likesLiked_by IN (:my_links_r) AND wp.wall_type = ?4' );
    $q->orWhere( 'shares.sharesShared_by IN (:my_links_r) AND wp.wall_type = ?4' );
    $q->orWhere( 'comments.commentsIlook_user IN (:my_links_r) AND wp.wall_type = ?4' );
    $q->andWhere('fromUser.account_closed_on is null');
    if( $blocked_user_ids_arr ):
        $q->setParameter( 'blocked_users_r', $blocked_user_ids_arr );
        $q->andWhere( 'wp.wall_postsFrom_user NOT IN (:blocked_users_r)' );
    endif;

    $q->setParameter( 4, $wall_type );
}


$q = $q->getQuery()->getResult( \Doctrine\ORM\Query::HYDRATE_ARRAY );

任何帮助都将不胜感激。

1 个答案:

答案 0 :(得分:1)

如果我使用声明左连接代替内部联接,这不是确切的答案,但解决了我的目的:

$q->leftJoin( 'album.socialise_albumsSocialise_photo','photo');
$q->having('count(photo) > 1');