Skip to content

perf(folder): avoid on-disk temp table when sorting folders by group count#4736

Open
solracsf wants to merge 1 commit into
masterfrom
perf/folders-group-sort-tmp-table
Open

perf(folder): avoid on-disk temp table when sorting folders by group count#4736
solracsf wants to merge 1 commit into
masterfrom
perf/folders-group-sort-tmp-table

Conversation

@solracsf
Copy link
Copy Markdown
Member

@solracsf solracsf commented Jun 3, 2026

Summary

Found while working on nextcloud/server#60932

The admin list, when sorted by the Groups column, calls FolderManager::getAllFoldersWithSize($offset, $limit, 'groups', $order). That code path ordered folders by their applicable-group count with a LEFT JOIN group_folders_groups + GROUP BY f.folder_id:

$query
    ->leftJoin('f', 'group_folders_groups', 'g', $query->expr()->eq('f.folder_id', 'g.folder_id'))
    ->groupBy('f.folder_id')
    ->orderBy($query->func()->count('g.applicable_id'), $order);

The GROUP BY forces the database to materialize an internal temporary table. Because selectWithFileCache() selects the options column, a LONGTEXT, that temporary table cannot use the in-memory MEMORY engine (it cannot hold TEXT/BLOB), so MariaDB/MySQL always spill it to an on-disk (Aria) temporary table, regardless of tmp_table_size, on every request that uses this sort.

Validation (MariaDB 10.11, 3 group folders, 60k oc_filecache rows)

EXPLAIN, driving table f:

Extra
before Using where; Using temporary; Using filesort
after Using where; Using filesort

SHOW STATUS around the query (tmp_table_size = max_heap_table_size = 64M):

Created_tmp_disk_tables
before 1
after 0

Result-equivalence: the returned folder order is identical for both ASC and DESC, verified by building the query through IQueryBuilder against the real database.

Tests

Adds FolderManagerTest::testGetAllFoldersWithSizeOrderedByGroups, which creates folders with 1/2/3 applicable groups and asserts the 'groups' sort returns them ordered by group count (both ascending and descending).

@solracsf solracsf added 3. to review Items that need to be reviewed performance 🚀 labels Jun 3, 2026
…count

Signed-off-by: Git'Fellow <12234510+solracsf@users.noreply.github.com>
@solracsf solracsf force-pushed the perf/folders-group-sort-tmp-table branch from 54e3b4f to 8e8a70b Compare June 3, 2026 08:30
@solracsf solracsf self-assigned this Jun 3, 2026
@icewind1991
Copy link
Copy Markdown
Member

It's probably best to just drop the ordering altogether

@solracsf
Copy link
Copy Markdown
Member Author

solracsf commented Jun 3, 2026

Cc @provokateurin since this has been introduced by 38dfb30

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

3. to review Items that need to be reviewed performance 🚀

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants