From 574b41c7c1997c17476d3f7c3b0897067a5d244e Mon Sep 17 00:00:00 2001 From: Menci Date: Tue, 16 Apr 2019 16:55:50 +0800 Subject: [PATCH] Optimize user-accepted-problems related SQL queries --- models/user.js | 24 +++++++++--------------- 1 file changed, 9 insertions(+), 15 deletions(-) diff --git a/models/user.js b/models/user.js index c67c475..d8db557 100644 --- a/models/user.js +++ b/models/user.js @@ -85,8 +85,9 @@ class User extends Model { async refreshSubmitInfo() { await syzoj.utils.lock(['User::refreshSubmitInfo', this.id], async () => { let JudgeState = syzoj.model('judge_state'); - let all = await JudgeState.model.findAll({ - attributes: ['problem_id'], + this.ac_num = await JudgeState.model.count({ + col: 'problem_id', + distinct: true, where: { user_id: this.id, status: 'Accepted', @@ -96,19 +97,13 @@ class User extends Model { } }); - let s = new Set(); - all.forEach(x => s.add(parseInt(x.get('problem_id')))); - this.ac_num = s.size; - - let cnt = await JudgeState.count({ + this.submit_num = await JudgeState.count({ user_id: this.id, type: { $ne: 1 // Not a contest submission } }); - this.submit_num = cnt; - await this.save(); }); } @@ -116,20 +111,19 @@ class User extends Model { async getACProblems() { let JudgeState = syzoj.model('judge_state'); - let all = await JudgeState.model.findAll({ - attributes: ['problem_id'], + let queryResult = await JudgeState.model.aggregate('problem_id', 'DISTINCT', { + plain: false, where: { user_id: this.id, status: 'Accepted', type: { $ne: 1 // Not a contest submissio } - } + }, + order: [["problem_id", "ASC"]] }); - let s = new Set(); - all.forEach(x => s.add(parseInt(x.get('problem_id')))); - return Array.from(s).sort((a, b) => a - b); + return queryResult.map(record => record['DISTINCT']) } async getArticles() {