SQL isn't my forte, but we recently encountered something similar and it turned out we had accidentally uploaded 18 pages of unrelated quiz categories and associated questions to a quiz. Anything done with that quiz slowed performance drastically. If this is what is causing the issue, the fix is tedious and time consuming - you have to remove the extra categories and questions and/or rebuild the quiz from scratch. If no one has written the quiz, then it is easiest to export the categories and questions specific to the quiz, create a new quiz activity, import the categories and questions, and rebuild the quiz, then delete the faulty quiz (this may even work with other problems.
If the quiz has been used, then it becomes a bit more complicated, particularly if you have additional users needing to write the quiz. The same process to can be used, but in addition, you will need to export the associated marks and apply them to the new quiz - we did this directly into the grade book and the quiz marks show up as overwritten in the grade book.
The alternate is to manually delete the extra categories, then the questions to clean up the question bank associated with the quiz. This is the tedious and time-consuming method, but has lease impact on the users and the course. Our 18 pages took about 24 hours.