nextcloud / nextcloud/server

Some queries have a possible_index but are not using it with too huge IN(…)

Open
#30,042 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

1. to develop feature: caldav feature: database feature: status performance 🚀 technical debt
Dominant language
PHP
Stars
36.9k
Forks
5.2k
Avg merge
2d 3h
Merged PRs (30d)
713

Description

Getting user status for many users (e.g. the talk room list is loading the status of all one-to-one conversations)
 EXPLAIN SELECT * FROM `oc_user_status` WHERE `user_id` IN ('…', '…', '…', '…', '…', '…', '…', '…', '…', '…', '…', '…', '…', '…', '…', '…', '…', '…', '…', '…', '…', '…', '…', '…', '…', '…', '…', '…', '…', '…', '…', '…', '…', '…', '…', '…', '…', '…', '…', '…', '…', '…', '…', '…', '…', '…', '…', '…', '…', '…', '…', '…', '…', '…', '…', '…', '…', '…', '…', '…', '…', '…', '…', '…', '…', '…', '…', '…', '…', '…', '…', '…', '…', '…', '…', '…', '…', '…', '…', '…', '…', '…', '…', '…', '…');
+------+-------------+----------------+------+--------------------+------+---------+------+------+-------------+
| id   | select_type | table          | type | possible_keys      | key  | key_len | ref  | rows | Extra       |
+------+-------------+----------------+------+--------------------+------+---------+------+------+-------------+
|    1 | SIMPLE      | oc_user_status | ALL  | user_status_uid_ix | NULL | NULL    | NULL |  227 | Using where |
+------+-------------+----------------+------+--------------------+------+---------+------+------+-------------+

If the count is below 21 the index is used:

EXPLAIN SELECT * FROM `oc_user_status` WHERE `user_id` IN ('…', '…', '…', '…', '…', '…', '…', '…', '…', '…', '…', '…', '…', '…', '…', '…', '…', '…', '…');
+------+-------------+----------------+-------+--------------------+--------------------+---------+------+------+-----------------------+
| id   | select_type | table          | type  | possible_keys      | key                | key_len | ref  | rows | Extra                 |
+------+-------------+----------------+-------+--------------------+--------------------+---------+------+------+-----------------------+
|    1 | SIMPLE      | oc_user_status | range | user_status_uid_ix | user_status_uid_ix | 1022    | NULL |   19 | Using index condition |
+------+-------------+----------------+-------+--------------------+--------------------+---------+------+------+-----------------------+
Similar issue with getting calendars
EXPLAIN SELECT `displayname`, `description`, `timezone`, `calendarorder`, `calendarcolor`, `deleted_at`, `a`.`id`, `a`.`uri`, `a`.`synctoken`, `a`.`components`, `a`.`principaluri`, `a`.`transparent`, `s`.`access` FROM `oc_dav_shares` `s` INNER JOIN `oc_calendars` `a` ON `s`.`resourceid` = `a`.`id` WHERE (`s`.`principaluri` IN ('…', '…', '…', '…', '…', '…', '…', '…', '…')) AND (`s`.`type` = 'calendar');
+------+-------------+-------+--------+------------------+---------+---------+-----------------+------+-------------+
| id   | select_type | table | type   | possible_keys    | key     | key_len | ref             | rows | Extra       |
+------+-------------+-------+--------+------------------+---------+---------+-----------------+------+-------------+
|    1 | SIMPLE      | s     | ALL    | dav_shares_index | NULL    | NULL    | NULL            |   96 | Using where |
|    1 | SIMPLE      | a     | eq_ref | PRIMARY          | PRIMARY | 8       | oc.s.resourceid |    1 |             |
+------+-------------+-------+--------+------------------+---------+---------+-----------------+------+-------------+
2 rows in set (0.004 sec)

again fixed when using a smaller IN:

EXPLAIN SELECT `displayname`, `description`, `timezone`, `calendarorder`, `calendarcolor`, `deleted_at`, `a`.`id`, `a`.`uri`, `a`.`synctoken`, `a`.`components`, `a`.`principaluri`, `a`.`transparent`, `s`.`access` FROM `oc_dav_shares` `s` INNER JOIN `oc_calendars` `a` ON `s`.`resourceid` = `a`.`id` WHERE (`s`.`principaluri` IN ('…', '…', '…')) AND (`s`.`type` = 'calendar');
+------+-------------+-------+--------+------------------+------------------+---------+-----------------+------+-----------------------+
| id   | select_type | table | type   | possible_keys    | key              | key_len | ref             | rows | Extra                 |
+------+-------------+-------+--------+------------------+------------------+---------+-----------------+------+-----------------------+
|    1 | SIMPLE      | s     | range  | dav_shares_index | dav_shares_index | 1023    | NULL            |    8 | Using index condition |
|    1 | SIMPLE      | a     | eq_ref | PRIMARY          | PRIMARY          | 8       | oc.s.resourceid |    1 |                       |
+------+-------------+-------+--------+------------------+------------------+---------+-----------------+------+-----------------------+
2 rows in set (0.005 sec)

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the two EXPLAIN query pairs in the issue, comparing index selection for large and small IN lists. Trace the server code paths that load user statuses and calendars, then verify that both query patterns use appropriate indexes without changing their results.

Written by the indexing model from the issue text.

Assessment

Tech stack
php, sql
Domain
backend, databases
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.