Repository navigation
Continuous heavy per-mount SELECT COUNT(*) FROM oc_filecache queries on large instance (457 group folders) #279
Description
Title
Continuous heavy per-mount SELECT COUNT(*) FROM oc_filecache queries on large instance (457 group folders)
Which version of Nextcloud are you using?
33.0.9
Which version of PHP context_chat are you using?
5.4.0
Which version of backend context_chat are you using?
5.4.1
Nextcloud deployment method?
Bare metal, Ubuntu 22.04, PHP-FPM (PHP ), MariaDB 10.6.23
AppAPI 33.0.0, Group folders 21.0.15
Context Chat Backend deployed via AppAPI + HaRP on a remote GPU host. Upgraded from Context Chat 4.x.
Describe the Bug
The database is continuously busy with long-running SELECT COUNT(*) FROM oc_filecache ... queries,
one per mount. Several of them run in parallel at any time, and they keep re-appearing (also for low
group folder IDs such as 2 and 6, so the counting seems to start over again and again).
The queries match StorageService::countFilesInMount()
(https://github.com/nextcloud/context_chat/blob/v5.4.0/lib/Service/StorageService.php#L68).
They are not caused by a manually started occ context_chat:stats: while observing, either no stats
process was running or only a single one (which cannot explain several concurrent counts for different
group folders). The Context Chat admin settings page was not open.
Example from SHOW FULL PROCESSLIST (several of these concurrently, each ~1–7 s):
SELECT COUNT(*) FROM `oc_filecache` `filecache`
WHERE ((SELECT `p`.`encrypted` FROM `oc_filecache` `p` WHERE `p`.`fileid` = `filecache`.`parent`) = 0)
AND (`filecache`.`storage` = 2)
AND (`filecache`.`path` LIKE '__groupfolders/28/%')
AND (`filecache`.`path` NOT LIKE 'files_versions/%')
AND (`filecache`.`path` NOT LIKE 'files_trashbin/%')
AND (`filecache`.`storage` = '2')
AND (`filecache`.`mimetype` IN (29, 13, 20, 4, 27, 113, 14, 45, 34, 33, 8, 44, 9, 37, 118, 52, 67, 68, 135))
AND (`filecache`.`size` <= 104857600) AND (`filecache`.`size` > 0)
-- Time: 7 s, State: Sending dataNumbers from our instance
oc_filecache: ~1.9 M rows- Group folders: 457, all in the same storage (
storage = 2, paths__groupfolders/<id>/...) - Each per-mount query: ~1–7 s → group folders alone ≈ 8–50 min per full count, plus home storages
- All indices from
occ db:add-missing-indicesare present (incl.fs_storage_path_prefix) occ context_chat:statstakes more than one hour for the same reason
What we checked
- Only the backend (
ExApp/context_chat_backend/5.4.1) is calling Context Chat's OCS endpoints
(GET/DELETE /ocs/v2.php/apps/context_chat/queues/documents). - Queues are small:
oc_context_chat_queue~550 rows,oc_context_chat_action_queue~20 rows,
oc_context_chat_content_queue0 rows,oc_context_chat_fs_events~5 rows. last_indexed_timeis set:
- app: context_chat
- key: last_indexed_time
- value: 1789557889
- type: integer
- lazy: true
- PHP-FPM was restarted after setting
last_indexed_time. The COUNT queries still re-appear afterwards.
Possibly related code observations
QueueController::deleteDocumentsQueueItems()callssetInitialIndexCompletion()after every batch, which can
fall back toStorageService::countFiles():
https://github.com/nextcloud/context_chat/blob/v5.4.0/lib/Controller/QueueController.php#L184
https://github.com/nextcloud/context_chat/blob/v5.4.0/lib/Controller/QueueController.php#L376withinThreshold()looks inverted compared to its comment ("if the new files in the queue are less than 2% of
the total eligible files, we consider the initial indexing complete"):
return ((float)($total - $current) / (float)$total) < $threshold;It is called with $current = queuedNewFilesCount and $total = eligibleFilesCount. With e.g. 557 queued of
100 000 eligible files this evaluates to 0.994 < 0.02 → false, so completion would never be detected via this
path. The code is unchanged on main.
Since the queries continue even with last_indexed_time set and after a PHP-FPM restart, there seems to be
another caller of countFiles() / countFilesInMount() that we could not identify yet.
Possibly related: nextcloud/context_chat_backend#310 ("The initial indexing is still running" never completes).
Stack trace of the triggering request
<optional: excerpt from PHP-FPM slowlog containing countFiles / countFilesInMount>
To Reproduce
- Large instance (~2 M
oc_filecacherows) with several hundred group folders - Upgrade Context Chat from 4.x to 5.4.0, deploy Context Chat Backend 5.4.1 via AppAPI/HaRP
- Let the backend process the queues
- Observe
SHOW FULL PROCESSLIST→ repeated per-mountSELECT COUNT(*) FROM oc_filecache ...
Expected behavior
Counting all eligible files should not run continuously in the background and should not put permanent
load on the database. occ context_chat:stats should finish in reasonable time on large instances.
Activity
HKNDerRollo commentedon Sep 16, 2026
Maybe this can fix this:
`cd /var/www/nextcloud/apps/context_chat
cp lib/Service/StorageService.php /root/StorageService.php.orig
sed -i "s|createNamedParameter($path . '%')|createNamedParameter($this->db->escapeLikeParameter($path) . '%')|" lib/Service/StorageService.php
grep -n "escapeLikeParameter" lib/Service/StorageService.php
php -l lib/Service/StorageService.php`
marcelklehr commentedon Sep 29, 2026
Hi @HKNDerRollo
Thank you for taking the time to give feedback by opening this issue and sorry for the long wait!
We currently have a large load of issues and PRs, so please bear with us.
I've tried to come up with a fix for this issue over here: #280 Would you be willing to test it and/or review it?