MDL-43306 introduced a fix to prevent duplicate sortorder fields in grade_items, after restoring from back.
This works fine, but instead of being called once at the end of the restore, it is called once for every activity on the course.
On one of our customer's sites, the SQL query to detect duplicates is taking 10-15 seconds to run each time (due to both the number of activities on the course and the huge number of grade items on the site overall) - this is not great on its own, but when multiplied by 50+ activities in some of these courses, you end up with a 5+ minute delay added to the end of the restore from backup.
Identified on 3.9 site, but confirmed still an issue in latest 4.5 code.