Skip to content

[BUG] N+1 Problem on /v2/classes endpoint #960

Description

@calumbell

Bug description

The recently installed Django Debug Toolbar highlighted an issue on the /v2/classes that surface while I was exploring another issue:

### SQL queries from 1 connection
- **default** 19.06 ms (135 queries including 125 similar and 113 duplicates )

Classic N+1 behaviour. However on closer investigation it doesn't seem quite the same as other N+1 errors we have encountered on the server, where the problem is clearly triggered by a specific FK being incorrectly joined or prefetched.

SELECT "api_v2_characterclass"."name",
       "api_v2_characterclass"."desc",
       "api_v2_characterclass"."document_id",
       "api_v2_characterclass"."key",
       "api_v2_characterclass"."subclass_of_id",
       "api_v2_characterclass"."hit_dice",
       "api_v2_characterclass"."caster_type"
  FROM "api_v2_characterclass"
 WHERE "api_v2_characterclass"."key" = 'srd_warlock'
 LIMIT 21
125 similar queries. Duplicated 9 times. 

Steps to Reproduce

  1. Fork the staging branch of the Open5e API. Install deps.
  2. Visit the /v2/classes endpoint in a web browser
  3. Investigate the Django Debug Toolbar

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions