php - Inner join in my doctrine2 query increases execution time from 400 milliseconds to 5 secs -
inner join in doctrine2 query increases execution time 400 milliseconds 5 secs, if extract sql , run directly in sqlserver studio takes few milliseconds execute.
below query, bottle neck inner join only:
$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 , wp.wall_type = ?4' ); $q->orwhere( 'wp.wall_postsfrom_user in (:my_links_r) , wp.wall_type = ?4' ); $q->orwhere( 'likes.likesliked_by in (:my_links_r) , wp.wall_type = ?4' ); $q->orwhere( 'shares.sharesshared_by in (:my_links_r) , wp.wall_type = ?4' ); $q->orwhere( 'comments.commentsilook_user in (:my_links_r) , wp.wall_type = ?4' ); $q->andwhere('fromuser.account_closed_on 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 );
following sql extract, runs fantastic inner join also:
select top 10 w0_.id id0, w0_.last_activity_datetime last_activity_datetime1 wall_post w0_ left join likes l1_ on w0_.id = l1_.wall_post_id left join share s2_ on w0_.id = s2_.wall_post_id left join comments c3_ on w0_.id = c3_.wall_post_id left join ilook_user i4_ on w0_.from_user_id = i4_.id left join socialise_album s5_ on w0_.socialise_album_id = s5_.id inner join socialise_photo s6_ on s5_.id = s6_.socialise_album_id ((w0_.from_user_id = 1 , w0_.wall_type = 2) or (w0_.from_user_id in (1) , w0_.wall_type = 2) or (s2_.ilook_user_id in (1) , w0_.wall_type = 2)) , i4_.account_closed_on null group w0_.id, w0_.last_activity_datetime order w0_.last_activity_datetime desc
any appreciated.
it not exact answer solves purpose if use having statement , left join in stead of inner join:
$q->leftjoin( 'album.socialise_albumssocialise_photo','photo'); $q->having('count(photo) > 1');
Comments
Post a Comment