Pack list view runs O(packs x pack_types x rarities) queries for the probability calculation #5

Closed
opened 2026-10-08 21:43:01 +02:00 by freydis · 0 comments
Owner

`pack_list` in `views/cards.py` prefetches `cards__rarity` and preloads owned card ids, but then calls `prob_at_least_one_new_card` once per pack per pack type, and that function:

  • re-queries `pack.cards.select_related("rarity").all()` (bypassing the prefetch),
  • re-queries `UserCard` per call,
  • queries `RarityProbability` per generation/pack_type per call (same table fetched for every pack of the generation),
  • for god packs runs one `Card.count()` per eligible rarity plus one `Rarity.objects.get` per rarity, per pack, per request.

With ~30 packs and 2-3 pack types this is several hundred queries per page load. Suggested: load the rarity tables once per generation/pack_type, accept pre-fetched cards and owned ids as parameters, and derive god pack weights from the already-built per-rarity card lists.

\`pack_list\` in \`views/cards.py\` prefetches \`cards__rarity\` and preloads owned card ids, but then calls \`prob_at_least_one_new_card\` once per pack per pack type, and that function: - re-queries \`pack.cards.select_related("rarity").all()\` (bypassing the prefetch), - re-queries \`UserCard\` per call, - queries \`RarityProbability\` per generation/pack_type per call (same table fetched for every pack of the generation), - for god packs runs one \`Card.count()\` per eligible rarity plus one \`Rarity.objects.get\` per rarity, per pack, per request. With ~30 packs and 2-3 pack types this is several hundred queries per page load. Suggested: load the rarity tables once per generation/pack_type, accept pre-fetched cards and owned ids as parameters, and derive god pack weights from the already-built per-rarity card lists.
Sign in to join this conversation.
No labels
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
freydis/tcgp-tracker#5
No description provided.