Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf(core): Add index for jobs last_checked, reserved_at, time_sensitive #47324

Closed
wants to merge 1 commit into from

Conversation

provokateurin
Copy link
Member

Summary

Replaces the two existing indices with a single shared index saving some time on queries previously using both separate indices.

Checklist

@provokateurin provokateurin added this to the Nextcloud 31 milestone Aug 19, 2024
@provokateurin provokateurin requested review from nickvergessen, ChristophWurst, a team, ArtificialOwl, Altahrim and yemkareems and removed request for a team August 19, 2024 12:49
Comment on lines +38 to +41
# Add updated index
if (!$table->hasIndex('job_last_reserved_sensitive')) {
$table->addIndex(['last_checked', 'reserved_at', 'time_sensitive'], 'job_last_reserved_sensitive');
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# Add updated index
if (!$table->hasIndex('job_last_reserved_sensitive')) {
$table->addIndex(['last_checked', 'reserved_at', 'time_sensitive'], 'job_last_reserved_sensitive');
}

This should be done only on new installations and otherwise the change in core/Application.php takes care?
The best to achieve this is modifying the original migration that added the old indexes (you should also comment them out with a reference that Version31000Date20240819122840.php deletes them).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So just keep the old indices?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, deleting is fine. Just not creating new ones

Copy link
Member

@ChristophWurst ChristophWurst left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

\OC\Core\Migrations\Version13000Date20170718121200::changeSchema still creates the old index. Comment it out and add the new index with a comment as well saying that the indices were changed later on


# Add updated index
if (!$table->hasIndex('job_last_reserved_sensitive')) {
$table->addIndex(['last_checked', 'reserved_at', 'time_sensitive'], 'job_last_reserved_sensitive');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding an index can be slow if there is a lot of data. Would it be possible to use https://docs.nextcloud.com/server/latest/developer_manual/basics/storage/migrations.html#replacing-indices instead?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, that's the first change she did in core/Application

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah I didn't know about the possibility to replace indices.

@provokateurin
Copy link
Member Author

So should I replace or only add the index in Application.php?

@nickvergessen
Copy link
Member

Correct way is:

  1. Add $event->addMissingIndex( section in core/Application.php
  2. Add migration that deletes the old index
  3. Comment out the code that created the old index with a reference to the migration from 2. (could be in another migration file or in core/Application.php (like here)
  4. Find the migration file that creates the table (or the last one that creates all necessary columns for your index) and add the index there.

use OCP\Migration\IOutput;
use OCP\Migration\SimpleMigrationStep;

class Version31000Date20240819122840 extends SimpleMigrationStep {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See https://github.com/nextcloud/server/pull/46974/files#diff-a13c08ddffcec08aa3dfe030b79869cd4b164cf35f9a4fff0e22dbe9848b552eR20 for examples, DB migrations for 30+ should be annotated for the new 30+ feature to work 👍

@ChristophWurst
Copy link
Member

Correct way is:

1. Add `$event->addMissingIndex(` section in `core/Application.php`

2. Add migration that deletes the old index

3. Comment out the code that created the old index with a reference to the migration from 2. (could be in another migration file or in `core/Application.php` (like here)

4. Find the migration file that creates the table (or the last one that creates all necessary columns for your index) and add the index there.

I'm skeptical about this because it will leave instances in an unindexed state after the upgrade, until the admins apply optional indices.
We have the index replacement APIs for this case. The old index is not great but better than no index. Later on the admin can replace the index for best performance.

So my counter proposal is

  1. Adjust the original migration to comment out the old index and add the new
  2. Drop the old optional index logic
  3. Advertise the new index as replacement for the old, if it exists

@provokateurin provokateurin deleted the perf/core/jobs-index branch October 1, 2024 12:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
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
4 participants