Skip to content

feat(backend): optimize sql queries in Audit Logger#1022

Merged
emdevelopa merged 2 commits into
emdevelopa:mainfrom
emmanuelStack654:feature/be-optimize-sql-queries-in-audit-logger
Jun 26, 2026
Merged

feat(backend): optimize sql queries in Audit Logger#1022
emdevelopa merged 2 commits into
emdevelopa:mainfrom
emmanuelStack654:feature/be-optimize-sql-queries-in-audit-logger

Conversation

@emmanuelStack654

Copy link
Copy Markdown
Contributor

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.

@vercel

vercel Bot commented Jun 26, 2026

Copy link
Copy Markdown

@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.

@drips-wave

drips-wave Bot commented Jun 26, 2026

Copy link
Copy Markdown

@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! 🚀

Learn more about application limits

@emdevelopa emdevelopa merged commit 59955bd into emdevelopa:main Jun 26, 2026
1 of 4 checks passed
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.

[Backend] Optimize SQL queries in Audit Logger

2 participants