Skip to content

Conversation

@Altahrim
Copy link
Collaborator

@Altahrim Altahrim commented Jan 26, 2026

Optimize requests like:

SELECT
  `storage_id`,
  `root_id`,
  `user_id`,
  `mount_point`,
  `mount_id`,
  `f`.`path`,
  `mount_provider_class`
FROM
  `oc_mounts` `m`
  INNER JOIN `oc_filecache` `f` ON `m`.`root_id` = `f`.`fileid`
WHERE
  (`storage_id` = ?)
  AND (
    (`f`.`fileid` = ?)
    OR (`f`.`path` = ?)
    OR (
      `CONCAT` (`f`.`path`, ?) = SUBSTRING (?, ..., `CHAR_LENGTH` (`f`.`path`) + ?)
    )
  )

TODO

  • Is it better to add both fields so DB can choose the best path? ⇒ Changed

Explain

Before

+------+-------------+-------+--------+-----------------------------------------------------------+--------------------+---------+--------------+-------+-------------+
| id   | select_type | table | type   | possible_keys                                             | key                | key_len | ref          | rows  | Extra       |
+------+-------------+-------+--------+-----------------------------------------------------------+--------------------+---------+--------------+-------+-------------+
|    1 | SIMPLE      | m     | ref    | mounts_storage_index,mounts_root_index,mount_user_storage | mount_user_storage | 8       | const        | 64048 |             |
|    1 | SIMPLE      | f     | eq_ref | PRIMARY                                                   | PRIMARY            | 8       | oc.m.root_id | 1     | Using where |
+------+-------------+-------+--------+-----------------------------------------------------------+--------------------+---------+--------------+-------+-------------+

After

+------+-------------+-------+------+-------------------------------------------------------------------------------------------------------------+---------------------+---------+-------------+-------+-------------+
| id   | select_type | table | type | possible_keys                                                                                               | key                 | key_len | ref         | rows  | Extra       |
+------+-------------+-------+------+-------------------------------------------------------------------------------------------------------------+---------------------+---------+-------------+-------+-------------+
|    1 | SIMPLE      | f     | ref  | PRIMARY,fs_storage_path_hash,fs_storage_mimetype,fs_storage_mimepart,fs_storage_size,fs_storage_path_prefix | fs_storage_mimetype | 8       | const       | 20918 | Using where |
|    1 | SIMPLE      | m     | ref  | mounts_root_index                                                                                           | mounts_root_index   | 8       | oc.f.fileid | 7     |             |
+------+-------------+-------+------+-------------------------------------------------------------------------------------------------------------+---------------------+---------+-------------+-------+-------------+

The change allows to filter on filecache first

(Rough) Benchmark

  • Before: ≈ 400 ms
  • After : ≈ 70 ms

@Altahrim Altahrim added this to the Nextcloud 34 milestone Jan 26, 2026
@Altahrim Altahrim self-assigned this Jan 26, 2026
@Altahrim Altahrim requested a review from a team as a code owner January 26, 2026 12:39
@Altahrim Altahrim added the 3. to review Waiting for reviews label Jan 26, 2026
@Altahrim Altahrim requested review from ArtificialOwl and removed request for a team January 26, 2026 12:39
@icewind1991
Copy link
Member

I would expect the performance comparison to go the other way around for instances that have a few mounts per user but a lot of files. Adding both comparisons and hoping the DB knows what to do is probably for the best.

@Altahrim
Copy link
Collaborator Author

On my instance, having both still filter on oc_mounts first and is slower…

@Altahrim
Copy link
Collaborator Author

Altahrim commented Jan 26, 2026

I was able to trigger the same EXPLAIN by adding a condition to the join: AND m.storage_id = f.storage
Then, I hope the DB will choose wisely :)

@Altahrim
Copy link
Collaborator Author

Altahrim commented Jan 26, 2026

/backport to stable33

Signed-off-by: Benjamin Gaussorgues <benjamin.gaussorgues@nextcloud.com>
@Altahrim Altahrim force-pushed the feat/user-mount-cache-perf branch from 9560e1c to f71f19c Compare January 27, 2026 15:31
@Altahrim Altahrim enabled auto-merge January 27, 2026 15:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants