add index for models

This commit is contained in:
2026-04-23 14:10:48 -06:00
parent 028ea6e5f9
commit e2d566436f
13 changed files with 165 additions and 29 deletions

View File

@@ -0,0 +1,19 @@
# Generated by Django 6.0 on 2026-04-23 20:07
from django.conf import settings
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('announcement', '0001_initial'),
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
]
operations = [
migrations.AddIndex(
model_name='announcement',
index=models.Index(fields=['visible', '-top', '-create_time'], name='announcement_list_idx'),
),
]

View File

@@ -18,3 +18,6 @@ class Announcement(models.Model):
class Meta: class Meta:
db_table = "announcement" db_table = "announcement"
ordering = ("-top", "-create_time",) ordering = ("-top", "-create_time",)
indexes = [
models.Index(fields=["visible", "-top", "-create_time"], name="announcement_list_idx"),
]

View File

@@ -0,0 +1,21 @@
# Generated by Django 6.0 on 2026-04-23 20:07
from django.conf import settings
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('comment', '0001_initial'),
('problem', '0007_problem_problem_visible_idx'),
('submission', '0004_submission_problem_user_idx'),
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
]
operations = [
migrations.AddIndex(
model_name='comment',
index=models.Index(fields=['problem', 'create_time'], name='comment_problem_time_idx'),
),
]

View File

@@ -40,5 +40,8 @@ class Comment(models.Model):
class Meta: class Meta:
db_table = "comment" db_table = "comment"
ordering = ("-create_time",) ordering = ("-create_time",)
indexes = [
models.Index(fields=["problem", "create_time"], name="comment_problem_time_idx"),
]

View File

@@ -0,0 +1,23 @@
# Generated by Django 6.0 on 2026-04-23 20:07
from django.conf import settings
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('contest', '0002_acmcontestrank_acm_rank_order_idx_and_more'),
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
]
operations = [
migrations.AddIndex(
model_name='acmcontestrank',
index=models.Index(fields=['contest', 'user'], name='acm_rank_contest_user_idx'),
),
migrations.AddIndex(
model_name='oicontestrank',
index=models.Index(fields=['contest', 'user'], name='oi_rank_contest_user_idx'),
),
]

View File

@@ -82,6 +82,7 @@ class ACMContestRank(AbstractContestRank):
indexes = [ indexes = [
models.Index(fields=["contest", "accepted_number", "total_time"], models.Index(fields=["contest", "accepted_number", "total_time"],
name="acm_rank_order_idx"), name="acm_rank_order_idx"),
models.Index(fields=["contest", "user"], name="acm_rank_contest_user_idx"),
] ]
@@ -96,6 +97,7 @@ class OIContestRank(AbstractContestRank):
unique_together = (("user", "contest"),) unique_together = (("user", "contest"),)
indexes = [ indexes = [
models.Index(fields=["contest", "total_score"], name="oi_rank_order_idx"), models.Index(fields=["contest", "total_score"], name="oi_rank_order_idx"),
models.Index(fields=["contest", "user"], name="oi_rank_contest_user_idx"),
] ]

View File

@@ -0,0 +1,20 @@
# Generated by Django 6.0 on 2026-04-23 20:07
from django.conf import settings
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('message', '0001_initial'),
('submission', '0004_submission_problem_user_idx'),
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
]
operations = [
migrations.AddIndex(
model_name='message',
index=models.Index(fields=['recipient', 'create_time'], name='message_recipient_time_idx'),
),
]

View File

@@ -17,3 +17,6 @@ class Message(models.Model):
class Meta: class Meta:
db_table = "message" db_table = "message"
ordering = ("-create_time",) ordering = ("-create_time",)
indexes = [
models.Index(fields=["recipient", "create_time"], name="message_recipient_time_idx"),
]

View File

