A file in a question is restored once per non-empty course question bank subcategory. If you have 10 questions each with a different JPEG, you'd expect there to be 10 corresponding file restores, but if you have N non-empty question bank subcategories there are actually N×10 restores. If you have many files and/or subcategories this number can get very big making course restores take longer than necessary and increasing server load during the restore.
If anyone is interested on working with me on this uninspiring issue I think the starting points are:
- Confirm if my analysis of the problem correct.
- Investigate whether the fix is as straightforward as my initial suggestion supposes.
- See if there is any/enough automated test coverage to be confident that any proposed fix doesn't break restores.