refactor: optimize many-to-many relation loading for audit tracking - #173
Open
sundaram-logicloop wants to merge 1 commit into
Open
sundaram-logicloop wants to merge 1 commit into
sundaram-logicloop wants to merge 1 commit into
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary Updating an entity that has several many-to-many relations flagged with enableAuditTracking: true can time out (canceling statement due to statement timeout) instead of completing, once each relation has more than a small handful of related rows. Confirmed in production on PUT /api/country/:id (Country has 7 audit-tracked many-to-many relations: product, section, pageSeoMetadara, seo, Blogs, falconBanner, testimonial).
Root cause CRUDService.prepareManyToManyAuditSnapshot() (crud.service.ts) and AuditSubscriber.prepareManyToManyAuditUpdateSnapshot() (audit.subscriber.ts) each build the audit before/after snapshot with a single repo.findOne({ relations: {...} }) call that includes every audit-tracked relation at once. TypeORM compiles that into one SQL query with a LEFT JOIN per relation. Joining N sibling many-to-many relations off the same row in a single query produces the cartesian product of their row counts, not the sum — e.g. 7 relations at ~150 rows each yields up to 150⁷ (~1.7 × 10¹⁵) result rows for a single query, which blows past any reasonable statement timeout even though no individual relation holds "a lot" of data.