@@ -0,0 +1,20 @@
# Generated by Django 6.0 on 2026-04-23 20:07
from django.conf import settings
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('contest', '0003_acmcontestrank_acm_rank_contest_user_idx_and_more'),
('problem', '0006_problem_problem_contest_visible_idx'),
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
]
operations = [
migrations.AddIndex(
model_name='problem',
index=models.Index(fields=['visible'], name='problem_visible_idx'),
),
]

View File

@@ -95,6 +95,7 @@ class Problem(models.Model):
ordering = ("create_time",) ordering = ("create_time",)
indexes = [ indexes = [
models.Index(fields=["contest", "visible"], name="problem_contest_visible_idx"), models.Index(fields=["contest", "visible"], name="problem_contest_visible_idx"),
models.Index(fields=["visible"], name="problem_visible_idx"),
] ]
def add_submission_number(self): def add_submission_number(self):

View File

@@ -93,37 +93,36 @@ class ProblemSetBadge(models.Model):
def recalculate_user_badges(self): def recalculate_user_badges(self):
"""重新计算所有用户的徽章资格""" """重新计算所有用户的徽章资格"""
# 获取所有已加入该题单的用户进度 from django.db import transaction
user_progresses = ProblemSetProgress.objects.filter(problemset=self.problemset) user_progresses = ProblemSetProgress.objects.filter(problemset=self.problemset)
new_badges = [
UserBadge(user=progress.user, badge=self)
for progress in user_progresses
if self._is_eligible(progress)
]
with transaction.atomic():
UserBadge.objects.filter(badge=self).delete()
if new_badges:
UserBadge.objects.bulk_create(new_badges)
# 删除该徽章的所有现有用户徽章记录 def _is_eligible(self, progress):
UserBadge.objects.filter(badge=self).delete() """判断用户进度是否满足该徽章条件(纯逻辑,不查数据库)"""
if self.condition_type == "all_problems":
# 重新评估每个用户的徽章资格 return progress.completed_problems_count == progress.total_problems_count
for progress in user_progresses: if self.condition_type == "problem_count":
self._check_user_badge_eligibility(progress) return progress.completed_problems_count >= self.condition_value
if self.condition_type == "score":
return progress.total_score >= self.condition_value
return False
def _check_user_badge_eligibility(self, progress): def _check_user_badge_eligibility(self, progress):
"""检查用户是否符合该徽章的条件""" """检查并授予单个用户的徽章(供外部单次调用)"""
if self._is_eligible(progress) and not UserBadge.objects.filter(
# 检查是否已经拥有该徽章 user=progress.user, badge=self
if UserBadge.objects.filter(user=progress.user, badge=self).exists(): ).exists():
return False UserBadge.objects.create(user=progress.user, badge=self)
return True
# 根据条件类型检查用户是否符合条件
if self.condition_type == "all_problems":
if progress.completed_problems_count == progress.total_problems_count:
UserBadge.objects.create(user=progress.user, badge=self)
return True
elif self.condition_type == "problem_count":
if progress.completed_problems_count >= self.condition_value:
UserBadge.objects.create(user=progress.user, badge=self)
return True
elif self.condition_type == "score":
if progress.total_score >= self.condition_value:
UserBadge.objects.create(user=progress.user, badge=self)
return True
return False return False

View File

@@ -0,0 +1,19 @@
# Generated by Django 6.0 on 2026-04-23 20:07
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('contest', '0003_acmcontestrank_acm_rank_contest_user_idx_and_more'),
('problem', '0007_problem_problem_visible_idx'),
('submission', '0003_add_contest_create_time_idx'),
]
operations = [
migrations.AddIndex(
model_name='submission',
index=models.Index(fields=['problem_id', 'user_id'], name='problem_user_idx'),
),
]

View File

@@ -64,6 +64,9 @@ class Submission(models.Model):
models.Index( models.Index(
fields=["contest_id", "-create_time"], name="contest_create_time_idx" fields=["contest_id", "-create_time"], name="contest_create_time_idx"
), ),
models.Index(
fields=["problem_id", "user_id"], name="problem_user_idx"
),
] ]
def __str__(self): def __str__(self):