Optimize db query

This commit is contained in:
cuom1999 2023-02-13 17:57:27 -06:00
parent b95acb6202
commit 5e4b289833

View file

@ -22,37 +22,48 @@ def get_pp_breakdown(user, start=0, end=settings.DMOJ_PP_ENTRIES):
with connection.cursor() as cursor: with connection.cursor() as cursor:
cursor.execute( cursor.execute(
""" """
SELECT max_points_table.problem_code, SELECT submission.problem_code,
max_points_table.problem_name, submission.problem_name,
max_points_table.max_points, submission.max_points,
judge_submission.id, submission.sub_id,
judge_submission.date, submission.sub_date,
judge_submission.case_points, submission.case_points,
judge_submission.case_total, submission.case_total,
judge_submission.result, submission.result,
judge_language.short_name, judge_language.short_name,
judge_language.key judge_language.key
FROM
(SELECT max_points_table.problem_code problem_code,
max_points_table.problem_name problem_name,
max_points_table.max_points max_points,
judge_submission.id sub_id,
judge_submission.date sub_date,
judge_submission.case_points case_points,
judge_submission.case_total case_total,
judge_submission.result result,
judge_submission.language_id language_id
FROM judge_submission FROM judge_submission
JOIN (SELECT judge_problem.id problem_id, JOIN (
SELECT judge_problem.id problem_id,
judge_problem.name problem_name, judge_problem.name problem_name,
judge_problem.code problem_code, judge_problem.code problem_code,
MAX(judge_submission.points) AS max_points MAX(judge_submission.points) AS max_points
FROM judge_problem FROM judge_problem
INNER JOIN judge_submission ON (judge_problem.id = judge_submission.problem_id) INNER JOIN judge_submission
WHERE (judge_problem.is_public = True AND ON (judge_problem.id = judge_submission.problem_id)
judge_problem.is_organization_private = False AND WHERE (judge_problem.is_public = True AND judge_problem.is_organization_private = False AND judge_submission.points IS NOT NULL AND judge_submission.user_id = %s)
judge_submission.points IS NOT NULL AND
judge_submission.user_id = %s)
GROUP BY judge_problem.id GROUP BY judge_problem.id
HAVING MAX(judge_submission.points) > 0.0) AS max_points_table HAVING MAX(judge_submission.points) > 0.0
ON (judge_submission.problem_id = max_points_table.problem_id AND ) AS max_points_table
judge_submission.points = max_points_table.max_points AND ON (judge_submission.problem_id = max_points_table.problem_id AND judge_submission.points = max_points_table.max_points AND judge_submission.user_id = %s)
judge_submission.user_id = %s)
JOIN judge_language
ON judge_submission.language_id = judge_language.id
GROUP BY max_points_table.problem_id GROUP BY max_points_table.problem_id
ORDER BY max_points DESC, judge_submission.date DESC ORDER BY max_points DESC, judge_submission.date DESC
LIMIT %s OFFSET %s LIMIT %s
OFFSET %s
) AS submission
JOIN judge_language
ON submission.language_id = judge_language.id
ORDER BY submission.max_points DESC, submission.sub_date DESC;
""", """,
(user.id, user.id, end - start + 1, start), (user.id, user.id, end - start + 1, start),
) )