Skip to content

Reduce DB query number in post index action #460

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed

Reduce DB query number in post index action #460

wants to merge 3 commits into from

Conversation

yceruto
Copy link
Member

@yceruto yceruto commented Feb 12, 2017

Performance Issue

After tags feature the blog index page performs one extra query for each post to fetches its related tags. This PR avoids that.

Other changes:

  • Refactoring the Post repository methods to reuse paginator creation in the future.

Copy link
Member

@javiereguiluz javiereguiluz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Nice catch of this issue about the number of database queries! Thanks!

@Grafikart
Copy link
Contributor

Grafikart commented Feb 14, 2017

I noticed this issue too but I'm not sure avout this fix since the LIMIT is then applied to the tags. If the first post has 10 tags, then it will be the only one in the listing right ?

Doctrine doesn't seem to work well in this case. My solution for this kind of issues is to hydrate manually the entities using a new query on the joinTable.

@javiereguiluz
Copy link
Member

@Grafikart I'm not sure I understand your comment. This is how join queries are supposed to work in Doctrine. I always use a code similar to this one. I've just tested this PR locally and it seems to be working as expected. The results are the same, but the database queries:

Before After
db-before db-after

@javiereguiluz
Copy link
Member

Thanks Yonel.

@Grafikart
Copy link
Contributor

Grafikart commented Feb 14, 2017

@javiereguiluz sorry it works with PagerFanta indeed. It didn't worked for me cause I tried this PR without the paginator :

        $query = $this->getEntityManager()
            ->createQuery('
                SELECT p, t
                FROM AppBundle:Post p
                LEFT JOIN p.tags t
                WHERE p.publishedAt <= :now
                ORDER BY p.publishedAt DESC
            ')
            ->setMaxResults(5)
            ->setParameter('now', new \DateTime())

But doing a first query to get the posts ids (like PagerFanta does) solve this issue. Sorry for the false issue.

@yceruto yceruto deleted the performance branch February 14, 2017 12:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants