Browse Source

Optimize user-accepted-problems related SQL queries

pull/6/head
Menci 6 years ago
parent
commit
574b41c7c1
  1. 24
      models/user.js

24
models/user.js

@ -85,8 +85,9 @@ class User extends Model {
async refreshSubmitInfo() { async refreshSubmitInfo() {
await syzoj.utils.lock(['User::refreshSubmitInfo', this.id], async () => { await syzoj.utils.lock(['User::refreshSubmitInfo', this.id], async () => {
let JudgeState = syzoj.model('judge_state'); let JudgeState = syzoj.model('judge_state');
let all = await JudgeState.model.findAll({ this.ac_num = await JudgeState.model.count({
attributes: ['problem_id'], col: 'problem_id',
distinct: true,
where: { where: {
user_id: this.id, user_id: this.id,
status: 'Accepted', status: 'Accepted',
@ -96,19 +97,13 @@ class User extends Model {
} }
}); });
let s = new Set(); this.submit_num = await JudgeState.count({
all.forEach(x => s.add(parseInt(x.get('problem_id'))));
this.ac_num = s.size;
let cnt = await JudgeState.count({
user_id: this.id, user_id: this.id,
type: { type: {
$ne: 1 // Not a contest submission $ne: 1 // Not a contest submission
} }
}); });
this.submit_num = cnt;
await this.save(); await this.save();
}); });
} }
@ -116,20 +111,19 @@ class User extends Model {
async getACProblems() { async getACProblems() {
let JudgeState = syzoj.model('judge_state'); let JudgeState = syzoj.model('judge_state');
let all = await JudgeState.model.findAll({ let queryResult = await JudgeState.model.aggregate('problem_id', 'DISTINCT', {
attributes: ['problem_id'], plain: false,
where: { where: {
user_id: this.id, user_id: this.id,
status: 'Accepted', status: 'Accepted',
type: { type: {
$ne: 1 // Not a contest submissio $ne: 1 // Not a contest submissio
} }
} },
order: [["problem_id", "ASC"]]
}); });
let s = new Set(); return queryResult.map(record => record['DISTINCT'])
all.forEach(x => s.add(parseInt(x.get('problem_id'))));
return Array.from(s).sort((a, b) => a - b);
} }
async getArticles() { async getArticles() {

Loading…
Cancel
Save