Skip to content

Commit 573b853

Browse files
Backend: Add composite index to LeaderboardData model for query optimization (#4989)
1 parent 40a0ffa commit 573b853

File tree

3 files changed

+52
-0
lines changed

3 files changed

+52
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Generated by Django 2.2.20 on 2026-02-06 03:35
2+
3+
from django.db import migrations, models
4+
5+
6+
class Migration(migrations.Migration):
7+
8+
dependencies = [
9+
(
10+
"challenges",
11+
"0115_add_show_scores_on_leaderboard_to_challenge_phase_split",
12+
),
13+
]
14+
15+
operations = [
16+
migrations.AddIndex(
17+
model_name="leaderboarddata",
18+
index=models.Index(
19+
fields=["challenge_phase_split", "is_disabled", "-created_at"],
20+
name="ld_chphase_isdisc_created_idx",
21+
),
22+
),
23+
]

apps/challenges/models.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -573,6 +573,12 @@ def __str__(self):
573573
class Meta:
574574
app_label = "challenges"
575575
db_table = "leaderboard_data"
576+
indexes = [
577+
models.Index(
578+
fields=["challenge_phase_split", "is_disabled", "-created_at"],
579+
name="ld_chphase_isdisc_created_idx",
580+
),
581+
]
576582

577583

578584
class ChallengeConfiguration(TimeStampedModel):

tests/unit/challenges/test_models.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,3 +272,26 @@ def test__str__(self):
272272
"{0} : {1}".format(self.challenge_phase_split, self.submission),
273273
self.leaderboard_data.__str__(),
274274
)
275+
276+
def test_leaderboard_query_index_exists(self):
277+
"""
278+
Verify LeaderboardData has the composite index for leaderboard query
279+
optimization (EVALAI-HY8). The index supports filtering by
280+
challenge_phase_split, is_disabled and ordering by created_at DESC.
281+
"""
282+
index_names = [idx.name for idx in LeaderboardData._meta.indexes]
283+
self.assertIn(
284+
"ld_chphase_isdisc_created_idx",
285+
index_names,
286+
"LeaderboardData should have ld_chphase_isdisc_created_idx index",
287+
)
288+
index = next(
289+
idx
290+
for idx in LeaderboardData._meta.indexes
291+
if idx.name == "ld_chphase_isdisc_created_idx"
292+
)
293+
self.assertEqual(
294+
index.fields,
295+
["challenge_phase_split", "is_disabled", "-created_at"],
296+
"Index should cover challenge_phase_split, is_disabled, created_at DESC",
297+
)

0 commit comments

Comments
 (0)