3

I have MN relation between USER and CATEGORY table. Problem appear when user has more than 100 interests. When I execute very simple query like this:

  return $this->createQueryBuilder('usercategory')
        ->innerJoin('usercategory.user', 'u')
        ->innerJoin('usercategory.category', 'c')
        ->where('u.id = :user_id')
        ->setParameter('user_id', $user_id)
        ->getQuery()
        ->getResult();

Symfony profiler reported that more than 100 queries are executed with more than 150ms time for execution. 90% of those queries look like

SELECT t0.category_name AS category_name1,t0.type AS type2, t0.status AS status3,  t0.category_id AS category_id4, t0.parent_id AS parent_id5 FROM categories t0 WHERE t0.category_id = ?

This mean, if user has 100 categories, doctrine will execute 100 query to fetch them all.

Am i doing something wrong and how i can use make it faster by using DQL?

thx, MIlos

2
  • 4
    ->addSelect('c') will cause the categories to be returned from your query (as opposed to lazy loading them). Commented Nov 18, 2013 at 16:07
  • 2
    @Cerad Please make your comment as answer so i can accept it. thx again! Commented Nov 19, 2013 at 7:27

1 Answer 1

4
->addSelect('c') 

will cause the categories to be returned from your query (as opposed to lazy loading them).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.