diff --git a/app/models/submission.rb b/app/models/submission.rb index bdd4692a39d29..5ab84b6676586 100644 --- a/app/models/submission.rb +++ b/app/models/submission.rb @@ -206,8 +206,8 @@ class Submission < ActiveRecord::Base end # see #needs_grading? - # When changing these conditions, consider updating index_submissions_on_assignment_id - # to maintain performance. + # When changing these conditions, update index_submissions_needs_grading to + # maintain performance. def self.needs_grading_conditions conditions = <<-SQL submissions.submission_type IS NOT NULL diff --git a/db/migrate/20170830204122_add_submissions_needs_grading_index.rb b/db/migrate/20170830204122_add_submissions_needs_grading_index.rb new file mode 100644 index 0000000000000..6814d4d410d04 --- /dev/null +++ b/db/migrate/20170830204122_add_submissions_needs_grading_index.rb @@ -0,0 +1,18 @@ +class AddSubmissionsNeedsGradingIndex < ActiveRecord::Migration[5.0] + tag :postdeploy + disable_ddl_transaction! + + def change + # see Submission.needs_grading; duplicated instead of called directly so the migration doesn't + # change even if the query does + add_index :submissions, :assignment_id, name: 'index_submissions_needs_grading', algorithm: :concurrently, where: <<-SQL + submissions.submission_type IS NOT NULL + AND (submissions.excused = 'f' OR submissions.excused IS NULL) + AND (submissions.workflow_state = 'pending_review' + OR (submissions.workflow_state IN ('submitted', 'graded') + AND (submissions.score IS NULL OR NOT submissions.grade_matches_current_submission) + ) + ) + SQL + end +end