feat(backend): optimize sql queries in Audit Logger#1022
Merged
emdevelopa merged 2 commits intoJun 26, 2026
Merged
Conversation
|
@emmanuelStack654 is attempting to deploy a commit to the Emmanuel's projects Team on Vercel. A member of the Team first needs to authorize it. |
|
@emmanuelStack654 Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits. You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀 |
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.
closes #904
This pull request optimizes the SQL query performance within the Audit Logger service (auditService.js) and helper (audit.js). Previously, the paginated log retrieval relied on a COUNT(*) OVER() window function to retrieve the total count and log data in a single round-trip. While this avoided a round-trip, it forced PostgreSQL to scan and sort all matching logs for a merchant before applying the limit and offset. In production databases with large volumes of log records, this results in significant performance degradation.
To resolve this issue, the retrieval logic has been restructured to query the database count and log records in parallel using Promise.all. The count query performs a quick index-only scan on the composite (merchant_id, timestamp) index, and the logs query utilizes a backward index scan that terminates immediately after retrieving the specified page limit. Furthermore, all query interactions have been migrated to the optimized database pooler (optimizedQuery and optimizedWrite), integrating query result caching, write invalidation, database-level rate limiting, and parameter signature verification.
Unit tests in auditService.test.js and audit.test.js have been updated to align with the parallelized query execution model. The Vitest database mock was extended to mock both direct pool queries and retried queries, and assertions were updated to verify the separate count and data fetch patterns.