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() {
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() {

Loading…
Cancel
Save