Repository navigation
Conversation
A page of /payments ran `WHERE addressId IN (<user's addresses>) AND isPayment ORDER BY timestamp LIMIT 10 OFFSET n`. With an index on (addressId, isPayment) only, MariaDB read every payment row of the user and sorted them all to keep ten, so each page, page change and re-sort cost time proportional to the merchant's whole history. - Index (addressId, isPayment, timestamp), replacing (addressId, isPayment). - Pages in timestamp order are the merge of each address's own index range, read only as far as the page reaches (one UNION ALL subquery per address, falling back to one query over all addresses above 200 of them). Only the page's txs are then fetched with their prices, buttons and invoices. The button page's tx list uses the same path. - Ordering by button name picks the page from (tx, button) pairs and fetches prices and invoices only for it, instead of joining and JSON-aggregating every price link and invoice of every payment before sorting. It now also applies the year and date filters, which the count already did. - Payment years are found with one index lookup per address and per year instead of reading every tx of the user, and only count payments. - txs with the same sort value are ordered by id, so they are neither repeated nor skipped across pages. - Button filters resolve the user's addresses in one query instead of two. Measured on a local MariaDB (1G buffer pool) with a user with 300k payments over 6 addresses, warm (cold, after a restart and dropped page cache): first page, newest first 365ms (723ms) -> 7ms (17ms) page 1000 485ms (582ms) -> 49ms (66ms) last page 799ms -> 255ms sorted by button name 5356ms -> 608ms filtered to a year 82ms -> 8ms count for a year 64ms -> 14ms payment years 74ms -> 6ms button page (242k txs) 374ms -> 5ms The results are identical to the previous implementation's for the same inputs, except for ties, which are now ordered. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_019G564i2sNiHJYQR95m8zGq
- Payments page: the table no longer waits for the button and year lists, which are now fetched together; the first page isn't fetched twice on mount; and the payment count is only fetched again when the filters change, not on every page change and sort. - Button page: the tx page and the tx count are fetched in parallel. - getServerSideProps of most pages: the user profile and the supertokens user are fetched in parallel, and the payments and account pages fetch their other data alongside them. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_019G564i2sNiHJYQR95m8zGq
Contributor
|
Warning Review limit reachedYou've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository. Next included review available in 43 minutes. View limit details
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. |
This branch has not been deployed
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.
Related to #1122, #1072
Description
Why /payments was slow (#1122). Every page ran:
The only usable index was
(addressId, isPayment), so MariaDB read every payment row of the user and filesorted all of them to keep 10.ANALYZEon a 300k-payment user showsr_rows: 300000andUsing filesortfor page 0. Each page change and re-sort paid that cost again, as did the count request that was re-sent with every page. The cost grows linearly with a merchant's history, which is why #1124's index and parallel counts weren't enough. The "sort by button" path was much worse (5+ s). It joined andJSON_ARRAYAGG'd every price link and invoice of every payment before sorting.Changes
(addressId, isPayment, timestamp), replacing(addressId, isPayment). The new index covers every lookup the old one served.fetchTxIdPageByTimestamp). Each address's payments are already in timestamp order in the index, so a page is the merge of each address's firstoffset + limitentries. That is oneUNION ALLsubquery per address, and the outer query keepslimitrows. Page 0 now reads (#addresses × 10) index entries, however many payments the user has. Above 200 addresses it falls back to a single query, which is still index-only thanks to the new index. Only the page's txs are then loaded with prices/buttons/invoices. The button page's tx list (/api/paybutton/transactions/[id]) uses the same path.Using index for group-by) plus one index probe per candidate year, instead of reading every tx of the user. The list now only includes years with payments, since it feeds the payments filter.id, so payments that share a timestamp (or amount) are no longer repeated or skipped across pages.Navigation / client (#1072)
/paymentsno longer blocks the table on/api/paybuttonsand/api/transaction/years. It used to fetch those one after the other and only then start loading payments. They are now fetched together, alongside the table./paymentsno longer fetches the first page twice on mount, and only re-fetches the count when the filters change, not on every page change and sort.getServerSideProps(runs on every navigation):fetchUserWithSupertokensfetches the DB profile and the supertokens-core user in parallel, which affects most pages. The payments and account pages also fetch their other data in parallel.Can /payments use the payment summary table from f9d06f2? I looked at this and decided not to. The summary stores 15-minute buckets, so it can't produce the rows of a page. For the count, it would only be correct for addresses whose summary is built and not stale, and the count must match the listing exactly. With the new index the count is an index-only range scan (~50 ms for 300k payments, 14 ms for a year), and the client now only asks for it when the filters change. That left little to gain for the staleness risk. If counts for very large merchants still show up,
SUM(paymentCount)over built summaries is the next step.Measurements
Local MariaDB (latest image, 1 GB buffer pool). User with 300k payments (+30k outgoing txs) over 6 addresses/buttons, 600k txs belonging to other users, 2 price links per tx, 500 invoices. Service-level timings, median of 3 warm runs. "Cold" means first call after a DB restart with the OS page cache dropped.
All of these grow linearly with history before the change and stay flat after (except deep pages and the remarks below). That fits the reporter's 5–20 s on accounts with far more payments.
Correctness check: a throwaway script compared the old and new
fetchAllPaymentsByUserIdWithPaginationon that dataset for 13 combinations of page, size, sort direction, button/year/date filters, amount sort and button-name sort, plus count and years. All results were identical, apart from tie order where the old query had none.Test plan
make github-test-unit: 19 suites, 416 tests pass. New unit tests cover the merged-page SQL (per-address limits, ranges, direction, fallback above the address limit, no query when nothing can match), page order after hydration, single-query button address lookup, the button-name path (filters applied, prices not joined), payment years, and the button tx page.tests/integration-tests/paymentsPagination.test.tspages through every payment of a user, with ties within and across addresses and page sizes 1/4/7/100, in both directions and through both the merged and the single-query path. It checks the result against a reference ordering, plus counts, button/year filters, button-name order (once per button), amount sort stability, years and the button tx page.prisma migrate diffbetween the migrations and the schema shows no difference. The migration applies on the 930k-tx dataset in ~1 s.npx tsc --noEmit -p .: no new errors in the touched files. The repo has existing errors elsewhere./payments(no blocking on the filter data, single initial fetch, cached count). Worth a quick manual check that filters, paging, sorting and CSV export still behave.Remarks
Transaction. MariaDB does this online (in-place, no lock on writes), but on a large production table it will take a while.YEAR(FROM_UNIXTIME())on a UTC server). They now come from payments only.Generated by Claude Code