Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

MDL-30185 question engine reporting: add redundant where to query for…

… perf.

Without this, MySQL fails to cope.
  • Loading branch information...
commit 471992218f8a29fcff1849f441c033b6335bf0da 1 parent e25ecdf
Tim Hunt timhunt authored
10 mod/quiz/report/attemptsreport.php
@@ -614,13 +614,21 @@ protected function add_latest_state_join($slot) {
614 614 return;
615 615 }
616 616
  617 + // This condition roughly filters the list of attempts to be considered.
  618 + // It is only used in a subselect to help crappy databases (see MDL-30122)
  619 + // therefore, it is better to use a very simple join, which may include
  620 + // too many records, than to do a super-accurate join.
  621 + $qubaids = new qubaid_join("{quiz_attempts} {$alias}quiza", "{$alias}quiza.uniqueid",
  622 + "{$alias}quiza.quiz = :{$alias}quizid", array("{$alias}quizid" => $this->sql->params['quizid']));
  623 +
617 624 $dm = new question_engine_data_mapper();
618   - $inlineview = $dm->question_attempt_latest_state_view($alias);
  625 + list($inlineview, $viewparams) = $dm->question_attempt_latest_state_view($alias, $qubaids);
619 626
620 627 $this->sql->fields .= ",\n$fields";
621 628 $this->sql->from .= "\nLEFT JOIN $inlineview ON " .
622 629 "$alias.questionusageid = quiza.uniqueid AND $alias.slot = :{$alias}slot";
623 630 $this->sql->params[$alias . 'slot'] = $slot;
  631 + $this->sql->params = array_merge($this->sql->params, $viewparams);
624 632 }
625 633
626 634 /**
63 question/engine/datalib.php
@@ -865,33 +865,42 @@ public function sum_usage_marks_subquery($qubaid) {
865 865 END) = 0";
866 866 }
867 867
868   - public function question_attempt_latest_state_view($alias) {
869   - return "(
870   - SELECT
871   - {$alias}qa.id AS questionattemptid,
872   - {$alias}qa.questionusageid,
873   - {$alias}qa.slot,
874   - {$alias}qa.behaviour,
875   - {$alias}qa.questionid,
876   - {$alias}qa.variant,
877   - {$alias}qa.maxmark,
878   - {$alias}qa.minfraction,
879   - {$alias}qa.flagged,
880   - {$alias}qa.questionsummary,
881   - {$alias}qa.rightanswer,
882   - {$alias}qa.responsesummary,
883   - {$alias}qa.timemodified,
884   - {$alias}qas.id AS attemptstepid,
885   - {$alias}qas.sequencenumber,
886   - {$alias}qas.state,
887   - {$alias}qas.fraction,
888   - {$alias}qas.timecreated,
889   - {$alias}qas.userid
890   -
891   - FROM {question_attempts} {$alias}qa
892   - JOIN {question_attempt_steps} {$alias}qas ON
893   - {$alias}qas.id = {$this->latest_step_for_qa_subquery($alias . 'qa.id')}
894   - ) $alias";
  868 + /**
  869 + * Get a subquery that returns the latest step of every qa in some qubas.
  870 + * Currently, this is only used by the quiz reports. See
  871 + * {@link quiz_attempt_report_table::add_latest_state_join()}.
  872 + * @param string $alias alias to use for this inline-view.
  873 + * @param qubaid_condition $qubaids restriction on which question_usages we
  874 + * are interested in. This is important for performance.
  875 + * @return array with two elements, the SQL fragment and any params requried.
  876 + */
  877 + public function question_attempt_latest_state_view($alias, qubaid_condition $qubaids) {
  878 + return array("(
  879 + SELECT {$alias}qa.id AS questionattemptid,
  880 + {$alias}qa.questionusageid,
  881 + {$alias}qa.slot,
  882 + {$alias}qa.behaviour,
  883 + {$alias}qa.questionid,
  884 + {$alias}qa.variant,
  885 + {$alias}qa.maxmark,
  886 + {$alias}qa.minfraction,
  887 + {$alias}qa.flagged,
  888 + {$alias}qa.questionsummary,
  889 + {$alias}qa.rightanswer,
  890 + {$alias}qa.responsesummary,
  891 + {$alias}qa.timemodified,
  892 + {$alias}qas.id AS attemptstepid,
  893 + {$alias}qas.sequencenumber,
  894 + {$alias}qas.state,
  895 + {$alias}qas.fraction,
  896 + {$alias}qas.timecreated,
  897 + {$alias}qas.userid
  898 +
  899 + FROM {$qubaids->from_question_attempts($alias . 'qa')}
  900 + JOIN {question_attempt_steps} {$alias}qas ON
  901 + {$alias}qas.id = {$this->latest_step_for_qa_subquery($alias . 'qa.id')}
  902 + WHERE {$qubaids->where()}
  903 + ) $alias", $qubaids->from_where_params());
895 904 }
896 905
897 906 protected function latest_step_for_qa_subquery($questionattemptid = 'qa.id') {

0 comments on commit 4719922

Please sign in to comment.
Something went wrong with that request. Please try again.