Issue Task Under Review Quiet

#89058PageRouter::matchRequest could use a cache

Andreas Kienast 09/02/19, 11:49 updated 10/23/26, 13:43

Description

Currently, on every request PageRouter::matchRequest collects all slug candidates and chooses the best candidate. Depending on the amount of records in the database, this may lead to a lot of queries.

For a route that has been resolved previously, the result might get cached for faster resolving. This needs a proper concept in terms of cache invalidation.

Reviews 1

Files 4

History 1519

  1. Benni Mack commented <span data-processor="relativetime" data-value="2019-09-02T13:44:07+00:00" > 09/02/19, 13:44 </span>

    Hey Andy,

    where exactly are the multiple DB queries located?

    AFAIK getPagesFromDatabaseForCandidates() does only one query per language. If workspaces are enabled, we have more, of course.

  2. Carlos Meyer commented <span data-processor="relativetime" data-value="2019-11-11T17:04:15+00:00" > 11/11/19, 17:04 </span>

    We have the performance problem currently in an installation with 1600 domains too. In our case we fixed it by extending the query in the getPagesFromDatabaseForCandidates, so that only the pages of the current rootline are returned. But I’m not sure if there could be problems with mountpoints?

    Changes in PageRouter.php line 334 and below

      /**
         * Get all uids below given page uid
         *
         * @param $pageUID
         * @return string
         */
    
        private function getRecursivePageUIDs($pageUID)
        {
            $depth = 999999;
            $queryGenerator = GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Database\\QueryGenerator');
            $rGetTreeList = $queryGenerator->getTreeList($pageUID, $depth, 0, 1);
            //Will be a string
            return explode(',', $rGetTreeList);
    
        }
    
        /**
         * Check for records in the database which matches one of the slug candidates.
         *
         * @param array $slugCandidates
         * @param int $languageId
         * @return array
         */
        protected function getPagesFromDatabaseForCandidates(array $slugCandidates, int $languageId): array
        {
            $context = GeneralUtility::makeInstance(Context::class);
            $searchLiveRecordsOnly = $context->getPropertyFromAspect('workspace', 'isLive');
            $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
                ->getQueryBuilderForTable('pages');
            $queryBuilder
                ->getRestrictions()
                ->removeAll()
                ->add(GeneralUtility::makeInstance(DeletedRestriction::class))
                ->add(GeneralUtility::makeInstance(FrontendWorkspaceRestriction::class, null, null, $searchLiveRecordsOnly));
            //restrict query to sites in rootline
            $uids = $this->getRecursivePageUIDs($this->site->getRootPageId());
            $statement = $queryBuilder
                ->select('uid', 'l10n_parent', 'pid', 'slug')
                ->from('pages')
                ->where(
                    $queryBuilder->expr()->in('uid',$uids),
                    $queryBuilder->expr()->eq(
                        'sys_language_uid',
                        $queryBuilder->createNamedParameter($languageId, \PDO::PARAM_INT)
                    ),
                    $queryBuilder->expr()->in(
                        'slug',
                        $queryBuilder->createNamedParameter(
                            $slugCandidates,
                            Connection::PARAM_STR_ARRAY
                        )
                    )
                )
                // Exact match will be first, that's important
                ->orderBy('slug', 'desc')
                ->execute();
  3. Yohann CERDAN commented <span data-processor="relativetime" data-value="2020-02-05T10:20:29+00:00" > 02/05/20, 10:20 </span>

    Same problem here, we have a large website with 100+ domains and some mountpoints.
    The method getPagesFromDatabaseForCandidates() iterate a lot of non necessary pages (/home/about-us/offices/,/home/about-us/,/home/,/ for each site) and call a lot of methods for each.
    For examples, in my case, i will iterate overs 500+ pages to find the slug that appear in the first row of the query. This result in a big performance issue.

    So, even if we find a page wich are on the same site, we iterate all the others pages.
    If we have the good site ID and the exact same slug, maybe we could stop iteration here?

  4. Xavier Perseguers commented and changed attached typo3-core-speedup-slug-resolution-with-many-sites.patch <span data-processor="relativetime" data-value="2023-04-13T08:28:17+00:00" > 04/13/23, 08:28 </span>

    Just investigated this problem this morning and indeed, I implemented a simple cache which tremendously speeds up all my websites. This is day and night!

    From my Slack investigations (https://typo3.slack.com/archives/C025BQLFA/p1681370066698969):

    - Around 300 domains in the install
    - Using mount point for shared pages (such as the authentication page which is the same for every site)
    - Resolving the candidates for /share/authentication for a given domain takes around 3.5 sec (locally, needs to loop over 600+ rows)
    - Resolving the root page such as /fr for a given domain takes around 1.6 sec.
    - Using the simple cache I did then only takes a few ms (!!!)

    Please see patch as attachement.

    I’m using a cache for a custom extension that automatically flushes the caches based on modified table name + uid by hooking into DataHandler, that’s why I think I can safely use a 90 day cache validity and I generate tags of the form pages%UID

  5. Xavier Perseguers commented <span data-processor="relativetime" data-value="2023-04-13T08:53:06+00:00" > 04/13/23, 08:53 </span>

    After adapting the original patch from @protos as provided in #89058-6 to work with current TYPO3 v10, it turns out this does not work at all to restrict the query on the subpages of a given site when you work with mount points.

    Too bad indeed, I liked the idea even though the very first call was then much longer to complete since it had to compute the full page tree of my domain once.

    So I’ll go back to my caching solution for the time being.

  6. Xavier Perseguers commented and changed attached typo3-core-89058-speed-up-slug-resolution-with-many-sites.patch <span data-processor="relativetime" data-value="2023-04-13T09:44:11+00:00" > 04/13/23, 09:44 </span>

    Please find enclosed (typo3-core-89058-speed-up-slug-resolution-with-many-sites.patch) a generic solution that doesn’t rely on my own caching mechanism.

  7. Xavier Perseguers commented and changed attached typo3-core-89058-speed-up-slug-resolution-with-many-sites_v2.patch <span data-processor="relativetime" data-value="2025-06-20T12:17:17+00:00" > 06/20/25, 12:17 </span>

    tracked down a bug where “anonymous result sets” could pollute the cache and prevent some legit page localization from working (would result in a 404 when trying to access a translation because the translated page record would not be present in the [wrong] cache entry. Problem spotted and fixed in TYPO3 v11.

  8. Gerrit Code Review pushed patch set 1 for main to change 95753 status Accepted Under Review <span data-processor="relativetime" data-value="2026-09-09T21:55:04+00:00" > 09/09/26, 21:55 </span>