django-DefectDojo icon indicating copy to clipboard operation
django-DefectDojo copied to clipboard

[performance] Findings list UI loads one Endpoint per row: status_finding is prefetched but status_finding__endpoint is not (83 queries for 25 rows, 155 for 100)

Open Ludocius opened this issue 4 weeks ago • 1 comments

Description

Every row of the findings list (/finding, /test/<id>, product/engagement finding tabs) renders the endpoint popover with {{ endpoint_status.endpoint }}. prefetch_for_findings() prefetches status_finding but not status_finding__endpoint, so Django lazily loads one Endpoint per finding (SELECT ... FROM "dojo_endpoint" WHERE id = ? LIMIT 21). The endpoints prefetch that is already there does not help, because the template walks status_finding (to show mitigated/active per endpoint), not endpoints.

Test setup: official image defectdojo/defectdojo-django:2.50.1 + defectdojo-nginx:2.50.1, upstream docker-compose.yml (Postgres 17.6, Redis 7.2, 4 uWSGI processes x 4 threads), Docker Desktop on Windows 11. Data: 6,000 findings created through /api/v2/import-scan/ (two Generic Findings Import reports of 3,000 findings, one endpoint each) plus 30 empty products. Query counts come from django.test.utils.CaptureQueriesContext around django.test.Client requests executed inside the uwsgi container; load numbers from hey -z 20s -c 20 against nginx; memory/CPU from docker stats.

Measured (every finding has one endpoint):

  • /finding?page_size=25: 83 queries, of which 25 are the per-row endpoint load (1,843 ms wall cold / ~500 ms warm)
  • /finding?page_size=100: 155 queries, 100 per-row endpoint loads
  • /test/2 (25 findings): 105 queries, 25 per-row endpoint loads

Query count grows linearly with page size; with the usual several endpoints per finding it is one query per Endpoint_Status row, not per finding.

PoC

[UI findings list] GET /finding?page_size=25
   HTTP 200 | 83 SQL queries  | wall 1843 ms | body 800 KB
      25 x  SELECT "dojo_endpoint".* FROM "dojo_endpoint" WHERE "dojo_endpoint"."id" = ? LIMIT 21
[UI findings list 100] GET /finding?page_size=100
   HTTP 200 | 155 SQL queries | wall 830 ms  | body 2113 KB
     100 x  SELECT "dojo_endpoint".* FROM "dojo_endpoint" WHERE "dojo_endpoint"."id" = ? LIMIT 21

DD-PERF-UI-FINDINGS

Before / After

Before (dojo/finding/queries.py)

prefetched_findings = prefetched_findings.prefetch_related(
    "notes",
    "tags",
    "endpoints",
    "status_finding",
    "finding_group_set",
    ...
)

After: load the endpoint together with its status row

from django.db.models import Prefetch

prefetched_findings = prefetched_findings.prefetch_related(
    "notes",
    "tags",
    "endpoints",
    Prefetch(
        "status_finding",
        queryset=Endpoint_Status.objects.select_related("endpoint").order_by("endpoint__host"),
    ),
    "finding_group_set",
    ...
)

Expected result: 83 -> 58 queries for 25 rows and 155 -> 55 for 100 rows (one status_finding query with a join instead of N lazy loads); the endpoints prefetch can then be dropped if nothing else in the row uses it.

Measured on the 2.50.1 image (commit 8292998); the same code is present on dev at 237458c. Links above point to both.

Ludocius avatar Nov 07 '26 04:11 Ludocius

I’ll take this on. I’ll check the endpoint prefetch on dev and add regression coverage for query counts and the existing endpoint display.

euvoor avatar Nov 07 '26 19:11 euvoor