Skip to content

fix: optimise segment deletion and cloning with bulk operations#6401

Merged
gagantrivedi merged 4 commits intomainfrom
fix/segment-delete-n-plus-one
Dec 12, 2025
Merged

fix: optimise segment deletion and cloning with bulk operations#6401
gagantrivedi merged 4 commits intomainfrom
fix/segment-delete-n-plus-one

Conversation

@gagantrivedi
Copy link
Member

@gagantrivedi gagantrivedi commented Dec 12, 2025

Thanks for submitting a PR! Please check the boxes below:

  • I have added information to docs/ if required so people know about the feature!
  • I have filled in the "Changes" section below?
  • I have filled in the "How did you test this code" section below?
  • I have used a Conventional Commit title for this Pull Request

Changes

Optimize segment deletion and cloning to reduce database queries from O(n) to O(depth), where n is the number of rules and conditions. This addresses high DB CPU from N+1 queries.

Segment Deletion

  • Add segments/services.py with optimized delete_segment function
  • Use bulk UPDATE for soft-deleting rules/conditions instead of individual deletes
  • Add async task for audit log creation to isolate from delete transaction
  • Move AuthorData to core/dataclasses.py for reuse

Segment Cloning

  • Add copy_segment_rules_and_conditions to services.py
  • Use bulk_create instead of individual saves for rules and conditions
  • Process rules level-by-level to maintain parent relationships

Query Count Improvements

Operation Before After
Delete segment O(n) 26 queries (constant)
Clone segment O(n) 19 queries (constant)
Copy rules/conditions O(n) 10 queries (constant)

How did you test this code?

Added unit tests with explicit query count assertions to verify O(depth) complexity.

@gagantrivedi gagantrivedi requested a review from a team as a code owner December 12, 2025 08:22
@gagantrivedi gagantrivedi requested review from khvn26 and removed request for a team December 12, 2025 08:22
@vercel
Copy link

vercel bot commented Dec 12, 2025

The latest updates on your projects. Learn more about Vercel for GitHub.

3 Skipped Deployments
Project Deployment Preview Comments Updated (UTC)
docs Ignored Ignored Preview Dec 12, 2025 11:21am
flagsmith-frontend-preview Ignored Ignored Preview Dec 12, 2025 11:21am
flagsmith-frontend-staging Ignored Ignored Preview Dec 12, 2025 11:21am

@github-actions github-actions bot added api Issue related to the REST API fix labels Dec 12, 2025
@github-actions
Copy link
Contributor

github-actions bot commented Dec 12, 2025

Docker builds report

Image Build Status Security report
ghcr.io/flagsmith/flagsmith-e2e:pr-6401 Finished ✅ Skipped
ghcr.io/flagsmith/flagsmith-frontend:pr-6401 Finished ✅ Results
ghcr.io/flagsmith/flagsmith-frontend:pr-6401 Finished ✅ Results
ghcr.io/flagsmith/flagsmith-api:pr-6401 Finished ✅ Results
ghcr.io/flagsmith/flagsmith-api:pr-6401 Finished ✅ Results
ghcr.io/flagsmith/flagsmith:pr-6401 Finished ✅ Results
ghcr.io/flagsmith/flagsmith:pr-6401 Finished ✅ Results
ghcr.io/flagsmith/flagsmith-private-cloud:pr-6401 Finished ✅ Results
ghcr.io/flagsmith/flagsmith-private-cloud:pr-6401 Finished ✅ Results

Implement bulk UPDATE/DELETE operations for segment deletion to reduce
database queries from O(n) to O(1), where n is the number of rules and
conditions. This addresses high DB CPU from N+1 queries during segment
deletion.

Changes:
- Move AuthorData to core/dataclasses.py for reuse
- Add segments/services.py with optimized delete_segment function
- Add async task for audit log creation to isolate from delete
- Update views.py to use new service for API deletions
@gagantrivedi gagantrivedi force-pushed the fix/segment-delete-n-plus-one branch from ad35100 to 3182311 Compare December 12, 2025 08:25
@github-actions github-actions bot added fix and removed fix labels Dec 12, 2025
@codecov
Copy link

codecov bot commented Dec 12, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 98.06%. Comparing base (9e584f1) to head (87957a4).
⚠️ Report is 2 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff            @@
##             main    #6401    +/-   ##
========================================
  Coverage   98.06%   98.06%            
========================================
  Files        1289     1292     +3     
  Lines       46293    46498   +205     
========================================
+ Hits        45395    45599   +204     
- Misses        898      899     +1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

- Remove unnecessary `from __future__ import annotations`
- Move local import to top level in tasks.py
- Simplify loop break logic (remove redundant break)
- Add explicit query count (26) to test assertion
@github-actions github-actions bot added fix and removed fix labels Dec 12, 2025
Move copy_rules_and_conditions logic to services.py with bulk operations:
- Use bulk_create instead of individual saves for rules and conditions
- Process rules level-by-level to maintain parent relationships
- Reduces query count from O(n) to O(depth)
@github-actions github-actions bot added fix and removed fix labels Dec 12, 2025
@gagantrivedi gagantrivedi changed the title fix: optimize segment deletion with O(1) bulk operations fix: optimize segment deletion and cloning with bulk operations Dec 12, 2025
matthewelwell
matthewelwell previously approved these changes Dec 12, 2025
Copy link
Contributor

@matthewelwell matthewelwell left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approved with a minor remaining comment.

@github-actions github-actions bot added fix and removed fix labels Dec 12, 2025
@khvn26 khvn26 changed the title fix: optimize segment deletion and cloning with bulk operations fix: optimise segment deletion and cloning with bulk operations Dec 12, 2025
@gagantrivedi gagantrivedi merged commit acd2d4a into main Dec 12, 2025
51 checks passed
@gagantrivedi gagantrivedi deleted the fix/segment-delete-n-plus-one branch December 12, 2025 11:28
Copy link

@cursor cursor bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This PR is being reviewed by Cursor Bugbot

Details

Your team is on the Bugbot Free tier. On this plan, Bugbot will review limited PRs each billing cycle for each member of your team.

To receive Bugbot reviews on all of your PRs, visit the Cursor dashboard to activate Pro and start your 14-day free trial.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

api Issue related to the REST API fix

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants