Skip to content

perf(dupe-delete): use bulk_delete_findings + correlated subquery in async_dupe_delete#14797

Open
valentijnscholten wants to merge 1 commit intoDefectDojo:devfrom
valentijnscholten:async-dupe-delete-bulk-cascade
Open

perf(dupe-delete): use bulk_delete_findings + correlated subquery in async_dupe_delete#14797
valentijnscholten wants to merge 1 commit intoDefectDojo:devfrom
valentijnscholten:async-dupe-delete-bulk-cascade

Conversation

@valentijnscholten
Copy link
Copy Markdown
Member

@valentijnscholten valentijnscholten commented May 3, 2026

Summary

Usually the number of findings deleted in the asyn delete job is small, but let's use the optimized cascade_delete method anyway to make it efficient.

  • Replaces the per-row Finding.delete() loop in async_dupe_delete with bulk_delete_findings, which uses raw SQL cascade (cascade_delete_related_objects) instead of Django's per-object Collector.
  • Moves excess-duplicate selection fully into the DB via a correlated subquery that counts newer siblings per original (later date, or same date but higher id). A duplicate is excess when newer_cnt >= max_dupes, eliminating the Python-side per-original counting loop.
  • Adds select_related("test__engagement__product") + only("id", "test_id") to the collection pass to avoid N+1 product lookups.
  • The deleted findings are pure duplicates (original_finding is empty), so skipping finding_delete()'s duplicate-cluster reconfiguration is safe.

…async_dupe_delete

Replace per-row Finding.delete() loop with bulk_delete_findings (raw SQL
cascade) and move excess-duplicate selection fully into the DB via a
correlated subquery that counts newer siblings per original.
select_related + only() eliminate the N+1 product lookup.
@valentijnscholten valentijnscholten added this to the 2.59.0 milestone May 3, 2026
@Maffooch Maffooch requested review from Jino-T and blakeaowens May 4, 2026 18:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants