Reduce SQL queries to find a unsolved recommended RfC

Fixes CODEOCEAN-JQ
This commit is contained in:
Sebastian Serth
2023-03-14 12:58:47 +01:00
parent 939b31967f
commit 417ead3d3f
3 changed files with 36 additions and 4 deletions

View File

@ -133,9 +133,14 @@ class Submission < ApplicationRecord
end
def unsolved_rfc(user = self.user)
Pundit.policy_scope(user, RequestForComment).unsolved.where(exercise_id: exercise).where.not(question: nil).where(created_at: OLDEST_RFC_TO_SHOW.ago..Time.current).order('RANDOM()').find do |rfc_element|
((rfc_element.comments_count < MAX_COMMENTS_ON_RECOMMENDED_RFC) && !rfc_element.question.empty?)
end
Pundit.policy_scope(user, RequestForComment)
.unsolved.where.not(question: [nil, ''])
.where(exercise_id: exercise, created_at: OLDEST_RFC_TO_SHOW.ago...)
.left_joins(:comments)
.having('COUNT(comments.id) < ?', MAX_COMMENTS_ON_RECOMMENDED_RFC)
.group(:id)
.order('RANDOM()').limit(1)
.first
end
# @raise [Runner::Error] if the score could not be calculated due to a failure with the runner.

View File

@ -0,0 +1,7 @@
# frozen_string_literal: true
class AddIndexForRecommendedRfcs < ActiveRecord::Migration[7.0]
def change
add_index :request_for_comments, %i[exercise_id created_at], where: "(NOT solved OR solved IS NULL) AND (question IS NOT NULL AND question <> '')", name: :index_unresolved_recommended_rfcs
end
end

View File

@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema[7.0].define(version: 2023_02_19_113125) do
ActiveRecord::Schema[7.0].define(version: 2023_03_14_084733) do
# These are extensions that must be enabled in order to support this database
enable_extension "pg_trgm"
enable_extension "pgcrypto"
@ -387,6 +387,7 @@ ActiveRecord::Schema[7.0].define(version: 2023_02_19_113125) do
t.text "thank_you_note"
t.boolean "full_score_reached", default: false
t.integer "times_featured", default: 0
t.index ["exercise_id", "created_at"], name: "index_unresolved_recommended_rfcs", where: "(((NOT solved) OR (solved IS NULL)) AND ((question IS NOT NULL) AND (question <> ''::text)))"
t.index ["exercise_id"], name: "index_request_for_comments_on_exercise_id"
t.index ["submission_id"], name: "index_request_for_comments_on_submission_id"
t.index ["user_id", "user_type", "created_at"], name: "index_rfc_on_user_and_created_at", order: { created_at: :desc }
@ -570,6 +571,25 @@ ActiveRecord::Schema[7.0].define(version: 2023_02_19_113125) do
t.index ["user_type", "user_id"], name: "index_user_proxy_exercise_exercises_on_user"
end
create_table "wk2020_until_rfc_reply", id: false, force: :cascade do |t|
t.integer "user_id"
t.integer "exercise_id"
t.interval "working_time_until_rfc_reply"
end
create_table "wk2020_with_wk_until_rfc", id: false, force: :cascade do |t|
t.string "external_user_id", limit: 255
t.integer "user_id"
t.integer "exercise_id"
t.float "max_score"
t.float "max_reachable_points"
t.interval "working_time"
t.interval "working_time_until_rfc"
t.interval "working_time_until_rfc_reply"
t.time "percentile75"
t.time "percentile90"
end
add_foreign_key "authentication_tokens", "study_groups"
add_foreign_key "community_solution_contributions", "community_solution_locks"
add_foreign_key "community_solution_contributions", "community_solutions"