Skip to content

fix(db): add time_sensitive to oc_jobs index for maintenance window performance#58540

Open
turanalmammadov wants to merge 1 commit intonextcloud:masterfrom
turanalmammadov:fix/add-time-sensitive-to-jobs-index
Open

fix(db): add time_sensitive to oc_jobs index for maintenance window performance#58540
turanalmammadov wants to merge 1 commit intonextcloud:masterfrom
turanalmammadov:fix/add-time-sensitive-to-jobs-index

Conversation

@turanalmammadov
Copy link

Fixes #46126

Problem

The background job scheduler uses this query to pick the next job:

SELECT * FROM oc_jobs
WHERE reserved_at <= ?
  AND last_checked <= ?
  AND time_sensitive = ?
ORDER BY last_checked ASC
LIMIT ?

The old index job_lastcheck_reserved only covered (last_checked, reserved_at). The database had to do a partial index scan and then fetch each row individually to evaluate time_sensitive = ?. Under load with a maintenance window this causes many unnecessary I/O operations since most rows are excluded by the time_sensitive condition.

Solution

Drop job_lastcheck_reserved and replace it with job_lastcheck_timesens on (last_checked, reserved_at, time_sensitive). Because the query's WHERE columns are now all in the index the database can use a covering index scan, evaluating the time_sensitive predicate without reading any data rows.

Changes

1. New DB migration

core/Migrations/Version33000Date20260224000000.php

  • Drops old index job_lastcheck_reserved
  • Creates new index job_lastcheck_timesens with all three columns

2. AddMissingIndicesListener update

core/Listener/AddMissingIndicesListener.php

  • Updated to reference the new index name and columns
  • Ensures occ db:add-missing-indices repairs existing installations

Impact

  • Faster cron execution, especially when a maintenance window is active
  • Reduces database I/O during background job scheduling
  • No functional change — index covers the same query that already existed

Made with Cursor

…erformance

The background job scheduler uses this query to find eligible jobs:

  SELECT * FROM oc_jobs
  WHERE reserved_at <= ?
    AND last_checked <= ?
    AND time_sensitive = ?
  ORDER BY last_checked ASC
  LIMIT ?

The previous index 'job_lastcheck_reserved' only covered (last_checked,
reserved_at), forcing a partial index scan plus individual row reads to
evaluate the time_sensitive predicate. Under heavy load this causes
unnecessary I/O especially during a maintenance window where most rows
will be filtered out by the time_sensitive condition.

Changes:
1. New migration drops 'job_lastcheck_reserved' and creates a replacement
   index 'job_lastcheck_timesens' on (last_checked, reserved_at, time_sensitive).
   The new index is a covering index for the scheduler query, eliminating
   the row-level time_sensitive evaluation entirely.

2. AddMissingIndicesListener updated to expect the new index name and columns
   so that occ db:add-missing-indices picks it up on existing installations.

Fixes: nextcloud#46126
Co-authored-by: Cursor <cursoragent@cursor.com>
@turanalmammadov turanalmammadov requested a review from a team as a code owner February 24, 2026 13:14
@turanalmammadov turanalmammadov requested review from Altahrim, artonge, come-nc and icewind1991 and removed request for a team February 24, 2026 13:14
@artonge
Copy link
Contributor

artonge commented Feb 24, 2026

Hey @turanalmammadov, thanks for the pull request, and thanks for being transparent with your usage of AI.

Can you double-check that this won't impact other request negatively? I don't know if such requests exist, but we might have some that do not include the time sensitive filter, and if so, I think those requests won't be able to use the index anymore.

Also, can you share more regarding the actual impact in terms of numbers? How many rows do you have in your oc_jobs table, and how much time it takes to run the query with and without your changes?

@artonge artonge added AI assisted php Pull requests that update Php code enhancement labels Feb 24, 2026
@artonge artonge added this to the Nextcloud 34 milestone Feb 24, 2026
@artonge artonge added the 2. developing Work in progress label Feb 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

2. developing Work in progress AI assisted enhancement php Pull requests that update Php code

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Missing column in database index on query for time sensitive background jobs

2 participants