That query comes from context_module::instance, which is called in many places in Moodle.
That one call is exactly that: one call (to get the context of the quiz that the quiz attempt belongs to). That is not your problem.
The thousands of calls to that query are coming form somewhere else. You need to find where the thousands of calls are coming form, not the one.
The short form of the stack trace is much better. The nicest format for errors I know is what PHP outputs when you tell it to display HTML error messages. There is a typical screen-grab on http://jpgraph.net/download/manuals/chunkhtml/ch13s07.html.