Skip to content

refactor: optimize many-to-many relation loading for audit tracking - #173

Open
sundaram-logicloop wants to merge 1 commit into
devfrom
m2m-audit
Open

sundaram-logicloop wants to merge 1 commit into
devfrom
m2m-audit

Conversation

@sundaram-logicloop

Copy link
Copy Markdown
Contributor

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.

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.

1 participant