Skip to content

"setcontent" Performance #1333

@technicallyerik

Description

@technicallyerik

Enhancement

I was digging around the Symfony debugger and noticed ~180 queries used to build my homepage.

Right now, using setcontent (or any of the Frontend controllers that use it) does a separate query for:

  • List of content IDs that match the query
  • A query to get all the bolt_field for each content_id
  • An individual query for each bolt_field_translation

Additionally, if you use the taxonomies or related twig filters, additional queries are made.

Environment

Question Answer
Relevant Bolt Version 4.0.0-rc.17
Install type Composer install
PHP version 7.3.9
Web server Apache

Suggestion

Always join the related tables, and then filter by any criteria in the query.

I hardcoded a test twig query for the listing part of my homepage:

        $qb = $om->createQueryBuilder();
        $qb->select(['c', 'f', 't'])
            ->from(Content::class, 'c')
            ->innerJoin('c.fields', 'f')
            ->innerJoin('f.translations', 't')
            ->where('c.contentType IN (:cts)')
            ->setParameter('cts', ['blog','tutorials','links','quotes'])
            ->andWhere('c.status = :status')
            ->setParameter('status', Statuses::PUBLISHED)
            ->orderBy('c.publishedAt', 'DESC');
        $query = $qb->getQuery();
        $paginator = new Pagerfanta(new DoctrineORMAdapter($query, true, true));
        $paginator->setMaxPerPage(20);
        $paginator->setCurrentPage(1);

And it reduced the queries and time from:
Screen Shot 2020-04-26 at 3 33 58 PM

to:
Screen Shot 2020-04-26 at 3 34 10 PM

(The remaining queries are additional setcontents I use for the header and footer that would additionally be reduced)

Comments

I noticed some work in progress (#488) as well as a future ticket for caching that might make this invalid (#162), but wanted to raise the issue for posterity.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions