-
Notifications
You must be signed in to change notification settings - Fork 179
Description
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 eachcontent_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:
(The remaining queries are additional setcontent
s 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.